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"
40 #include "bufferobj.h"
41 #include "externalobjects.h"
44 #include "glformats.h"
46 #include "transformfeedback.h"
48 #include "util/u_atomic.h"
49 #include "util/u_memory.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 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 align_free( bufObj
->Data
);
627 new_data
= 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
, true, false);
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 _mesa_InternalBindElementBuffer(struct gl_context
*ctx
,
1233 struct gl_buffer_object
*buf
)
1235 struct gl_buffer_object
**bindTarget
=
1236 get_buffer_target(ctx
, GL_ELEMENT_ARRAY_BUFFER
);
1238 /* Move the buffer reference from the parameter to the bind point. */
1239 _mesa_reference_buffer_object(ctx
, bindTarget
, NULL
);
1245 * Binds a buffer object to a binding point.
1247 * The caller is responsible for validating the offset,
1248 * flushing the vertices and updating NewDriverState.
1251 set_buffer_binding(struct gl_context
*ctx
,
1252 struct gl_buffer_binding
*binding
,
1253 struct gl_buffer_object
*bufObj
,
1256 bool autoSize
, gl_buffer_usage usage
)
1258 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1260 binding
->Offset
= offset
;
1261 binding
->Size
= size
;
1262 binding
->AutomaticSize
= autoSize
;
1264 /* If this is a real buffer object, mark it has having been used
1265 * at some point as an atomic counter buffer.
1268 bufObj
->UsageHistory
|= usage
;
1272 set_buffer_multi_binding(struct gl_context
*ctx
,
1273 const GLuint
*buffers
,
1276 struct gl_buffer_binding
*binding
,
1280 gl_buffer_usage usage
)
1282 struct gl_buffer_object
*bufObj
;
1284 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[idx
])
1285 bufObj
= binding
->BufferObject
;
1288 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, idx
, caller
,
1295 set_buffer_binding(ctx
, binding
, bufObj
, -1, -1, !range
, usage
);
1297 set_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, !range
, usage
);
1301 bind_buffer(struct gl_context
*ctx
,
1302 struct gl_buffer_binding
*binding
,
1303 struct gl_buffer_object
*bufObj
,
1307 uint64_t driver_state
,
1308 gl_buffer_usage usage
)
1310 if (binding
->BufferObject
== bufObj
&&
1311 binding
->Offset
== offset
&&
1312 binding
->Size
== size
&&
1313 binding
->AutomaticSize
== autoSize
) {
1317 FLUSH_VERTICES(ctx
, 0);
1318 ctx
->NewDriverState
|= driver_state
;
1320 set_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
, usage
);
1324 * Binds a buffer object to a uniform buffer binding point.
1326 * Unlike set_buffer_binding(), this function also flushes vertices
1327 * and updates NewDriverState. It also checks if the binding
1328 * has actually changed before updating it.
1331 bind_uniform_buffer(struct gl_context
*ctx
,
1333 struct gl_buffer_object
*bufObj
,
1338 bind_buffer(ctx
, &ctx
->UniformBufferBindings
[index
],
1339 bufObj
, offset
, size
, autoSize
,
1340 ctx
->DriverFlags
.NewUniformBuffer
,
1341 USAGE_UNIFORM_BUFFER
);
1345 * Binds a buffer object to a shader storage buffer binding point.
1347 * Unlike set_ssbo_binding(), this function also flushes vertices
1348 * and updates NewDriverState. It also checks if the binding
1349 * has actually changed before updating it.
1352 bind_shader_storage_buffer(struct gl_context
*ctx
,
1354 struct gl_buffer_object
*bufObj
,
1359 bind_buffer(ctx
, &ctx
->ShaderStorageBufferBindings
[index
],
1360 bufObj
, offset
, size
, autoSize
,
1361 ctx
->DriverFlags
.NewShaderStorageBuffer
,
1362 USAGE_SHADER_STORAGE_BUFFER
);
1366 * Binds a buffer object to an atomic buffer binding point.
1368 * Unlike set_atomic_binding(), this function also flushes vertices
1369 * and updates NewDriverState. It also checks if the binding
1370 * has actually changed before updating it.
1373 bind_atomic_buffer(struct gl_context
*ctx
, unsigned index
,
1374 struct gl_buffer_object
*bufObj
, GLintptr offset
,
1375 GLsizeiptr size
, GLboolean autoSize
)
1377 bind_buffer(ctx
, &ctx
->AtomicBufferBindings
[index
],
1378 bufObj
, offset
, size
, autoSize
,
1379 ctx
->DriverFlags
.NewAtomicBuffer
,
1380 USAGE_ATOMIC_COUNTER_BUFFER
);
1384 * Bind a buffer object to a uniform block binding point.
1385 * As above, but offset = 0.
1388 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
1390 struct gl_buffer_object
*bufObj
)
1392 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
1393 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1397 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
1400 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1402 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1406 * Bind a buffer object to a shader storage block binding point.
1407 * As above, but offset = 0.
1410 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
1412 struct gl_buffer_object
*bufObj
)
1414 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
1415 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1419 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
1422 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1424 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1428 * Bind a buffer object to a shader storage block binding point.
1429 * As above, but offset = 0.
1432 bind_buffer_base_atomic_buffer(struct gl_context
*ctx
,
1434 struct gl_buffer_object
*bufObj
)
1436 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
1437 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1441 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
1444 bind_atomic_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1446 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1450 * Delete a set of buffer objects.
1452 * \param n Number of buffer objects to delete.
1453 * \param ids Array of \c n buffer object IDs.
1456 delete_buffers(struct gl_context
*ctx
, GLsizei n
, const GLuint
*ids
)
1458 FLUSH_VERTICES(ctx
, 0);
1460 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1462 for (GLsizei i
= 0; i
< n
; i
++) {
1463 struct gl_buffer_object
*bufObj
=
1464 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1466 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1469 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1471 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1473 /* unbind any vertex pointers bound to this buffer */
1474 for (j
= 0; j
< ARRAY_SIZE(vao
->BufferBinding
); j
++) {
1475 unbind(ctx
, vao
, j
, bufObj
);
1478 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1479 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1481 if (vao
->IndexBufferObj
== bufObj
) {
1482 bind_buffer_object(ctx
, &vao
->IndexBufferObj
, 0);
1485 /* unbind ARB_draw_indirect binding point */
1486 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1487 bind_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, 0);
1490 /* unbind ARB_indirect_parameters binding point */
1491 if (ctx
->ParameterBuffer
== bufObj
) {
1492 bind_buffer_object(ctx
, &ctx
->ParameterBuffer
, 0);
1495 /* unbind ARB_compute_shader binding point */
1496 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1497 bind_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, 0);
1500 /* unbind ARB_copy_buffer binding points */
1501 if (ctx
->CopyReadBuffer
== bufObj
) {
1502 bind_buffer_object(ctx
, &ctx
->CopyReadBuffer
, 0);
1504 if (ctx
->CopyWriteBuffer
== bufObj
) {
1505 bind_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, 0);
1508 /* unbind transform feedback binding points */
1509 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1510 bind_buffer_object(ctx
, &ctx
->TransformFeedback
.CurrentBuffer
, 0);
1512 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1513 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1514 _mesa_bind_buffer_base_transform_feedback(ctx
,
1515 ctx
->TransformFeedback
.CurrentObject
,
1520 /* unbind UBO binding points */
1521 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1522 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1523 bind_buffer_base_uniform_buffer(ctx
, j
, NULL
);
1527 if (ctx
->UniformBuffer
== bufObj
) {
1528 bind_buffer_object(ctx
, &ctx
->UniformBuffer
, 0);
1531 /* unbind SSBO binding points */
1532 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1533 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1534 bind_buffer_base_shader_storage_buffer(ctx
, j
, NULL
);
1538 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1539 bind_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, 0);
1542 /* unbind Atomci Buffer binding points */
1543 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1544 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1545 bind_buffer_base_atomic_buffer(ctx
, j
, NULL
);
1549 if (ctx
->AtomicBuffer
== bufObj
) {
1550 bind_buffer_object(ctx
, &ctx
->AtomicBuffer
, 0);
1553 /* unbind any pixel pack/unpack pointers bound to this buffer */
1554 if (ctx
->Pack
.BufferObj
== bufObj
) {
1555 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1557 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1558 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1561 if (ctx
->Texture
.BufferObject
== bufObj
) {
1562 bind_buffer_object(ctx
, &ctx
->Texture
.BufferObject
, 0);
1565 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1566 bind_buffer_object(ctx
, &ctx
->ExternalVirtualMemoryBuffer
, 0);
1569 /* unbind query buffer binding point */
1570 if (ctx
->QueryBuffer
== bufObj
) {
1571 bind_buffer_object(ctx
, &ctx
->QueryBuffer
, 0);
1574 /* The ID is immediately freed for re-use */
1575 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1576 /* Make sure we do not run into the classic ABA problem on bind.
1577 * We don't want to allow re-binding a buffer object that's been
1578 * "deleted" by glDeleteBuffers().
1580 * The explicit rebinding to the default object in the current context
1581 * prevents the above in the current context, but another context
1582 * sharing the same objects might suffer from this problem.
1583 * The alternative would be to do the hash lookup in any case on bind
1584 * which would introduce more runtime overhead than this.
1586 bufObj
->DeletePending
= GL_TRUE
;
1587 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1591 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1596 _mesa_DeleteBuffers_no_error(GLsizei n
, const GLuint
*ids
)
1598 GET_CURRENT_CONTEXT(ctx
);
1599 delete_buffers(ctx
, n
, ids
);
1604 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1606 GET_CURRENT_CONTEXT(ctx
);
1609 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1613 delete_buffers(ctx
, n
, ids
);
1618 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1619 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1623 create_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1626 struct gl_buffer_object
*buf
;
1632 * This must be atomic (generation and allocation of buffer object IDs)
1634 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1636 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1638 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1639 * DummyBufferObject. Otherwise, create a new buffer object and insert
1642 for (int i
= 0; i
< n
; i
++) {
1643 buffers
[i
] = first
+ i
;
1645 assert(ctx
->Driver
.NewBufferObject
);
1646 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1648 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCreateBuffers");
1649 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1654 buf
= &DummyBufferObject
;
1656 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1659 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1664 create_buffers_err(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1666 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1668 if (MESA_VERBOSE
& VERBOSE_API
)
1669 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1672 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1676 create_buffers(ctx
, n
, buffers
, dsa
);
1680 * Generate a set of unique buffer object IDs and store them in \c buffers.
1682 * \param n Number of IDs to generate.
1683 * \param buffers Array of \c n locations to store the IDs.
1686 _mesa_GenBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1688 GET_CURRENT_CONTEXT(ctx
);
1689 create_buffers(ctx
, n
, buffers
, false);
1694 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1696 GET_CURRENT_CONTEXT(ctx
);
1697 create_buffers_err(ctx
, n
, buffers
, false);
1701 * Create a set of buffer objects and store their unique IDs in \c buffers.
1703 * \param n Number of IDs to generate.
1704 * \param buffers Array of \c n locations to store the IDs.
1707 _mesa_CreateBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1709 GET_CURRENT_CONTEXT(ctx
);
1710 create_buffers(ctx
, n
, buffers
, true);
1715 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1717 GET_CURRENT_CONTEXT(ctx
);
1718 create_buffers_err(ctx
, n
, buffers
, true);
1723 * Determine if ID is the name of a buffer object.
1725 * \param id ID of the potential buffer object.
1726 * \return \c GL_TRUE if \c id is the name of a buffer object,
1727 * \c GL_FALSE otherwise.
1729 GLboolean GLAPIENTRY
1730 _mesa_IsBuffer(GLuint id
)
1732 struct gl_buffer_object
*bufObj
;
1733 GET_CURRENT_CONTEXT(ctx
);
1734 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1736 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1738 return bufObj
&& bufObj
!= &DummyBufferObject
;
1743 validate_buffer_storage(struct gl_context
*ctx
,
1744 struct gl_buffer_object
*bufObj
, GLsizeiptr size
,
1745 GLbitfield flags
, const char *func
)
1748 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1752 GLbitfield valid_flags
= GL_MAP_READ_BIT
|
1754 GL_MAP_PERSISTENT_BIT
|
1755 GL_MAP_COHERENT_BIT
|
1756 GL_DYNAMIC_STORAGE_BIT
|
1757 GL_CLIENT_STORAGE_BIT
;
1759 if (ctx
->Extensions
.ARB_sparse_buffer
)
1760 valid_flags
|= GL_SPARSE_STORAGE_BIT_ARB
;
1762 if (flags
& ~valid_flags
) {
1763 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1767 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1769 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1770 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1771 * MAP_READ_BIT or MAP_WRITE_BIT."
1773 if (flags
& GL_SPARSE_STORAGE_BIT_ARB
&&
1774 flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) {
1775 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(SPARSE_STORAGE and READ/WRITE)", func
);
1779 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1780 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1781 _mesa_error(ctx
, GL_INVALID_VALUE
,
1782 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1786 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1787 _mesa_error(ctx
, GL_INVALID_VALUE
,
1788 "%s(COHERENT and flags!=PERSISTENT)", func
);
1792 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
1793 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1802 buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1803 struct gl_memory_object
*memObj
, GLenum target
,
1804 GLsizeiptr size
, const GLvoid
*data
, GLbitfield flags
,
1805 GLuint64 offset
, const char *func
)
1809 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1810 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1812 FLUSH_VERTICES(ctx
, 0);
1814 bufObj
->Written
= GL_TRUE
;
1815 bufObj
->Immutable
= GL_TRUE
;
1816 bufObj
->MinMaxCacheDirty
= true;
1819 assert(ctx
->Driver
.BufferDataMem
);
1820 res
= ctx
->Driver
.BufferDataMem(ctx
, target
, size
, memObj
, offset
,
1821 GL_DYNAMIC_DRAW
, bufObj
);
1824 assert(ctx
->Driver
.BufferData
);
1825 res
= ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1830 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1831 /* Even though the interaction between AMD_pinned_memory and
1832 * glBufferStorage is not described in the spec, Graham Sellers
1833 * said that it should behave the same as glBufferData.
1835 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1838 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1844 static ALWAYS_INLINE
void
1845 inlined_buffer_storage(GLenum target
, GLuint buffer
, GLsizeiptr size
,
1846 const GLvoid
*data
, GLbitfield flags
,
1847 GLuint memory
, GLuint64 offset
,
1848 bool dsa
, bool mem
, bool no_error
, const char *func
)
1850 GET_CURRENT_CONTEXT(ctx
);
1851 struct gl_buffer_object
*bufObj
;
1852 struct gl_memory_object
*memObj
= NULL
;
1856 if (!ctx
->Extensions
.EXT_memory_object
) {
1857 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
1861 /* From the EXT_external_objects spec:
1863 * "An INVALID_VALUE error is generated by BufferStorageMemEXT and
1864 * NamedBufferStorageMemEXT if <memory> is 0, or ..."
1867 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(memory == 0)", func
);
1871 memObj
= _mesa_lookup_memory_object(ctx
, memory
);
1875 /* From the EXT_external_objects spec:
1877 * "An INVALID_OPERATION error is generated if <memory> names a
1878 * valid memory object which has no associated memory."
1880 if (!no_error
&& !memObj
->Immutable
) {
1881 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no associated memory)",
1889 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1891 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
1897 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
1898 bufObj
= *bufObjPtr
;
1900 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
1906 if (no_error
|| validate_buffer_storage(ctx
, bufObj
, size
, flags
, func
))
1907 buffer_storage(ctx
, bufObj
, memObj
, target
, size
, data
, flags
, offset
, func
);
1912 _mesa_BufferStorage_no_error(GLenum target
, GLsizeiptr size
,
1913 const GLvoid
*data
, GLbitfield flags
)
1915 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1916 false, false, true, "glBufferStorage");
1921 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1924 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1925 false, false, false, "glBufferStorage");
1929 _mesa_NamedBufferStorageEXT(GLuint buffer
, GLsizeiptr size
,
1930 const GLvoid
*data
, GLbitfield flags
)
1932 GET_CURRENT_CONTEXT(ctx
);
1934 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1935 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1936 &bufObj
, "glNamedBufferStorageEXT"))
1939 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1940 true, false, false, "glNamedBufferStorageEXT");
1945 _mesa_BufferStorageMemEXT(GLenum target
, GLsizeiptr size
,
1946 GLuint memory
, GLuint64 offset
)
1948 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1949 false, true, false, "glBufferStorageMemEXT");
1954 _mesa_BufferStorageMemEXT_no_error(GLenum target
, GLsizeiptr size
,
1955 GLuint memory
, GLuint64 offset
)
1957 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1958 false, true, true, "glBufferStorageMemEXT");
1963 _mesa_NamedBufferStorage_no_error(GLuint buffer
, GLsizeiptr size
,
1964 const GLvoid
*data
, GLbitfield flags
)
1966 /* In direct state access, buffer objects have an unspecified target
1967 * since they are not required to be bound.
1969 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1970 true, false, true, "glNamedBufferStorage");
1975 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1978 /* In direct state access, buffer objects have an unspecified target
1979 * since they are not required to be bound.
1981 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1982 true, false, false, "glNamedBufferStorage");
1986 _mesa_NamedBufferStorageMemEXT(GLuint buffer
, GLsizeiptr size
,
1987 GLuint memory
, GLuint64 offset
)
1989 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1990 true, true, false, "glNamedBufferStorageMemEXT");
1995 _mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer
, GLsizeiptr size
,
1996 GLuint memory
, GLuint64 offset
)
1998 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1999 true, true, true, "glNamedBufferStorageMemEXT");
2003 static ALWAYS_INLINE
void
2004 buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2005 GLenum target
, GLsizeiptr size
, const GLvoid
*data
, GLenum usage
,
2006 const char *func
, bool no_error
)
2010 if (MESA_VERBOSE
& VERBOSE_API
) {
2011 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
2013 _mesa_enum_to_string(target
),
2014 (long int) size
, data
,
2015 _mesa_enum_to_string(usage
));
2020 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
2025 case GL_STREAM_DRAW_ARB
:
2026 valid_usage
= (ctx
->API
!= API_OPENGLES
);
2028 case GL_STATIC_DRAW_ARB
:
2029 case GL_DYNAMIC_DRAW_ARB
:
2032 case GL_STREAM_READ_ARB
:
2033 case GL_STREAM_COPY_ARB
:
2034 case GL_STATIC_READ_ARB
:
2035 case GL_STATIC_COPY_ARB
:
2036 case GL_DYNAMIC_READ_ARB
:
2037 case GL_DYNAMIC_COPY_ARB
:
2038 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
2041 valid_usage
= false;
2046 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
2047 _mesa_enum_to_string(usage
));
2051 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
2052 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
2057 /* Unmap the existing buffer. We'll replace it now. Not an error. */
2058 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
2060 FLUSH_VERTICES(ctx
, 0);
2062 bufObj
->Written
= GL_TRUE
;
2063 bufObj
->MinMaxCacheDirty
= true;
2066 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
2067 bufObj
->Name
, size
, data
, usage
);
2074 assert(ctx
->Driver
.BufferData
);
2075 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
2078 GL_DYNAMIC_STORAGE_BIT
,
2080 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
2082 /* From GL_AMD_pinned_memory:
2084 * INVALID_OPERATION is generated by BufferData if <target> is
2085 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
2086 * mapped to the GPU address space.
2088 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2091 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2097 buffer_data_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2098 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2099 GLenum usage
, const char *func
)
2101 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, false);
2105 buffer_data_no_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2106 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2107 GLenum usage
, const char *func
)
2109 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, true);
2113 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2114 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2115 GLenum usage
, const char *func
)
2117 buffer_data_error(ctx
, bufObj
, target
, size
, data
, usage
, func
);
2121 _mesa_BufferData_no_error(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2124 GET_CURRENT_CONTEXT(ctx
);
2126 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2127 buffer_data_no_error(ctx
, *bufObj
, target
, size
, data
, usage
,
2132 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
2133 const GLvoid
*data
, GLenum usage
)
2135 GET_CURRENT_CONTEXT(ctx
);
2136 struct gl_buffer_object
*bufObj
;
2138 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
2142 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
2147 _mesa_NamedBufferData_no_error(GLuint buffer
, GLsizeiptr size
,
2148 const GLvoid
*data
, GLenum usage
)
2150 GET_CURRENT_CONTEXT(ctx
);
2152 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2153 buffer_data_no_error(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2154 "glNamedBufferData");
2158 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2161 GET_CURRENT_CONTEXT(ctx
);
2162 struct gl_buffer_object
*bufObj
;
2164 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
2168 /* In direct state access, buffer objects have an unspecified target since
2169 * they are not required to be bound.
2171 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2172 "glNamedBufferData");
2176 _mesa_NamedBufferDataEXT(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2179 GET_CURRENT_CONTEXT(ctx
);
2180 struct gl_buffer_object
*bufObj
;
2183 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2184 "glNamedBufferDataEXT(buffer=0)");
2188 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2189 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2190 &bufObj
, "glNamedBufferDataEXT"))
2193 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2194 "glNamedBufferDataEXT");
2198 validate_buffer_sub_data(struct gl_context
*ctx
,
2199 struct gl_buffer_object
*bufObj
,
2200 GLintptr offset
, GLsizeiptr size
,
2203 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2205 /* error already recorded */
2209 if (bufObj
->Immutable
&&
2210 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
2211 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2215 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2216 bufObj
->Usage
== GL_STATIC_COPY
) &&
2217 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
- 1) {
2218 /* If the application declared the buffer as static draw/copy or stream
2219 * draw, it should not be frequently modified with glBufferSubData.
2221 BUFFER_USAGE_WARNING(ctx
,
2222 "using %s(buffer %u, offset %u, size %u) to "
2223 "update a %s buffer",
2224 func
, bufObj
->Name
, offset
, size
,
2225 _mesa_enum_to_string(bufObj
->Usage
));
2233 * Implementation for glBufferSubData and glNamedBufferSubData.
2235 * \param ctx GL context.
2236 * \param bufObj The buffer object.
2237 * \param offset Offset of the first byte of the subdata range.
2238 * \param size Size, in bytes, of the subdata range.
2239 * \param data The data store.
2240 * \param func Name of calling function for recording errors.
2244 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2245 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
)
2250 bufObj
->NumSubDataCalls
++;
2251 bufObj
->Written
= GL_TRUE
;
2252 bufObj
->MinMaxCacheDirty
= true;
2254 assert(ctx
->Driver
.BufferSubData
);
2255 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
2259 static ALWAYS_INLINE
void
2260 buffer_sub_data(GLenum target
, GLuint buffer
, GLintptr offset
,
2261 GLsizeiptr size
, const GLvoid
*data
,
2262 bool dsa
, bool no_error
, const char *func
)
2264 GET_CURRENT_CONTEXT(ctx
);
2265 struct gl_buffer_object
*bufObj
;
2269 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2271 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
2277 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2278 bufObj
= *bufObjPtr
;
2280 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
2286 if (no_error
|| validate_buffer_sub_data(ctx
, bufObj
, offset
, size
, func
))
2287 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2292 _mesa_BufferSubData_no_error(GLenum target
, GLintptr offset
,
2293 GLsizeiptr size
, const GLvoid
*data
)
2295 buffer_sub_data(target
, 0, offset
, size
, data
, false, true,
2301 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
2302 GLsizeiptr size
, const GLvoid
*data
)
2304 buffer_sub_data(target
, 0, offset
, size
, data
, false, false,
2309 _mesa_NamedBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
2310 GLsizeiptr size
, const GLvoid
*data
)
2312 buffer_sub_data(0, buffer
, offset
, size
, data
, true, true,
2313 "glNamedBufferSubData");
2317 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
2318 GLsizeiptr size
, const GLvoid
*data
)
2320 buffer_sub_data(0, buffer
, offset
, size
, data
, true, false,
2321 "glNamedBufferSubData");
2325 _mesa_NamedBufferSubDataEXT(GLuint buffer
, GLintptr offset
,
2326 GLsizeiptr size
, const GLvoid
*data
)
2328 GET_CURRENT_CONTEXT(ctx
);
2329 struct gl_buffer_object
*bufObj
;
2332 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2333 "glNamedBufferSubDataEXT(buffer=0)");
2337 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2338 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2339 &bufObj
, "glNamedBufferSubDataEXT"))
2342 if (validate_buffer_sub_data(ctx
, bufObj
, offset
, size
,
2343 "glNamedBufferSubDataEXT")) {
2344 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2350 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
2351 GLsizeiptr size
, GLvoid
*data
)
2353 GET_CURRENT_CONTEXT(ctx
);
2354 struct gl_buffer_object
*bufObj
;
2356 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
2357 GL_INVALID_OPERATION
);
2361 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2362 "glGetBufferSubData")) {
2366 assert(ctx
->Driver
.GetBufferSubData
);
2367 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2371 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
2372 GLsizeiptr size
, GLvoid
*data
)
2374 GET_CURRENT_CONTEXT(ctx
);
2375 struct gl_buffer_object
*bufObj
;
2377 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2378 "glGetNamedBufferSubData");
2382 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2383 "glGetNamedBufferSubData")) {
2387 assert(ctx
->Driver
.GetBufferSubData
);
2388 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2393 _mesa_GetNamedBufferSubDataEXT(GLuint buffer
, GLintptr offset
,
2394 GLsizeiptr size
, GLvoid
*data
)
2396 GET_CURRENT_CONTEXT(ctx
);
2397 struct gl_buffer_object
*bufObj
;
2400 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2401 "glGetNamedBufferSubDataEXT(buffer=0)");
2405 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2406 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2407 &bufObj
, "glGetNamedBufferSubDataEXT"))
2410 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2411 "glGetNamedBufferSubDataEXT")) {
2415 assert(ctx
->Driver
.GetBufferSubData
);
2416 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2420 * \param subdata true if caller is *SubData, false if *Data
2422 static ALWAYS_INLINE
void
2423 clear_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2424 GLenum internalformat
, GLintptr offset
, GLsizeiptr size
,
2425 GLenum format
, GLenum type
, const GLvoid
*data
,
2426 const char *func
, bool subdata
, bool no_error
)
2428 mesa_format mesaFormat
;
2429 GLubyte clearValue
[MAX_PIXEL_BYTES
];
2430 GLsizeiptr clearValueSize
;
2432 /* This checks for disallowed mappings. */
2433 if (!no_error
&& !buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2439 mesaFormat
= _mesa_get_texbuffer_format(ctx
, internalformat
);
2441 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
2442 format
, type
, func
);
2445 if (mesaFormat
== MESA_FORMAT_NONE
)
2448 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
2450 (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0)) {
2451 _mesa_error(ctx
, GL_INVALID_VALUE
,
2452 "%s(offset or size is not a multiple of "
2453 "internalformat size)", func
);
2457 /* Bail early. Negative size has already been checked. */
2461 bufObj
->MinMaxCacheDirty
= true;
2464 /* clear to zeros, per the spec */
2465 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2466 NULL
, clearValueSize
, bufObj
);
2470 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
2471 format
, type
, data
, func
)) {
2475 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2476 clearValue
, clearValueSize
, bufObj
);
2480 clear_buffer_sub_data_error(struct gl_context
*ctx
,
2481 struct gl_buffer_object
*bufObj
,
2482 GLenum internalformat
, GLintptr offset
,
2483 GLsizeiptr size
, GLenum format
, GLenum type
,
2484 const GLvoid
*data
, const char *func
, bool subdata
)
2486 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2487 type
, data
, func
, subdata
, false);
2492 clear_buffer_sub_data_no_error(struct gl_context
*ctx
,
2493 struct gl_buffer_object
*bufObj
,
2494 GLenum internalformat
, GLintptr offset
,
2495 GLsizeiptr size
, GLenum format
, GLenum type
,
2496 const GLvoid
*data
, const char *func
,
2499 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2500 type
, data
, func
, subdata
, true);
2505 _mesa_ClearBufferData_no_error(GLenum target
, GLenum internalformat
,
2506 GLenum format
, GLenum type
, const GLvoid
*data
)
2508 GET_CURRENT_CONTEXT(ctx
);
2510 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2511 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, 0,
2512 (*bufObj
)->Size
, format
, type
, data
,
2513 "glClearBufferData", false);
2518 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
2519 GLenum type
, const GLvoid
*data
)
2521 GET_CURRENT_CONTEXT(ctx
);
2522 struct gl_buffer_object
*bufObj
;
2524 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
2528 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2529 format
, type
, data
, "glClearBufferData", false);
2534 _mesa_ClearNamedBufferData_no_error(GLuint buffer
, GLenum internalformat
,
2535 GLenum format
, GLenum type
,
2538 GET_CURRENT_CONTEXT(ctx
);
2540 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2541 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2542 format
, type
, data
, "glClearNamedBufferData",
2548 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
2549 GLenum format
, GLenum type
, const GLvoid
*data
)
2551 GET_CURRENT_CONTEXT(ctx
);
2552 struct gl_buffer_object
*bufObj
;
2554 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
2558 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2559 format
, type
, data
, "glClearNamedBufferData",
2565 _mesa_ClearNamedBufferDataEXT(GLuint buffer
, GLenum internalformat
,
2566 GLenum format
, GLenum type
, const GLvoid
*data
)
2568 GET_CURRENT_CONTEXT(ctx
);
2569 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2570 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2571 &bufObj
, "glClearNamedBufferDataEXT"))
2574 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2575 format
, type
, data
, "glClearNamedBufferDataEXT",
2581 _mesa_ClearBufferSubData_no_error(GLenum target
, GLenum internalformat
,
2582 GLintptr offset
, GLsizeiptr size
,
2583 GLenum format
, GLenum type
,
2586 GET_CURRENT_CONTEXT(ctx
);
2588 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2589 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, offset
, size
,
2590 format
, type
, data
, "glClearBufferSubData",
2596 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
2597 GLintptr offset
, GLsizeiptr size
,
2598 GLenum format
, GLenum type
,
2601 GET_CURRENT_CONTEXT(ctx
);
2602 struct gl_buffer_object
*bufObj
;
2604 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
2608 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2609 format
, type
, data
, "glClearBufferSubData",
2615 _mesa_ClearNamedBufferSubData_no_error(GLuint buffer
, GLenum internalformat
,
2616 GLintptr offset
, GLsizeiptr size
,
2617 GLenum format
, GLenum type
,
2620 GET_CURRENT_CONTEXT(ctx
);
2622 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2623 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, offset
, size
,
2625 "glClearNamedBufferSubData", true);
2630 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
2631 GLintptr offset
, GLsizeiptr size
,
2632 GLenum format
, GLenum type
,
2635 GET_CURRENT_CONTEXT(ctx
);
2636 struct gl_buffer_object
*bufObj
;
2638 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2639 "glClearNamedBufferSubData");
2643 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2644 format
, type
, data
, "glClearNamedBufferSubData",
2649 _mesa_ClearNamedBufferSubDataEXT(GLuint buffer
, GLenum internalformat
,
2650 GLintptr offset
, GLsizeiptr size
,
2651 GLenum format
, GLenum type
,
2654 GET_CURRENT_CONTEXT(ctx
);
2655 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2656 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2657 &bufObj
, "glClearNamedBufferSubDataEXT"))
2660 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2661 format
, type
, data
, "glClearNamedBufferSubDataEXT",
2666 unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
2668 GLboolean status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2669 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2670 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2671 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2672 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2678 validate_and_unmap_buffer(struct gl_context
*ctx
,
2679 struct gl_buffer_object
*bufObj
,
2682 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2684 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2685 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2686 "%s(buffer is not mapped)", func
);
2691 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2692 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2694 /* check that last 100 bytes are still = magic value */
2695 for (i
= 0; i
< 100; i
++) {
2696 GLuint pos
= bufObj
->Size
- i
- 1;
2697 if (buf
[pos
] != 123) {
2698 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2699 " at position %d (value = %u)\n",
2707 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2708 GLuint i
, unchanged
= 0;
2709 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2711 /* check which bytes changed */
2712 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2713 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2720 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2721 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2726 return unmap_buffer(ctx
, bufObj
);
2729 GLboolean GLAPIENTRY
2730 _mesa_UnmapBuffer_no_error(GLenum target
)
2732 GET_CURRENT_CONTEXT(ctx
);
2733 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2734 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2736 return unmap_buffer(ctx
, bufObj
);
2739 GLboolean GLAPIENTRY
2740 _mesa_UnmapBuffer(GLenum target
)
2742 GET_CURRENT_CONTEXT(ctx
);
2743 struct gl_buffer_object
*bufObj
;
2745 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2749 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2752 GLboolean GLAPIENTRY
2753 _mesa_UnmapNamedBufferEXT_no_error(GLuint buffer
)
2755 GET_CURRENT_CONTEXT(ctx
);
2756 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2758 return unmap_buffer(ctx
, bufObj
);
2761 GLboolean GLAPIENTRY
2762 _mesa_UnmapNamedBufferEXT(GLuint buffer
)
2764 GET_CURRENT_CONTEXT(ctx
);
2765 struct gl_buffer_object
*bufObj
;
2768 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2769 "glUnmapNamedBufferEXT(buffer=0)");
2773 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2777 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2782 get_buffer_parameter(struct gl_context
*ctx
,
2783 struct gl_buffer_object
*bufObj
, GLenum pname
,
2784 GLint64
*params
, const char *func
)
2787 case GL_BUFFER_SIZE_ARB
:
2788 *params
= bufObj
->Size
;
2790 case GL_BUFFER_USAGE_ARB
:
2791 *params
= bufObj
->Usage
;
2793 case GL_BUFFER_ACCESS_ARB
:
2794 *params
= simplified_access_mode(ctx
,
2795 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2797 case GL_BUFFER_MAPPED_ARB
:
2798 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2800 case GL_BUFFER_ACCESS_FLAGS
:
2801 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2803 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2805 case GL_BUFFER_MAP_OFFSET
:
2806 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2808 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2810 case GL_BUFFER_MAP_LENGTH
:
2811 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2813 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2815 case GL_BUFFER_IMMUTABLE_STORAGE
:
2816 if (!ctx
->Extensions
.ARB_buffer_storage
)
2818 *params
= bufObj
->Immutable
;
2820 case GL_BUFFER_STORAGE_FLAGS
:
2821 if (!ctx
->Extensions
.ARB_buffer_storage
)
2823 *params
= bufObj
->StorageFlags
;
2832 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2833 _mesa_enum_to_string(pname
));
2838 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2840 GET_CURRENT_CONTEXT(ctx
);
2841 struct gl_buffer_object
*bufObj
;
2844 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2845 GL_INVALID_OPERATION
);
2849 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2850 "glGetBufferParameteriv"))
2851 return; /* Error already recorded. */
2853 *params
= (GLint
) parameter
;
2857 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2859 GET_CURRENT_CONTEXT(ctx
);
2860 struct gl_buffer_object
*bufObj
;
2863 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2864 GL_INVALID_OPERATION
);
2868 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2869 "glGetBufferParameteri64v"))
2870 return; /* Error already recorded. */
2872 *params
= parameter
;
2876 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2878 GET_CURRENT_CONTEXT(ctx
);
2879 struct gl_buffer_object
*bufObj
;
2882 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2883 "glGetNamedBufferParameteriv");
2887 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2888 "glGetNamedBufferParameteriv"))
2889 return; /* Error already recorded. */
2891 *params
= (GLint
) parameter
;
2895 _mesa_GetNamedBufferParameterivEXT(GLuint buffer
, GLenum pname
, GLint
*params
)
2897 GET_CURRENT_CONTEXT(ctx
);
2898 struct gl_buffer_object
*bufObj
;
2902 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2903 "glGetNamedBufferParameterivEXT: buffer=0");
2907 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2908 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2909 &bufObj
, "glGetNamedBufferParameterivEXT"))
2912 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2913 "glGetNamedBufferParameterivEXT"))
2914 return; /* Error already recorded. */
2916 *params
= (GLint
) parameter
;
2920 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2923 GET_CURRENT_CONTEXT(ctx
);
2924 struct gl_buffer_object
*bufObj
;
2927 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2928 "glGetNamedBufferParameteri64v");
2932 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2933 "glGetNamedBufferParameteri64v"))
2934 return; /* Error already recorded. */
2936 *params
= parameter
;
2941 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2943 GET_CURRENT_CONTEXT(ctx
);
2944 struct gl_buffer_object
*bufObj
;
2946 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2947 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2948 "GL_BUFFER_MAP_POINTER)");
2952 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2953 GL_INVALID_OPERATION
);
2957 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2961 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2963 GET_CURRENT_CONTEXT(ctx
);
2964 struct gl_buffer_object
*bufObj
;
2966 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2967 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2968 "GL_BUFFER_MAP_POINTER)");
2972 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2973 "glGetNamedBufferPointerv");
2977 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2981 _mesa_GetNamedBufferPointervEXT(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2983 GET_CURRENT_CONTEXT(ctx
);
2984 struct gl_buffer_object
*bufObj
;
2987 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2988 "glGetNamedBufferPointervEXT(buffer=0)");
2991 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2992 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointervEXT(pname != "
2993 "GL_BUFFER_MAP_POINTER)");
2997 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2998 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2999 &bufObj
, "glGetNamedBufferPointervEXT"))
3002 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
3006 copy_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*src
,
3007 struct gl_buffer_object
*dst
, GLintptr readOffset
,
3008 GLintptr writeOffset
, GLsizeiptr size
, const char *func
)
3010 if (_mesa_check_disallowed_mapping(src
)) {
3011 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3012 "%s(readBuffer is mapped)", func
);
3016 if (_mesa_check_disallowed_mapping(dst
)) {
3017 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3018 "%s(writeBuffer is mapped)", func
);
3022 if (readOffset
< 0) {
3023 _mesa_error(ctx
, GL_INVALID_VALUE
,
3024 "%s(readOffset %d < 0)", func
, (int) readOffset
);
3028 if (writeOffset
< 0) {
3029 _mesa_error(ctx
, GL_INVALID_VALUE
,
3030 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
3035 _mesa_error(ctx
, GL_INVALID_VALUE
,
3036 "%s(size %d < 0)", func
, (int) size
);
3040 if (readOffset
+ size
> src
->Size
) {
3041 _mesa_error(ctx
, GL_INVALID_VALUE
,
3042 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
3043 (int) readOffset
, (int) size
, (int) src
->Size
);
3047 if (writeOffset
+ size
> dst
->Size
) {
3048 _mesa_error(ctx
, GL_INVALID_VALUE
,
3049 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
3050 (int) writeOffset
, (int) size
, (int) dst
->Size
);
3055 if (readOffset
+ size
<= writeOffset
) {
3058 else if (writeOffset
+ size
<= readOffset
) {
3062 /* overlapping src/dst is illegal */
3063 _mesa_error(ctx
, GL_INVALID_VALUE
,
3064 "%s(overlapping src/dst)", func
);
3069 dst
->MinMaxCacheDirty
= true;
3071 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
3075 _mesa_CopyBufferSubData_no_error(GLenum readTarget
, GLenum writeTarget
,
3076 GLintptr readOffset
, GLintptr writeOffset
,
3079 GET_CURRENT_CONTEXT(ctx
);
3081 struct gl_buffer_object
**src_ptr
= get_buffer_target(ctx
, readTarget
);
3082 struct gl_buffer_object
*src
= *src_ptr
;
3084 struct gl_buffer_object
**dst_ptr
= get_buffer_target(ctx
, writeTarget
);
3085 struct gl_buffer_object
*dst
= *dst_ptr
;
3087 dst
->MinMaxCacheDirty
= true;
3088 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
3093 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
3094 GLintptr readOffset
, GLintptr writeOffset
,
3097 GET_CURRENT_CONTEXT(ctx
);
3098 struct gl_buffer_object
*src
, *dst
;
3100 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
3101 GL_INVALID_OPERATION
);
3105 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
3106 GL_INVALID_OPERATION
);
3110 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
3111 "glCopyBufferSubData");
3115 _mesa_NamedCopyBufferSubDataEXT(GLuint readBuffer
, GLuint writeBuffer
,
3116 GLintptr readOffset
, GLintptr writeOffset
,
3119 GET_CURRENT_CONTEXT(ctx
);
3120 struct gl_buffer_object
*src
, *dst
;
3122 src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
3123 if (!_mesa_handle_bind_buffer_gen(ctx
, readBuffer
,
3125 "glNamedCopyBufferSubDataEXT"))
3128 dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
3129 if (!_mesa_handle_bind_buffer_gen(ctx
, writeBuffer
,
3131 "glNamedCopyBufferSubDataEXT"))
3134 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
3135 "glNamedCopyBufferSubDataEXT");
3139 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer
, GLuint writeBuffer
,
3140 GLintptr readOffset
,
3141 GLintptr writeOffset
, GLsizeiptr size
)
3143 GET_CURRENT_CONTEXT(ctx
);
3145 struct gl_buffer_object
*src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
3146 struct gl_buffer_object
*dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
3148 dst
->MinMaxCacheDirty
= true;
3149 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
3154 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
3155 GLintptr readOffset
, GLintptr writeOffset
,
3158 GET_CURRENT_CONTEXT(ctx
);
3159 struct gl_buffer_object
*src
, *dst
;
3161 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
3162 "glCopyNamedBufferSubData");
3166 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
3167 "glCopyNamedBufferSubData");
3171 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
3172 "glCopyNamedBufferSubData");
3176 _mesa_InternalBufferSubDataCopyMESA(GLintptr srcBuffer
, GLuint srcOffset
,
3177 GLuint dstTargetOrName
, GLintptr dstOffset
,
3178 GLsizeiptr size
, GLboolean named
,
3181 GET_CURRENT_CONTEXT(ctx
);
3182 struct gl_buffer_object
*src
= (struct gl_buffer_object
*)srcBuffer
;
3183 struct gl_buffer_object
*dst
;
3186 /* Handle behavior for all 3 variants. */
3187 if (named
&& ext_dsa
) {
3188 func
= "glNamedBufferSubDataEXT";
3189 dst
= _mesa_lookup_bufferobj(ctx
, dstTargetOrName
);
3190 if (!_mesa_handle_bind_buffer_gen(ctx
, dstTargetOrName
, &dst
, func
))
3193 func
= "glNamedBufferSubData";
3194 dst
= _mesa_lookup_bufferobj_err(ctx
, dstTargetOrName
, func
);
3199 func
= "glBufferSubData";
3200 dst
= get_buffer(ctx
, func
, dstTargetOrName
, GL_INVALID_OPERATION
);
3205 if (!validate_buffer_sub_data(ctx
, dst
, dstOffset
, size
, func
))
3206 goto done
; /* the error is already set */
3208 dst
->MinMaxCacheDirty
= true;
3209 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, srcOffset
, dstOffset
, size
);
3212 /* The caller passes the reference to this function, so unreference it. */
3213 _mesa_reference_buffer_object(ctx
, &src
, NULL
);
3217 validate_map_buffer_range(struct gl_context
*ctx
,
3218 struct gl_buffer_object
*bufObj
, GLintptr offset
,
3219 GLsizeiptr length
, GLbitfield access
,
3222 GLbitfield allowed_access
;
3224 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, false);
3227 _mesa_error(ctx
, GL_INVALID_VALUE
,
3228 "%s(offset %ld < 0)", func
, (long) offset
);
3233 _mesa_error(ctx
, GL_INVALID_VALUE
,
3234 "%s(length %ld < 0)", func
, (long) length
);
3238 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
3240 * "An INVALID_OPERATION error is generated for any of the following
3243 * * <length> is zero."
3245 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
3246 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
3250 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
3254 allowed_access
= GL_MAP_READ_BIT
|
3256 GL_MAP_INVALIDATE_RANGE_BIT
|
3257 GL_MAP_INVALIDATE_BUFFER_BIT
|
3258 GL_MAP_FLUSH_EXPLICIT_BIT
|
3259 GL_MAP_UNSYNCHRONIZED_BIT
;
3261 if (ctx
->Extensions
.ARB_buffer_storage
) {
3262 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
3263 GL_MAP_COHERENT_BIT
;
3266 if (access
& ~allowed_access
) {
3267 /* generate an error if any bits other than those allowed are set */
3268 _mesa_error(ctx
, GL_INVALID_VALUE
,
3269 "%s(access has undefined bits set)", func
);
3273 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
3274 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3275 "%s(access indicates neither read or write)", func
);
3279 if ((access
& GL_MAP_READ_BIT
) &&
3280 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
3281 GL_MAP_INVALIDATE_BUFFER_BIT
|
3282 GL_MAP_UNSYNCHRONIZED_BIT
))) {
3283 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3284 "%s(read access with disallowed bits)", func
);
3288 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
3289 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
3290 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3291 "%s(access has flush explicit without write)", func
);
3295 if (access
& GL_MAP_READ_BIT
&&
3296 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
3297 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3298 "%s(buffer does not allow read access)", func
);
3302 if (access
& GL_MAP_WRITE_BIT
&&
3303 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
3304 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3305 "%s(buffer does not allow write access)", func
);
3309 if (access
& GL_MAP_COHERENT_BIT
&&
3310 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
3311 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3312 "%s(buffer does not allow coherent access)", func
);
3316 if (access
& GL_MAP_PERSISTENT_BIT
&&
3317 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
3318 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3319 "%s(buffer does not allow persistent access)", func
);
3323 if (offset
+ length
> bufObj
->Size
) {
3324 _mesa_error(ctx
, GL_INVALID_VALUE
,
3325 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
3326 (unsigned long) offset
, (unsigned long) length
,
3327 (unsigned long) bufObj
->Size
);
3331 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3332 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3333 "%s(buffer already mapped)", func
);
3337 if (access
& GL_MAP_WRITE_BIT
) {
3338 bufObj
->NumMapBufferWriteCalls
++;
3339 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
3340 bufObj
->Usage
== GL_STATIC_COPY
) &&
3341 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
3342 BUFFER_USAGE_WARNING(ctx
,
3343 "using %s(buffer %u, offset %u, length %u) to "
3344 "update a %s buffer",
3345 func
, bufObj
->Name
, offset
, length
,
3346 _mesa_enum_to_string(bufObj
->Usage
));
3354 map_buffer_range(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
3355 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
3358 if (!bufObj
->Size
) {
3359 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
3363 assert(ctx
->Driver
.MapBufferRange
);
3364 void *map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
3367 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
3370 /* The driver callback should have set all these fields.
3371 * This is important because other modules (like VBO) might call
3372 * the driver function directly.
3374 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
3375 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
3376 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
3377 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
3380 if (access
& GL_MAP_WRITE_BIT
) {
3381 bufObj
->Written
= GL_TRUE
;
3382 bufObj
->MinMaxCacheDirty
= true;
3386 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3387 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
3388 bufObj
->Name
, bufObj
->Size
, access
);
3389 /* Access must be write only */
3390 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
3392 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
3393 for (i
= 0; i
< bufObj
->Size
; i
++)
3400 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3401 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
3403 /* buffer is 100 bytes larger than requested, fill with magic value */
3404 for (i
= 0; i
< 100; i
++) {
3405 buf
[bufObj
->Size
- i
- 1] = 123;
3414 _mesa_MapBufferRange_no_error(GLenum target
, GLintptr offset
,
3415 GLsizeiptr length
, GLbitfield access
)
3417 GET_CURRENT_CONTEXT(ctx
);
3419 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3420 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3422 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3423 "glMapBufferRange");
3427 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
3430 GET_CURRENT_CONTEXT(ctx
);
3431 struct gl_buffer_object
*bufObj
;
3433 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3434 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3435 "glMapBufferRange(ARB_map_buffer_range not supported)");
3439 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
3443 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3444 "glMapBufferRange"))
3447 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3448 "glMapBufferRange");
3452 _mesa_MapNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3453 GLsizeiptr length
, GLbitfield access
)
3455 GET_CURRENT_CONTEXT(ctx
);
3456 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3458 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3459 "glMapNamedBufferRange");
3463 map_named_buffer_range(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3464 GLbitfield access
, bool dsa_ext
, const char *func
)
3466 GET_CURRENT_CONTEXT(ctx
);
3467 struct gl_buffer_object
*bufObj
= NULL
;
3469 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3470 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3471 "%s(ARB_map_buffer_range not supported)", func
);
3476 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3477 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
, &bufObj
, func
))
3480 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
3485 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
, func
))
3488 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
, func
);
3492 _mesa_MapNamedBufferRangeEXT(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3495 GET_CURRENT_CONTEXT(ctx
);
3497 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3498 "glMapNamedBufferRangeEXT(buffer=0)");
3501 return map_named_buffer_range(buffer
, offset
, length
, access
, true,
3502 "glMapNamedBufferRangeEXT");
3506 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3509 return map_named_buffer_range(buffer
, offset
, length
, access
, false,
3510 "glMapNamedBufferRange");
3514 * Converts GLenum access from MapBuffer and MapNamedBuffer into
3515 * flags for input to map_buffer_range.
3517 * \return true if the type of requested access is permissible.
3520 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
3524 case GL_READ_ONLY_ARB
:
3525 *flags
= GL_MAP_READ_BIT
;
3526 return _mesa_is_desktop_gl(ctx
);
3527 case GL_WRITE_ONLY_ARB
:
3528 *flags
= GL_MAP_WRITE_BIT
;
3530 case GL_READ_WRITE_ARB
:
3531 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
3532 return _mesa_is_desktop_gl(ctx
);
3540 _mesa_MapBuffer_no_error(GLenum target
, GLenum access
)
3542 GET_CURRENT_CONTEXT(ctx
);
3544 GLbitfield accessFlags
;
3545 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3547 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3548 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3550 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3555 _mesa_MapBuffer(GLenum target
, GLenum access
)
3557 GET_CURRENT_CONTEXT(ctx
);
3558 struct gl_buffer_object
*bufObj
;
3559 GLbitfield accessFlags
;
3561 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3562 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
3566 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
3570 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3574 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3579 _mesa_MapNamedBuffer_no_error(GLuint buffer
, GLenum access
)
3581 GET_CURRENT_CONTEXT(ctx
);
3583 GLbitfield accessFlags
;
3584 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3586 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3588 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3589 "glMapNamedBuffer");
3593 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
3595 GET_CURRENT_CONTEXT(ctx
);
3596 struct gl_buffer_object
*bufObj
;
3597 GLbitfield accessFlags
;
3599 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3600 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
3604 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
3608 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3609 "glMapNamedBuffer"))
3612 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3613 "glMapNamedBuffer");
3617 _mesa_MapNamedBufferEXT(GLuint buffer
, GLenum access
)
3619 GET_CURRENT_CONTEXT(ctx
);
3621 GLbitfield accessFlags
;
3623 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3624 "glMapNamedBufferEXT(buffer=0)");
3627 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3628 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBufferEXT(invalid access)");
3632 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3633 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3634 &bufObj
, "glMapNamedBufferEXT"))
3637 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3638 "glMapNamedBufferEXT"))
3641 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3642 "glMapNamedBufferEXT");
3646 flush_mapped_buffer_range(struct gl_context
*ctx
,
3647 struct gl_buffer_object
*bufObj
,
3648 GLintptr offset
, GLsizeiptr length
,
3651 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3652 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3653 "%s(ARB_map_buffer_range not supported)", func
);
3658 _mesa_error(ctx
, GL_INVALID_VALUE
,
3659 "%s(offset %ld < 0)", func
, (long) offset
);
3664 _mesa_error(ctx
, GL_INVALID_VALUE
,
3665 "%s(length %ld < 0)", func
, (long) length
);
3669 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3670 /* buffer is not mapped */
3671 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3672 "%s(buffer is not mapped)", func
);
3676 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
3677 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
3678 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3679 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
3683 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
3684 _mesa_error(ctx
, GL_INVALID_VALUE
,
3685 "%s(offset %ld + length %ld > mapped length %ld)", func
,
3686 (long) offset
, (long) length
,
3687 (long) bufObj
->Mappings
[MAP_USER
].Length
);
3691 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
3693 if (ctx
->Driver
.FlushMappedBufferRange
)
3694 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3699 _mesa_FlushMappedBufferRange_no_error(GLenum target
, GLintptr offset
,
3702 GET_CURRENT_CONTEXT(ctx
);
3703 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3704 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3706 if (ctx
->Driver
.FlushMappedBufferRange
)
3707 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3712 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
3715 GET_CURRENT_CONTEXT(ctx
);
3716 struct gl_buffer_object
*bufObj
;
3718 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
3719 GL_INVALID_OPERATION
);
3723 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3724 "glFlushMappedBufferRange");
3728 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3731 GET_CURRENT_CONTEXT(ctx
);
3732 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3734 if (ctx
->Driver
.FlushMappedBufferRange
)
3735 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3740 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
3743 GET_CURRENT_CONTEXT(ctx
);
3744 struct gl_buffer_object
*bufObj
;
3746 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
3747 "glFlushMappedNamedBufferRange");
3751 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3752 "glFlushMappedNamedBufferRange");
3756 _mesa_FlushMappedNamedBufferRangeEXT(GLuint buffer
, GLintptr offset
,
3759 GET_CURRENT_CONTEXT(ctx
);
3760 struct gl_buffer_object
*bufObj
;
3763 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3764 "glFlushMappedNamedBufferRangeEXT(buffer=0)");
3768 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3769 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3770 &bufObj
, "glFlushMappedNamedBufferRangeEXT"))
3773 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3774 "glFlushMappedNamedBufferRangeEXT");
3778 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
, GLuint index
,
3779 struct gl_buffer_object
*bufObj
,
3780 GLintptr offset
, GLsizeiptr size
)
3787 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3788 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3792 * Bind a region of a buffer object to a uniform block binding point.
3793 * \param index the uniform buffer binding point index
3794 * \param bufObj the buffer object
3795 * \param offset offset to the start of buffer object region
3796 * \param size size of the buffer object region
3799 bind_buffer_range_uniform_buffer_err(struct gl_context
*ctx
, GLuint index
,
3800 struct gl_buffer_object
*bufObj
,
3801 GLintptr offset
, GLsizeiptr size
)
3803 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3804 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3808 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3809 _mesa_error(ctx
, GL_INVALID_VALUE
,
3810 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3811 ctx
->Const
.UniformBufferOffsetAlignment
);
3815 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3819 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3821 struct gl_buffer_object
*bufObj
,
3830 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3831 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3835 * Bind a region of a buffer object to a shader storage block binding point.
3836 * \param index the shader storage buffer binding point index
3837 * \param bufObj the buffer object
3838 * \param offset offset to the start of buffer object region
3839 * \param size size of the buffer object region
3842 bind_buffer_range_shader_storage_buffer_err(struct gl_context
*ctx
,
3844 struct gl_buffer_object
*bufObj
,
3845 GLintptr offset
, GLsizeiptr size
)
3847 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3848 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3852 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3853 _mesa_error(ctx
, GL_INVALID_VALUE
,
3854 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3855 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3859 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3863 bind_buffer_range_atomic_buffer(struct gl_context
*ctx
, GLuint index
,
3864 struct gl_buffer_object
*bufObj
,
3865 GLintptr offset
, GLsizeiptr size
)
3872 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3873 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3877 * Bind a region of a buffer object to an atomic storage block binding point.
3878 * \param index the shader storage buffer binding point index
3879 * \param bufObj the buffer object
3880 * \param offset offset to the start of buffer object region
3881 * \param size size of the buffer object region
3884 bind_buffer_range_atomic_buffer_err(struct gl_context
*ctx
,
3886 struct gl_buffer_object
*bufObj
,
3887 GLintptr offset
, GLsizeiptr size
)
3889 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3890 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3894 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3895 _mesa_error(ctx
, GL_INVALID_VALUE
,
3896 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3897 ATOMIC_COUNTER_SIZE
);
3901 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
3905 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3907 const GLintptr
*offsets
,
3908 const GLsizeiptr
*sizes
)
3910 if (offsets
[index
] < 0) {
3911 /* The ARB_multi_bind spec says:
3913 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3914 * value in <offsets> is less than zero (per binding)."
3916 _mesa_error(ctx
, GL_INVALID_VALUE
,
3917 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3918 index
, (int64_t) offsets
[index
]);
3922 if (sizes
[index
] <= 0) {
3923 /* The ARB_multi_bind spec says:
3925 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3926 * value in <sizes> is less than or equal to zero (per binding)."
3928 _mesa_error(ctx
, GL_INVALID_VALUE
,
3929 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3930 index
, (int64_t) sizes
[index
]);
3938 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3939 GLuint first
, GLsizei count
,
3942 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3943 _mesa_error(ctx
, GL_INVALID_ENUM
,
3944 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3948 /* The ARB_multi_bind_spec says:
3950 * "An INVALID_OPERATION error is generated if <first> + <count> is
3951 * greater than the number of target-specific indexed binding points,
3952 * as described in section 6.7.1."
3954 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3955 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3956 "%s(first=%u + count=%d > the value of "
3957 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3958 caller
, first
, count
,
3959 ctx
->Const
.MaxUniformBufferBindings
);
3967 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3968 GLuint first
, GLsizei count
,
3971 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3972 _mesa_error(ctx
, GL_INVALID_ENUM
,
3973 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3977 /* The ARB_multi_bind_spec says:
3979 * "An INVALID_OPERATION error is generated if <first> + <count> is
3980 * greater than the number of target-specific indexed binding points,
3981 * as described in section 6.7.1."
3983 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3984 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3985 "%s(first=%u + count=%d > the value of "
3986 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3987 caller
, first
, count
,
3988 ctx
->Const
.MaxShaderStorageBufferBindings
);
3996 * Unbind all uniform buffers in the range
3997 * <first> through <first>+<count>-1
4000 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
4002 for (int i
= 0; i
< count
; i
++)
4003 set_buffer_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
4004 NULL
, -1, -1, GL_TRUE
, 0);
4008 * Unbind all shader storage buffers in the range
4009 * <first> through <first>+<count>-1
4012 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
4015 for (int i
= 0; i
< count
; i
++)
4016 set_buffer_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
4017 NULL
, -1, -1, GL_TRUE
, 0);
4021 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
4022 const GLuint
*buffers
,
4024 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
4027 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
4030 /* Assume that at least one binding will be changed */
4031 FLUSH_VERTICES(ctx
, 0);
4032 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
4035 /* The ARB_multi_bind spec says:
4037 * "If <buffers> is NULL, all bindings from <first> through
4038 * <first>+<count>-1 are reset to their unbound (zero) state.
4039 * In this case, the offsets and sizes associated with the
4040 * binding points are set to default values, ignoring
4041 * <offsets> and <sizes>."
4043 unbind_uniform_buffers(ctx
, first
, count
);
4047 /* Note that the error semantics for multi-bind commands differ from
4048 * those of other GL commands.
4050 * The Issues section in the ARB_multi_bind spec says:
4052 * "(11) Typically, OpenGL specifies that if an error is generated by a
4053 * command, that command has no effect. This is somewhat
4054 * unfortunate for multi-bind commands, because it would require a
4055 * first pass to scan the entire list of bound objects for errors
4056 * and then a second pass to actually perform the bindings.
4057 * Should we have different error semantics?
4059 * RESOLVED: Yes. In this specification, when the parameters for
4060 * one of the <count> binding points are invalid, that binding point
4061 * is not updated and an error will be generated. However, other
4062 * binding points in the same command will be updated if their
4063 * parameters are valid and no other error occurs."
4066 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4068 for (int i
= 0; i
< count
; i
++) {
4069 struct gl_buffer_binding
*binding
=
4070 &ctx
->UniformBufferBindings
[first
+ i
];
4071 GLintptr offset
= 0;
4072 GLsizeiptr size
= 0;
4075 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4078 /* The ARB_multi_bind spec says:
4080 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4081 * pair of values in <offsets> and <sizes> does not respectively
4082 * satisfy the constraints described for those parameters for the
4083 * specified target, as described in section 6.7.1 (per binding)."
4085 * Section 6.7.1 refers to table 6.5, which says:
4087 * "┌───────────────────────────────────────────────────────────────┐
4088 * │ Uniform buffer array bindings (see sec. 7.6) │
4089 * ├─────────────────────┬─────────────────────────────────────────┤
4091 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
4092 * │ │ OFFSET_ALIGNMENT │
4094 * │ size restriction │ none │
4095 * └─────────────────────┴─────────────────────────────────────────┘"
4097 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
4098 _mesa_error(ctx
, GL_INVALID_VALUE
,
4099 "glBindBuffersRange(offsets[%u]=%" PRId64
4100 " is misaligned; it must be a multiple of the value of "
4101 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
4102 "target=GL_UNIFORM_BUFFER)",
4103 i
, (int64_t) offsets
[i
],
4104 ctx
->Const
.UniformBufferOffsetAlignment
);
4108 offset
= offsets
[i
];
4112 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4113 binding
, offset
, size
, range
,
4114 USAGE_UNIFORM_BUFFER
);
4117 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4121 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
4122 GLsizei count
, const GLuint
*buffers
,
4124 const GLintptr
*offsets
,
4125 const GLsizeiptr
*sizes
,
4128 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
4131 /* Assume that at least one binding will be changed */
4132 FLUSH_VERTICES(ctx
, 0);
4133 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
4136 /* The ARB_multi_bind spec says:
4138 * "If <buffers> is NULL, all bindings from <first> through
4139 * <first>+<count>-1 are reset to their unbound (zero) state.
4140 * In this case, the offsets and sizes associated with the
4141 * binding points are set to default values, ignoring
4142 * <offsets> and <sizes>."
4144 unbind_shader_storage_buffers(ctx
, first
, count
);
4148 /* Note that the error semantics for multi-bind commands differ from
4149 * those of other GL commands.
4151 * The Issues section in the ARB_multi_bind spec says:
4153 * "(11) Typically, OpenGL specifies that if an error is generated by a
4154 * command, that command has no effect. This is somewhat
4155 * unfortunate for multi-bind commands, because it would require a
4156 * first pass to scan the entire list of bound objects for errors
4157 * and then a second pass to actually perform the bindings.
4158 * Should we have different error semantics?
4160 * RESOLVED: Yes. In this specification, when the parameters for
4161 * one of the <count> binding points are invalid, that binding point
4162 * is not updated and an error will be generated. However, other
4163 * binding points in the same command will be updated if their
4164 * parameters are valid and no other error occurs."
4167 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4169 for (int i
= 0; i
< count
; i
++) {
4170 struct gl_buffer_binding
*binding
=
4171 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
4172 GLintptr offset
= 0;
4173 GLsizeiptr size
= 0;
4176 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4179 /* The ARB_multi_bind spec says:
4181 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4182 * pair of values in <offsets> and <sizes> does not respectively
4183 * satisfy the constraints described for those parameters for the
4184 * specified target, as described in section 6.7.1 (per binding)."
4186 * Section 6.7.1 refers to table 6.5, which says:
4188 * "┌───────────────────────────────────────────────────────────────┐
4189 * │ Shader storage buffer array bindings (see sec. 7.8) │
4190 * ├─────────────────────┬─────────────────────────────────────────┤
4192 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
4193 * │ │ BUFFER_OFFSET_ALIGNMENT │
4195 * │ size restriction │ none │
4196 * └─────────────────────┴─────────────────────────────────────────┘"
4198 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
4199 _mesa_error(ctx
, GL_INVALID_VALUE
,
4200 "glBindBuffersRange(offsets[%u]=%" PRId64
4201 " is misaligned; it must be a multiple of the value of "
4202 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
4203 "target=GL_SHADER_STORAGE_BUFFER)",
4204 i
, (int64_t) offsets
[i
],
4205 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
4209 offset
= offsets
[i
];
4213 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4214 binding
, offset
, size
, range
,
4215 USAGE_SHADER_STORAGE_BUFFER
);
4218 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4222 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
4223 struct gl_transform_feedback_object
*tfObj
,
4224 GLuint first
, GLsizei count
, const char *caller
)
4226 if (!ctx
->Extensions
.EXT_transform_feedback
) {
4227 _mesa_error(ctx
, GL_INVALID_ENUM
,
4228 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
4232 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
4234 * "An INVALID_OPERATION error is generated :
4237 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
4238 * FEEDBACK_BUFFER and transform feedback is currently active."
4240 * We assume that this is also meant to apply to BindBuffersRange
4241 * and BindBuffersBase.
4243 if (tfObj
->Active
) {
4244 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4245 "%s(Changing transform feedback buffers while "
4246 "transform feedback is active)", caller
);
4250 /* The ARB_multi_bind_spec says:
4252 * "An INVALID_OPERATION error is generated if <first> + <count> is
4253 * greater than the number of target-specific indexed binding points,
4254 * as described in section 6.7.1."
4256 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
4257 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4258 "%s(first=%u + count=%d > the value of "
4259 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
4260 caller
, first
, count
,
4261 ctx
->Const
.MaxTransformFeedbackBuffers
);
4269 * Unbind all transform feedback buffers in the range
4270 * <first> through <first>+<count>-1
4273 unbind_xfb_buffers(struct gl_context
*ctx
,
4274 struct gl_transform_feedback_object
*tfObj
,
4275 GLuint first
, GLsizei count
)
4277 for (int i
= 0; i
< count
; i
++)
4278 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
4283 bind_xfb_buffers(struct gl_context
*ctx
,
4284 GLuint first
, GLsizei count
,
4285 const GLuint
*buffers
,
4287 const GLintptr
*offsets
,
4288 const GLsizeiptr
*sizes
,
4291 struct gl_transform_feedback_object
*tfObj
=
4292 ctx
->TransformFeedback
.CurrentObject
;
4294 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
4297 /* Assume that at least one binding will be changed */
4298 FLUSH_VERTICES(ctx
, 0);
4299 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
4302 /* The ARB_multi_bind spec says:
4304 * "If <buffers> is NULL, all bindings from <first> through
4305 * <first>+<count>-1 are reset to their unbound (zero) state.
4306 * In this case, the offsets and sizes associated with the
4307 * binding points are set to default values, ignoring
4308 * <offsets> and <sizes>."
4310 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
4314 /* Note that the error semantics for multi-bind commands differ from
4315 * those of other GL commands.
4317 * The Issues section in the ARB_multi_bind spec says:
4319 * "(11) Typically, OpenGL specifies that if an error is generated by a
4320 * command, that command has no effect. This is somewhat
4321 * unfortunate for multi-bind commands, because it would require a
4322 * first pass to scan the entire list of bound objects for errors
4323 * and then a second pass to actually perform the bindings.
4324 * Should we have different error semantics?
4326 * RESOLVED: Yes. In this specification, when the parameters for
4327 * one of the <count> binding points are invalid, that binding point
4328 * is not updated and an error will be generated. However, other
4329 * binding points in the same command will be updated if their
4330 * parameters are valid and no other error occurs."
4333 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4335 for (int i
= 0; i
< count
; i
++) {
4336 const GLuint index
= first
+ i
;
4337 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
4338 struct gl_buffer_object
*bufObj
;
4339 GLintptr offset
= 0;
4340 GLsizeiptr size
= 0;
4343 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4346 /* The ARB_multi_bind spec says:
4348 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4349 * pair of values in <offsets> and <sizes> does not respectively
4350 * satisfy the constraints described for those parameters for the
4351 * specified target, as described in section 6.7.1 (per binding)."
4353 * Section 6.7.1 refers to table 6.5, which says:
4355 * "┌───────────────────────────────────────────────────────────────┐
4356 * │ Transform feedback array bindings (see sec. 13.2.2) │
4357 * ├───────────────────────┬───────────────────────────────────────┤
4359 * │ offset restriction │ multiple of 4 │
4361 * │ size restriction │ multiple of 4 │
4362 * └───────────────────────┴───────────────────────────────────────┘"
4364 if (offsets
[i
] & 0x3) {
4365 _mesa_error(ctx
, GL_INVALID_VALUE
,
4366 "glBindBuffersRange(offsets[%u]=%" PRId64
4367 " is misaligned; it must be a multiple of 4 when "
4368 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4369 i
, (int64_t) offsets
[i
]);
4373 if (sizes
[i
] & 0x3) {
4374 _mesa_error(ctx
, GL_INVALID_VALUE
,
4375 "glBindBuffersRange(sizes[%u]=%" PRId64
4376 " is misaligned; it must be a multiple of 4 when "
4377 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4378 i
, (int64_t) sizes
[i
]);
4382 offset
= offsets
[i
];
4386 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
4387 bufObj
= boundBufObj
;
4390 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
,
4396 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
4400 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4404 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
4405 GLuint first
, GLsizei count
,
4408 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
4409 _mesa_error(ctx
, GL_INVALID_ENUM
,
4410 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
4414 /* The ARB_multi_bind_spec says:
4416 * "An INVALID_OPERATION error is generated if <first> + <count> is
4417 * greater than the number of target-specific indexed binding points,
4418 * as described in section 6.7.1."
4420 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
4421 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4422 "%s(first=%u + count=%d > the value of "
4423 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4424 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
4432 * Unbind all atomic counter buffers in the range
4433 * <first> through <first>+<count>-1
4436 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
4438 for (int i
= 0; i
< count
; i
++)
4439 set_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
4440 NULL
, -1, -1, GL_TRUE
, 0);
4444 bind_atomic_buffers(struct gl_context
*ctx
,
4447 const GLuint
*buffers
,
4449 const GLintptr
*offsets
,
4450 const GLsizeiptr
*sizes
,
4453 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
4456 /* Assume that at least one binding will be changed */
4457 FLUSH_VERTICES(ctx
, 0);
4458 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
4461 /* The ARB_multi_bind spec says:
4463 * "If <buffers> is NULL, all bindings from <first> through
4464 * <first>+<count>-1 are reset to their unbound (zero) state.
4465 * In this case, the offsets and sizes associated with the
4466 * binding points are set to default values, ignoring
4467 * <offsets> and <sizes>."
4469 unbind_atomic_buffers(ctx
, first
, count
);
4473 /* Note that the error semantics for multi-bind commands differ from
4474 * those of other GL commands.
4476 * The Issues section in the ARB_multi_bind spec says:
4478 * "(11) Typically, OpenGL specifies that if an error is generated by a
4479 * command, that command has no effect. This is somewhat
4480 * unfortunate for multi-bind commands, because it would require a
4481 * first pass to scan the entire list of bound objects for errors
4482 * and then a second pass to actually perform the bindings.
4483 * Should we have different error semantics?
4485 * RESOLVED: Yes. In this specification, when the parameters for
4486 * one of the <count> binding points are invalid, that binding point
4487 * is not updated and an error will be generated. However, other
4488 * binding points in the same command will be updated if their
4489 * parameters are valid and no other error occurs."
4492 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4494 for (int i
= 0; i
< count
; i
++) {
4495 struct gl_buffer_binding
*binding
=
4496 &ctx
->AtomicBufferBindings
[first
+ i
];
4497 GLintptr offset
= 0;
4498 GLsizeiptr size
= 0;
4501 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4504 /* The ARB_multi_bind spec says:
4506 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4507 * pair of values in <offsets> and <sizes> does not respectively
4508 * satisfy the constraints described for those parameters for the
4509 * specified target, as described in section 6.7.1 (per binding)."
4511 * Section 6.7.1 refers to table 6.5, which says:
4513 * "┌───────────────────────────────────────────────────────────────┐
4514 * │ Atomic counter array bindings (see sec. 7.7.2) │
4515 * ├───────────────────────┬───────────────────────────────────────┤
4517 * │ offset restriction │ multiple of 4 │
4519 * │ size restriction │ none │
4520 * └───────────────────────┴───────────────────────────────────────┘"
4522 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
4523 _mesa_error(ctx
, GL_INVALID_VALUE
,
4524 "glBindBuffersRange(offsets[%u]=%" PRId64
4525 " is misaligned; it must be a multiple of %d when "
4526 "target=GL_ATOMIC_COUNTER_BUFFER)",
4527 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
4531 offset
= offsets
[i
];
4535 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4536 binding
, offset
, size
, range
,
4537 USAGE_ATOMIC_COUNTER_BUFFER
);
4540 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4543 static ALWAYS_INLINE
void
4544 bind_buffer_range(GLenum target
, GLuint index
, GLuint buffer
, GLintptr offset
,
4545 GLsizeiptr size
, bool no_error
)
4547 GET_CURRENT_CONTEXT(ctx
);
4548 struct gl_buffer_object
*bufObj
;
4550 if (MESA_VERBOSE
& VERBOSE_API
) {
4551 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4552 _mesa_enum_to_string(target
), index
, buffer
,
4553 (unsigned long) offset
, (unsigned long) size
);
4559 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4560 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4561 &bufObj
, "glBindBufferRange"))
4564 if (!no_error
&& !bufObj
) {
4565 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4566 "glBindBufferRange(invalid buffer=%u)", buffer
);
4573 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4574 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4575 index
, bufObj
, offset
, size
);
4577 case GL_UNIFORM_BUFFER
:
4578 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
4580 case GL_SHADER_STORAGE_BUFFER
:
4581 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
,
4584 case GL_ATOMIC_COUNTER_BUFFER
:
4585 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
4588 unreachable("invalid BindBufferRange target with KHR_no_error");
4593 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
4600 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4601 if (!_mesa_validate_buffer_range_xfb(ctx
,
4602 ctx
->TransformFeedback
.CurrentObject
,
4603 index
, bufObj
, offset
, size
,
4607 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4608 index
, bufObj
, offset
, size
);
4610 case GL_UNIFORM_BUFFER
:
4611 bind_buffer_range_uniform_buffer_err(ctx
, index
, bufObj
, offset
,
4614 case GL_SHADER_STORAGE_BUFFER
:
4615 bind_buffer_range_shader_storage_buffer_err(ctx
, index
, bufObj
,
4618 case GL_ATOMIC_COUNTER_BUFFER
:
4619 bind_buffer_range_atomic_buffer_err(ctx
, index
, bufObj
,
4623 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
4630 _mesa_BindBufferRange_no_error(GLenum target
, GLuint index
, GLuint buffer
,
4631 GLintptr offset
, GLsizeiptr size
)
4633 bind_buffer_range(target
, index
, buffer
, offset
, size
, true);
4637 _mesa_BindBufferRange(GLenum target
, GLuint index
,
4638 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
4640 bind_buffer_range(target
, index
, buffer
, offset
, size
, false);
4644 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
4646 GET_CURRENT_CONTEXT(ctx
);
4647 struct gl_buffer_object
*bufObj
;
4649 if (MESA_VERBOSE
& VERBOSE_API
) {
4650 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
4651 _mesa_enum_to_string(target
), index
, buffer
);
4657 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4658 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4659 &bufObj
, "glBindBufferBase"))
4663 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4664 "glBindBufferBase(invalid buffer=%u)", buffer
);
4669 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4670 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4672 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4673 * zero and size equal to the size of buffer."
4675 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4677 * "If the parameter (starting offset or size) was not specified when the
4678 * buffer object was bound, zero is returned."
4680 * What happens if the size of the buffer changes? Does the size of the
4681 * buffer at the moment glBindBufferBase was called still play a role, like
4682 * the first quote would imply, or is the size meaningless in the
4683 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4684 * core spec page 45 says:
4686 * "It is equivalent to calling BindBufferRange with offset zero, while
4687 * size is determined by the size of the bound buffer at the time the
4690 * My interpretation is that the GL 4.1 spec was a clarification of the
4691 * behavior, not a change. In particular, this choice will only make
4692 * rendering work in cases where it would have had undefined results.
4696 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4697 _mesa_bind_buffer_base_transform_feedback(ctx
,
4698 ctx
->TransformFeedback
.CurrentObject
,
4699 index
, bufObj
, false);
4701 case GL_UNIFORM_BUFFER
:
4702 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4704 case GL_SHADER_STORAGE_BUFFER
:
4705 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4707 case GL_ATOMIC_COUNTER_BUFFER
:
4708 bind_buffer_base_atomic_buffer(ctx
, index
, bufObj
);
4711 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4717 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4718 const GLuint
*buffers
,
4719 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4721 GET_CURRENT_CONTEXT(ctx
);
4723 if (MESA_VERBOSE
& VERBOSE_API
) {
4724 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4725 _mesa_enum_to_string(target
), first
, count
,
4726 buffers
, offsets
, sizes
);
4730 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4731 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4732 "glBindBuffersRange");
4734 case GL_UNIFORM_BUFFER
:
4735 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4736 "glBindBuffersRange");
4738 case GL_SHADER_STORAGE_BUFFER
:
4739 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4740 "glBindBuffersRange");
4742 case GL_ATOMIC_COUNTER_BUFFER
:
4743 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4744 "glBindBuffersRange");
4747 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4748 _mesa_enum_to_string(target
));
4754 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4755 const GLuint
*buffers
)
4757 GET_CURRENT_CONTEXT(ctx
);
4759 if (MESA_VERBOSE
& VERBOSE_API
) {
4760 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4761 _mesa_enum_to_string(target
), first
, count
, buffers
);
4765 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4766 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4767 "glBindBuffersBase");
4769 case GL_UNIFORM_BUFFER
:
4770 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4771 "glBindBuffersBase");
4773 case GL_SHADER_STORAGE_BUFFER
:
4774 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4775 "glBindBuffersBase");
4777 case GL_ATOMIC_COUNTER_BUFFER
:
4778 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4779 "glBindBuffersBase");
4782 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4783 _mesa_enum_to_string(target
));
4788 static ALWAYS_INLINE
void
4789 invalidate_buffer_subdata(struct gl_context
*ctx
,
4790 struct gl_buffer_object
*bufObj
, GLintptr offset
,
4793 if (ctx
->Driver
.InvalidateBufferSubData
)
4794 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4798 _mesa_InvalidateBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
4801 GET_CURRENT_CONTEXT(ctx
);
4803 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4804 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4808 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4811 GET_CURRENT_CONTEXT(ctx
);
4812 struct gl_buffer_object
*bufObj
;
4813 const GLintptr end
= offset
+ length
;
4815 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4816 * Profile) spec says:
4818 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4819 * name of an existing buffer object."
4821 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4822 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4823 _mesa_error(ctx
, GL_INVALID_VALUE
,
4824 "glInvalidateBufferSubData(name = %u) invalid object",
4829 /* The GL_ARB_invalidate_subdata spec says:
4831 * "An INVALID_VALUE error is generated if <offset> or <length> is
4832 * negative, or if <offset> + <length> is greater than the value of
4835 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
4836 _mesa_error(ctx
, GL_INVALID_VALUE
,
4837 "glInvalidateBufferSubData(invalid offset or length)");
4841 /* The OpenGL 4.4 (Core Profile) spec says:
4843 * "An INVALID_OPERATION error is generated if buffer is currently
4844 * mapped by MapBuffer or if the invalidate range intersects the range
4845 * currently mapped by MapBufferRange, unless it was mapped
4846 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4848 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4849 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4850 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4851 "glInvalidateBufferSubData(intersection with mapped "
4856 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4860 _mesa_InvalidateBufferData_no_error(GLuint buffer
)
4862 GET_CURRENT_CONTEXT(ctx
);
4864 struct gl_buffer_object
*bufObj
=_mesa_lookup_bufferobj(ctx
, buffer
);
4865 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4869 _mesa_InvalidateBufferData(GLuint buffer
)
4871 GET_CURRENT_CONTEXT(ctx
);
4872 struct gl_buffer_object
*bufObj
;
4874 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4875 * Profile) spec says:
4877 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4878 * name of an existing buffer object."
4880 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4881 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4882 _mesa_error(ctx
, GL_INVALID_VALUE
,
4883 "glInvalidateBufferData(name = %u) invalid object",
4888 /* The OpenGL 4.4 (Core Profile) spec says:
4890 * "An INVALID_OPERATION error is generated if buffer is currently
4891 * mapped by MapBuffer or if the invalidate range intersects the range
4892 * currently mapped by MapBufferRange, unless it was mapped
4893 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4895 if (_mesa_check_disallowed_mapping(bufObj
)) {
4896 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4897 "glInvalidateBufferData(intersection with mapped "
4902 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4906 buffer_page_commitment(struct gl_context
*ctx
,
4907 struct gl_buffer_object
*bufferObj
,
4908 GLintptr offset
, GLsizeiptr size
,
4909 GLboolean commit
, const char *func
)
4911 if (!(bufferObj
->StorageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)) {
4912 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(not a sparse buffer object)",
4917 if (size
< 0 || size
> bufferObj
->Size
||
4918 offset
< 0 || offset
> bufferObj
->Size
- size
) {
4919 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(out of bounds)",
4924 /* The GL_ARB_sparse_buffer extension specification says:
4926 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4927 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4928 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4929 * not extend to the end of the buffer's data store."
4931 if (offset
% ctx
->Const
.SparseBufferPageSize
!= 0) {
4932 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset not aligned to page size)",
4937 if (size
% ctx
->Const
.SparseBufferPageSize
!= 0 &&
4938 offset
+ size
!= bufferObj
->Size
) {
4939 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size not aligned to page size)",
4944 ctx
->Driver
.BufferPageCommitment(ctx
, bufferObj
, offset
, size
, commit
);
4948 _mesa_BufferPageCommitmentARB(GLenum target
, GLintptr offset
, GLsizeiptr size
,
4951 GET_CURRENT_CONTEXT(ctx
);
4952 struct gl_buffer_object
*bufferObj
;
4954 bufferObj
= get_buffer(ctx
, "glBufferPageCommitmentARB", target
,
4959 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4960 "glBufferPageCommitmentARB");
4964 _mesa_NamedBufferPageCommitmentARB(GLuint buffer
, GLintptr offset
,
4965 GLsizeiptr size
, GLboolean commit
)
4967 GET_CURRENT_CONTEXT(ctx
);
4968 struct gl_buffer_object
*bufferObj
;
4970 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4971 if (!bufferObj
|| bufferObj
== &DummyBufferObject
) {
4972 /* Note: the extension spec is not clear about the excpected error value. */
4973 _mesa_error(ctx
, GL_INVALID_VALUE
,
4974 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4979 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4980 "glNamedBufferPageCommitmentARB");
4984 _mesa_NamedBufferPageCommitmentEXT(GLuint buffer
, GLintptr offset
,
4985 GLsizeiptr size
, GLboolean commit
)
4987 GET_CURRENT_CONTEXT(ctx
);
4988 struct gl_buffer_object
*bufferObj
;
4990 /* Use NamedBuffer* functions logic from EXT_direct_state_access */
4992 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4993 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
, &bufferObj
,
4994 "glNamedBufferPageCommitmentEXT"))
4997 /* GL_EXT_direct_state_access says about NamedBuffer* functions:
4999 * There is no buffer corresponding to the name zero, these commands
5000 * generate the INVALID_OPERATION error if the buffer parameter is
5003 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5004 "glNamedBufferPageCommitmentEXT(buffer = 0)");
5007 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
5008 "glNamedBufferPageCommitmentEXT");