2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
29 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
30 * \author Brian Paul, Ian Romanick
34 #include <inttypes.h> /* for PRId64 macro */
35 #include "util/debug.h"
39 #include "util/imports.h"
41 #include "bufferobj.h"
42 #include "externalobjects.h"
45 #include "glformats.h"
47 #include "transformfeedback.h"
49 #include "util/u_atomic.h"
54 /*#define BOUNDS_CHECK*/
58 * We count the number of buffer modification calls to check for
59 * inefficient buffer use. This is the number of such calls before we
62 #define BUFFER_WARNING_CALL_COUNT 4
66 * Helper to warn of possible performance issues, such as frequently
67 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
71 buffer_usage_warning(struct gl_context
*ctx
, GLuint
*id
, const char *fmt
, ...)
76 _mesa_gl_vdebugf(ctx
, id
,
77 MESA_DEBUG_SOURCE_API
,
78 MESA_DEBUG_TYPE_PERFORMANCE
,
79 MESA_DEBUG_SEVERITY_MEDIUM
,
84 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
86 static GLuint id = 0; \
87 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
92 * Used as a placeholder for buffer objects between glGenBuffers() and
93 * glBindBuffer() so that glIsBuffer() can work correctly.
95 static struct gl_buffer_object DummyBufferObject
;
99 * Return pointer to address of a buffer object target.
100 * \param ctx the GL context
101 * \param target the buffer object target to be retrieved.
102 * \return pointer to pointer to the buffer object bound to \c target in the
103 * specified context or \c NULL if \c target is invalid.
105 static inline struct gl_buffer_object
**
106 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
108 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
110 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
111 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
115 case GL_ARRAY_BUFFER_ARB
:
116 if (ctx
->Array
.ArrayBufferObj
)
117 ctx
->Array
.ArrayBufferObj
->UsageHistory
|= USAGE_ARRAY_BUFFER
;
118 return &ctx
->Array
.ArrayBufferObj
;
119 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
120 if (ctx
->Array
.VAO
->IndexBufferObj
)
121 ctx
->Array
.VAO
->IndexBufferObj
->UsageHistory
122 |= USAGE_ELEMENT_ARRAY_BUFFER
;
123 return &ctx
->Array
.VAO
->IndexBufferObj
;
124 case GL_PIXEL_PACK_BUFFER_EXT
:
125 return &ctx
->Pack
.BufferObj
;
126 case GL_PIXEL_UNPACK_BUFFER_EXT
:
127 return &ctx
->Unpack
.BufferObj
;
128 case GL_COPY_READ_BUFFER
:
129 return &ctx
->CopyReadBuffer
;
130 case GL_COPY_WRITE_BUFFER
:
131 return &ctx
->CopyWriteBuffer
;
132 case GL_QUERY_BUFFER
:
133 if (_mesa_has_ARB_query_buffer_object(ctx
))
134 return &ctx
->QueryBuffer
;
136 case GL_DRAW_INDIRECT_BUFFER
:
137 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_draw_indirect
) ||
138 _mesa_is_gles31(ctx
)) {
139 return &ctx
->DrawIndirectBuffer
;
142 case GL_PARAMETER_BUFFER_ARB
:
143 if (_mesa_has_ARB_indirect_parameters(ctx
)) {
144 return &ctx
->ParameterBuffer
;
147 case GL_DISPATCH_INDIRECT_BUFFER
:
148 if (_mesa_has_compute_shaders(ctx
)) {
149 return &ctx
->DispatchIndirectBuffer
;
152 case GL_TRANSFORM_FEEDBACK_BUFFER
:
153 if (ctx
->Extensions
.EXT_transform_feedback
) {
154 return &ctx
->TransformFeedback
.CurrentBuffer
;
157 case GL_TEXTURE_BUFFER
:
158 if (_mesa_has_ARB_texture_buffer_object(ctx
) ||
159 _mesa_has_OES_texture_buffer(ctx
)) {
160 return &ctx
->Texture
.BufferObject
;
163 case GL_UNIFORM_BUFFER
:
164 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
165 return &ctx
->UniformBuffer
;
168 case GL_SHADER_STORAGE_BUFFER
:
169 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
170 return &ctx
->ShaderStorageBuffer
;
173 case GL_ATOMIC_COUNTER_BUFFER
:
174 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
175 return &ctx
->AtomicBuffer
;
178 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
179 if (ctx
->Extensions
.AMD_pinned_memory
) {
180 return &ctx
->ExternalVirtualMemoryBuffer
;
191 * Get the buffer object bound to the specified target in a GL context.
192 * \param ctx the GL context
193 * \param target the buffer object target to be retrieved.
194 * \param error the GL error to record if target is illegal.
195 * \return pointer to the buffer object bound to \c target in the
196 * specified context or \c NULL if \c target is invalid.
198 static inline struct gl_buffer_object
*
199 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
202 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
205 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
209 if (!_mesa_is_bufferobj(*bufObj
)) {
210 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
219 * Convert a GLbitfield describing the mapped buffer access flags
220 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
223 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
225 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
226 if ((access
& rwFlags
) == rwFlags
)
227 return GL_READ_WRITE
;
228 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
230 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
231 return GL_WRITE_ONLY
;
233 /* Otherwise, AccessFlags is zero (the default state).
235 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
237 * Name Type Initial Value Legal Values
239 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
242 * However, table 6.8 in the GL_OES_mapbuffer extension says:
244 * Get Value Type Get Command Value Description
245 * --------- ---- ----------- ----- -----------
246 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
248 * The difference is because GL_OES_mapbuffer only supports mapping buffers
253 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
258 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
260 * The regions do not overlap if and only if the end of the given
261 * region is before the mapped region or the start of the given region
262 * is after the mapped region.
264 * \param obj Buffer object target on which to operate.
265 * \param offset Offset of the first byte of the subdata range.
266 * \param size Size, in bytes, of the subdata range.
267 * \return true if ranges overlap, false otherwise
271 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
272 GLintptr offset
, GLsizeiptr size
)
274 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
275 const GLintptr end
= offset
+ size
;
276 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
277 obj
->Mappings
[MAP_USER
].Length
;
279 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
288 * Tests the subdata range parameters and sets the GL error code for
289 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
290 * \c glClearBufferSubData.
292 * \param ctx GL context.
293 * \param bufObj The buffer object.
294 * \param offset Offset of the first byte of the subdata range.
295 * \param size Size, in bytes, of the subdata range.
296 * \param mappedRange If true, checks if an overlapping range is mapped.
297 * If false, checks if buffer is mapped.
298 * \param caller Name of calling function for recording errors.
299 * \return false if error, true otherwise
301 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
304 buffer_object_subdata_range_good(struct gl_context
*ctx
,
305 const struct gl_buffer_object
*bufObj
,
306 GLintptr offset
, GLsizeiptr size
,
307 bool mappedRange
, const char *caller
)
310 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
315 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
319 if (offset
+ size
> bufObj
->Size
) {
320 _mesa_error(ctx
, GL_INVALID_VALUE
,
321 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
322 (unsigned long) offset
,
323 (unsigned long) size
,
324 (unsigned long) bufObj
->Size
);
328 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
332 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
333 _mesa_error(ctx
, GL_INVALID_OPERATION
,
334 "%s(range is mapped without persistent bit)",
340 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
341 _mesa_error(ctx
, GL_INVALID_OPERATION
,
342 "%s(buffer is mapped without persistent bit)",
353 * Test the format and type parameters and set the GL error code for
354 * \c glClearBufferData, \c glClearNamedBufferData, \c glClearBufferSubData
355 * and \c glClearNamedBufferSubData.
357 * \param ctx GL context.
358 * \param internalformat Format to which the data is to be converted.
359 * \param format Format of the supplied data.
360 * \param type Type of the supplied data.
361 * \param caller Name of calling function for recording errors.
362 * \return If internalformat, format and type are legal the mesa_format
363 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
365 * \sa glClearBufferData, glClearNamedBufferData, glClearBufferSubData and
366 * glClearNamedBufferSubData.
369 validate_clear_buffer_format(struct gl_context
*ctx
,
370 GLenum internalformat
,
371 GLenum format
, GLenum type
,
374 mesa_format mesaFormat
;
375 GLenum errorFormatType
;
377 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
378 if (mesaFormat
== MESA_FORMAT_NONE
) {
379 _mesa_error(ctx
, GL_INVALID_ENUM
,
380 "%s(invalid internalformat)", caller
);
381 return MESA_FORMAT_NONE
;
384 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
385 * EXT_texture_integer there is no conversion between integer and
386 * non-integer formats
388 if (_mesa_is_enum_format_signed_int(format
) !=
389 _mesa_is_format_integer_color(mesaFormat
)) {
390 _mesa_error(ctx
, GL_INVALID_OPERATION
,
391 "%s(integer vs non-integer)", caller
);
392 return MESA_FORMAT_NONE
;
395 if (!_mesa_is_color_format(format
)) {
396 _mesa_error(ctx
, GL_INVALID_VALUE
,
397 "%s(format is not a color format)", caller
);
398 return MESA_FORMAT_NONE
;
401 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
402 if (errorFormatType
!= GL_NO_ERROR
) {
403 _mesa_error(ctx
, GL_INVALID_VALUE
,
404 "%s(invalid format or type)", caller
);
405 return MESA_FORMAT_NONE
;
413 * Convert user-specified clear value to the specified internal format.
415 * \param ctx GL context.
416 * \param internalformat Format to which the data is converted.
417 * \param clearValue Points to the converted clear value.
418 * \param format Format of the supplied data.
419 * \param type Type of the supplied data.
420 * \param data Data which is to be converted to internalformat.
421 * \param caller Name of calling function for recording errors.
422 * \return true if data could be converted, false otherwise.
424 * \sa glClearBufferData, glClearBufferSubData
427 convert_clear_buffer_data(struct gl_context
*ctx
,
428 mesa_format internalformat
,
429 GLubyte
*clearValue
, GLenum format
, GLenum type
,
430 const GLvoid
*data
, const char *caller
)
432 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
434 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
435 0, &clearValue
, 1, 1, 1,
436 format
, type
, data
, &ctx
->Unpack
)) {
440 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
447 * Allocate and initialize a new buffer object.
449 * Default callback for the \c dd_function_table::NewBufferObject() hook.
451 static struct gl_buffer_object
*
452 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
454 struct gl_buffer_object
*obj
= MALLOC_STRUCT(gl_buffer_object
);
458 _mesa_initialize_buffer_object(ctx
, obj
, name
);
464 * Delete a buffer object.
466 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
469 _mesa_delete_buffer_object(struct gl_context
*ctx
,
470 struct gl_buffer_object
*bufObj
)
474 vbo_delete_minmax_cache(bufObj
);
475 _mesa_align_free(bufObj
->Data
);
477 /* assign strange values here to help w/ debugging */
478 bufObj
->RefCount
= -1000;
481 simple_mtx_destroy(&bufObj
->MinMaxCacheMutex
);
489 * Set ptr to bufObj w/ reference counting.
490 * This is normally only called from the _mesa_reference_buffer_object() macro
491 * when there's a real pointer change.
494 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
495 struct gl_buffer_object
**ptr
,
496 struct gl_buffer_object
*bufObj
)
499 /* Unreference the old buffer */
500 struct gl_buffer_object
*oldObj
= *ptr
;
502 if (p_atomic_dec_zero(&oldObj
->RefCount
)) {
503 assert(ctx
->Driver
.DeleteBuffer
);
504 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
512 /* reference new buffer */
513 p_atomic_inc(&bufObj
->RefCount
);
520 * Get the value of MESA_NO_MINMAX_CACHE.
523 get_no_minmax_cache()
525 static bool read
= false;
526 static bool disable
= false;
529 disable
= env_var_as_boolean("MESA_NO_MINMAX_CACHE", false);
538 * Initialize a buffer object to default values.
541 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
542 struct gl_buffer_object
*obj
,
545 memset(obj
, 0, sizeof(struct gl_buffer_object
));
548 obj
->Usage
= GL_STATIC_DRAW_ARB
;
550 simple_mtx_init(&obj
->MinMaxCacheMutex
, mtx_plain
);
551 if (get_no_minmax_cache())
552 obj
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
558 * Callback called from _mesa_HashWalk()
561 count_buffer_size(GLuint key
, void *data
, void *userData
)
563 const struct gl_buffer_object
*bufObj
=
564 (const struct gl_buffer_object
*) data
;
565 GLuint
*total
= (GLuint
*) userData
;
568 *total
= *total
+ bufObj
->Size
;
573 * Compute total size (in bytes) of all buffer objects for the given context.
574 * For debugging purposes.
577 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
581 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
588 * Allocate space for and store data in a buffer object. Any data that was
589 * previously stored in the buffer object is lost. If \c data is \c NULL,
590 * memory will be allocated, but no copy will occur.
592 * This is the default callback for \c dd_function_table::BufferData()
593 * Note that all GL error checking will have been done already.
595 * \param ctx GL context.
596 * \param target Buffer object target on which to operate.
597 * \param size Size, in bytes, of the new data store.
598 * \param data Pointer to the data to store in the buffer object. This
599 * pointer may be \c NULL.
600 * \param usage Hints about how the data will be used.
601 * \param bufObj Object to be used.
603 * \return GL_TRUE for success, GL_FALSE for failure
604 * \sa glBufferDataARB, dd_function_table::BufferData.
607 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
608 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
609 struct gl_buffer_object
*bufObj
)
615 _mesa_align_free( bufObj
->Data
);
617 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
619 bufObj
->Data
= (GLubyte
*) new_data
;
621 bufObj
->Usage
= usage
;
622 bufObj
->StorageFlags
= storageFlags
;
625 memcpy( bufObj
->Data
, data
, size
);
637 * Replace data in a subrange of buffer object. If the data range
638 * specified by \c size + \c offset extends beyond the end of the buffer or
639 * if \c data is \c NULL, no copy is performed.
641 * This is the default callback for \c dd_function_table::BufferSubData()
642 * Note that all GL error checking will have been done already.
644 * \param ctx GL context.
645 * \param offset Offset of the first byte to be modified.
646 * \param size Size, in bytes, of the data range.
647 * \param data Pointer to the data to store in the buffer object.
648 * \param bufObj Object to be used.
650 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
653 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptrARB offset
,
654 GLsizeiptrARB size
, const GLvoid
*data
,
655 struct gl_buffer_object
*bufObj
)
659 /* this should have been caught in _mesa_BufferSubData() */
660 assert(size
+ offset
<= bufObj
->Size
);
663 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
669 * Retrieve data from a subrange of buffer object. If the data range
670 * specified by \c size + \c offset extends beyond the end of the buffer or
671 * if \c data is \c NULL, no copy is performed.
673 * This is the default callback for \c dd_function_table::GetBufferSubData()
674 * Note that all GL error checking will have been done already.
676 * \param ctx GL context.
677 * \param target Buffer object target on which to operate.
678 * \param offset Offset of the first byte to be fetched.
679 * \param size Size, in bytes, of the data range.
680 * \param data Destination for data
681 * \param bufObj Object to be used.
683 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
686 buffer_get_subdata(struct gl_context
*ctx
, GLintptrARB offset
,
687 GLsizeiptrARB size
, GLvoid
*data
,
688 struct gl_buffer_object
*bufObj
)
692 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
693 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
699 * Clear a subrange of the buffer object with copies of the supplied data.
700 * If data is NULL the buffer is filled with zeros.
702 * This is the default callback for \c dd_function_table::ClearBufferSubData()
703 * Note that all GL error checking will have been done already.
705 * \param ctx GL context.
706 * \param offset Offset of the first byte to be cleared.
707 * \param size Size, in bytes, of the to be cleared range.
708 * \param clearValue Source of the data.
709 * \param clearValueSize Size, in bytes, of the supplied data.
710 * \param bufObj Object to be cleared.
712 * \sa glClearBufferSubData, glClearBufferData and
713 * dd_function_table::ClearBufferSubData.
716 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
717 GLintptr offset
, GLsizeiptr size
,
718 const GLvoid
*clearValue
,
719 GLsizeiptr clearValueSize
,
720 struct gl_buffer_object
*bufObj
)
725 assert(ctx
->Driver
.MapBufferRange
);
726 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
728 GL_MAP_INVALIDATE_RANGE_BIT
,
729 bufObj
, MAP_INTERNAL
);
732 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
736 if (clearValue
== NULL
) {
737 /* Clear with zeros, per the spec */
738 memset(dest
, 0, size
);
739 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
743 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
744 memcpy(dest
, clearValue
, clearValueSize
);
745 dest
+= clearValueSize
;
748 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
753 * Default fallback for \c dd_function_table::MapBufferRange().
754 * Called via glMapBufferRange().
757 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
758 GLsizeiptr length
, GLbitfield access
,
759 struct gl_buffer_object
*bufObj
,
760 gl_map_buffer_index index
)
763 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
764 /* Just return a direct pointer to the data */
765 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
766 bufObj
->Mappings
[index
].Length
= length
;
767 bufObj
->Mappings
[index
].Offset
= offset
;
768 bufObj
->Mappings
[index
].AccessFlags
= access
;
769 return bufObj
->Mappings
[index
].Pointer
;
774 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
775 * Called via glFlushMappedBufferRange().
778 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
779 GLintptr offset
, GLsizeiptr length
,
780 struct gl_buffer_object
*obj
,
781 gl_map_buffer_index index
)
793 * Default callback for \c dd_function_table::UnmapBuffer().
795 * The input parameters will have been already tested for errors.
797 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
800 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
801 gl_map_buffer_index index
)
804 /* XXX we might assert here that bufObj->Pointer is non-null */
805 bufObj
->Mappings
[index
].Pointer
= NULL
;
806 bufObj
->Mappings
[index
].Length
= 0;
807 bufObj
->Mappings
[index
].Offset
= 0;
808 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
814 * Default fallback for \c dd_function_table::CopyBufferSubData().
815 * Called via glCopyBufferSubData().
818 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
819 struct gl_buffer_object
*src
,
820 struct gl_buffer_object
*dst
,
821 GLintptr readOffset
, GLintptr writeOffset
,
824 GLubyte
*srcPtr
, *dstPtr
;
827 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
829 GL_MAP_WRITE_BIT
, src
,
835 srcPtr
+= readOffset
;
836 dstPtr
+= writeOffset
;
838 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
839 GL_MAP_READ_BIT
, src
,
841 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
843 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
847 /* Note: the src and dst regions will never overlap. Trying to do so
848 * would generate GL_INVALID_VALUE earlier.
850 if (srcPtr
&& dstPtr
)
851 memcpy(dstPtr
, srcPtr
, size
);
853 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
855 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
861 * Initialize the state associated with buffer objects
864 _mesa_init_buffer_objects( struct gl_context
*ctx
)
868 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
869 simple_mtx_init(&DummyBufferObject
.MinMaxCacheMutex
, mtx_plain
);
870 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
872 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
873 _mesa_reference_buffer_object(ctx
,
874 &ctx
->UniformBufferBindings
[i
].BufferObject
,
876 ctx
->UniformBufferBindings
[i
].Offset
= -1;
877 ctx
->UniformBufferBindings
[i
].Size
= -1;
880 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
881 _mesa_reference_buffer_object(ctx
,
882 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
884 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
885 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
888 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
889 _mesa_reference_buffer_object(ctx
,
890 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
892 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
893 ctx
->AtomicBufferBindings
[i
].Size
= 0;
899 _mesa_free_buffer_objects( struct gl_context
*ctx
)
903 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
905 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
906 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
908 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
910 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
912 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
914 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
916 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
, NULL
);
918 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
920 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
, NULL
);
922 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
923 _mesa_reference_buffer_object(ctx
,
924 &ctx
->UniformBufferBindings
[i
].BufferObject
,
928 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
929 _mesa_reference_buffer_object(ctx
,
930 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
934 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
935 _mesa_reference_buffer_object(ctx
,
936 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
943 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
945 struct gl_buffer_object
**buf_handle
,
948 struct gl_buffer_object
*buf
= *buf_handle
;
950 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
)) {
951 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
955 if (!buf
|| buf
== &DummyBufferObject
) {
956 /* If this is a new buffer object id, or one which was generated but
957 * never used before, allocate a buffer object now.
959 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
961 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
964 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
972 * Bind the specified target to buffer for the specified context.
973 * Called by glBindBuffer() and other functions.
976 bind_buffer_object(struct gl_context
*ctx
,
977 struct gl_buffer_object
**bindTarget
, GLuint buffer
)
979 struct gl_buffer_object
*oldBufObj
;
980 struct gl_buffer_object
*newBufObj
= NULL
;
984 /* Get pointer to old buffer object (to be unbound) */
985 oldBufObj
= *bindTarget
;
986 if ((oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
) ||
987 (!oldBufObj
&& buffer
== 0))
988 return; /* rebinding the same buffer object- no change */
991 * Get pointer to new buffer object (newBufObj)
994 /* non-default buffer object */
995 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
996 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
997 &newBufObj
, "glBindBuffer"))
1000 /* record usage history */
1001 if (bindTarget
== &ctx
->Pack
.BufferObj
)
1002 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1005 /* bind new buffer */
1006 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1011 * Update the default buffer objects in the given context to reference those
1012 * specified in the shared state and release those referencing the old
1016 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1018 /* Bind 0 to remove references to those in the shared context hash table. */
1019 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1020 bind_buffer_object(ctx
, &ctx
->Array
.VAO
->IndexBufferObj
, 0);
1021 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1022 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1028 * Return the gl_buffer_object for the given ID.
1029 * Always return NULL for ID 0.
1031 struct gl_buffer_object
*
1032 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1037 return (struct gl_buffer_object
*)
1038 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1042 struct gl_buffer_object
*
1043 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1048 return (struct gl_buffer_object
*)
1049 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1053 * A convenience function for direct state access functions that throws
1054 * GL_INVALID_OPERATION if buffer is not the name of an existing
1057 struct gl_buffer_object
*
1058 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1061 struct gl_buffer_object
*bufObj
;
1063 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1064 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1065 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1066 "%s(non-existent buffer object %u)", caller
, buffer
);
1075 * Look up a buffer object for a multi-bind function.
1077 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1078 * of generating an error if the buffer ID is not zero or the name
1079 * of an existing buffer object.
1081 * If the buffer ID refers to an existing buffer object, a pointer
1082 * to the buffer object is returned. If the ID is zero, NULL is returned.
1083 * If the ID is not zero and does not refer to a valid buffer object, this
1084 * function returns NULL.
1086 * This function assumes that the caller has already locked the
1087 * hash table mutex by calling
1088 * _mesa_HashLockMutex(ctx->Shared->BufferObjects).
1090 struct gl_buffer_object
*
1091 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1092 const GLuint
*buffers
,
1093 GLuint index
, const char *caller
,
1096 struct gl_buffer_object
*bufObj
= NULL
;
1100 if (buffers
[index
] != 0) {
1101 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1103 /* The multi-bind functions don't create the buffer objects
1104 when they don't exist. */
1105 if (bufObj
== &DummyBufferObject
)
1109 /* The ARB_multi_bind spec says:
1111 * "An INVALID_OPERATION error is generated if any value
1112 * in <buffers> is not zero or the name of an existing
1113 * buffer object (per binding)."
1115 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1116 "%s(buffers[%u]=%u is not zero or the name "
1117 "of an existing buffer object)",
1118 caller
, index
, buffers
[index
]);
1128 * If *ptr points to obj, set ptr = the Null/default buffer object.
1129 * This is a helper for buffer object deletion.
1130 * The GL spec says that deleting a buffer object causes it to get
1131 * unbound from all arrays in the current context.
1134 unbind(struct gl_context
*ctx
,
1135 struct gl_vertex_array_object
*vao
, unsigned index
,
1136 struct gl_buffer_object
*obj
)
1138 if (vao
->BufferBinding
[index
].BufferObj
== obj
) {
1139 _mesa_bind_vertex_buffer(ctx
, vao
, index
, NULL
,
1140 vao
->BufferBinding
[index
].Offset
,
1141 vao
->BufferBinding
[index
].Stride
);
1147 * Plug default/fallback buffer object functions into the device
1151 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1153 /* GL_ARB_vertex/pixel_buffer_object */
1154 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1155 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1156 driver
->BufferData
= buffer_data_fallback
;
1157 driver
->BufferSubData
= buffer_sub_data_fallback
;
1158 driver
->GetBufferSubData
= buffer_get_subdata
;
1159 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1161 /* GL_ARB_clear_buffer_object */
1162 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1164 /* GL_ARB_map_buffer_range */
1165 driver
->MapBufferRange
= map_buffer_range_fallback
;
1166 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1168 /* GL_ARB_copy_buffer */
1169 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1174 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1175 struct gl_buffer_object
*bufObj
)
1177 for (int i
= 0; i
< MAP_COUNT
; i
++) {
1178 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1179 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1180 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1181 bufObj
->Mappings
[i
].AccessFlags
= 0;
1187 /**********************************************************************/
1189 /**********************************************************************/
1192 _mesa_BindBuffer_no_error(GLenum target
, GLuint buffer
)
1194 GET_CURRENT_CONTEXT(ctx
);
1196 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1197 bind_buffer_object(ctx
, bindTarget
, buffer
);
1202 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1204 GET_CURRENT_CONTEXT(ctx
);
1206 if (MESA_VERBOSE
& VERBOSE_API
) {
1207 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1208 _mesa_enum_to_string(target
), buffer
);
1211 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1213 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target %s)",
1214 _mesa_enum_to_string(target
));
1218 bind_buffer_object(ctx
, bindTarget
, buffer
);
1222 * Binds a buffer object to a binding point.
1224 * The caller is responsible for validating the offset,
1225 * flushing the vertices and updating NewDriverState.
1228 set_buffer_binding(struct gl_context
*ctx
,
1229 struct gl_buffer_binding
*binding
,
1230 struct gl_buffer_object
*bufObj
,
1233 bool autoSize
, gl_buffer_usage usage
)
1235 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1237 binding
->Offset
= offset
;
1238 binding
->Size
= size
;
1239 binding
->AutomaticSize
= autoSize
;
1241 /* If this is a real buffer object, mark it has having been used
1242 * at some point as an atomic counter buffer.
1245 bufObj
->UsageHistory
|= usage
;
1249 set_buffer_multi_binding(struct gl_context
*ctx
,
1250 const GLuint
*buffers
,
1253 struct gl_buffer_binding
*binding
,
1257 gl_buffer_usage usage
)
1259 struct gl_buffer_object
*bufObj
;
1261 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[idx
])
1262 bufObj
= binding
->BufferObject
;
1265 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, idx
, caller
,
1272 set_buffer_binding(ctx
, binding
, bufObj
, -1, -1, !range
, usage
);
1274 set_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, !range
, usage
);
1278 bind_buffer(struct gl_context
*ctx
,
1279 struct gl_buffer_binding
*binding
,
1280 struct gl_buffer_object
*bufObj
,
1284 uint64_t driver_state
,
1285 gl_buffer_usage usage
)
1287 if (binding
->BufferObject
== bufObj
&&
1288 binding
->Offset
== offset
&&
1289 binding
->Size
== size
&&
1290 binding
->AutomaticSize
== autoSize
) {
1294 FLUSH_VERTICES(ctx
, 0);
1295 ctx
->NewDriverState
|= driver_state
;
1297 set_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
, usage
);
1301 * Binds a buffer object to a uniform buffer binding point.
1303 * Unlike set_buffer_binding(), this function also flushes vertices
1304 * and updates NewDriverState. It also checks if the binding
1305 * has actually changed before updating it.
1308 bind_uniform_buffer(struct gl_context
*ctx
,
1310 struct gl_buffer_object
*bufObj
,
1315 bind_buffer(ctx
, &ctx
->UniformBufferBindings
[index
],
1316 bufObj
, offset
, size
, autoSize
,
1317 ctx
->DriverFlags
.NewUniformBuffer
,
1318 USAGE_UNIFORM_BUFFER
);
1322 * Binds a buffer object to a shader storage buffer binding point.
1324 * Unlike set_ssbo_binding(), this function also flushes vertices
1325 * and updates NewDriverState. It also checks if the binding
1326 * has actually changed before updating it.
1329 bind_shader_storage_buffer(struct gl_context
*ctx
,
1331 struct gl_buffer_object
*bufObj
,
1336 bind_buffer(ctx
, &ctx
->ShaderStorageBufferBindings
[index
],
1337 bufObj
, offset
, size
, autoSize
,
1338 ctx
->DriverFlags
.NewShaderStorageBuffer
,
1339 USAGE_SHADER_STORAGE_BUFFER
);
1343 * Binds a buffer object to an atomic buffer binding point.
1345 * Unlike set_atomic_binding(), this function also flushes vertices
1346 * and updates NewDriverState. It also checks if the binding
1347 * has actually changed before updating it.
1350 bind_atomic_buffer(struct gl_context
*ctx
, unsigned index
,
1351 struct gl_buffer_object
*bufObj
, GLintptr offset
,
1352 GLsizeiptr size
, GLboolean autoSize
)
1354 bind_buffer(ctx
, &ctx
->AtomicBufferBindings
[index
],
1355 bufObj
, offset
, size
, autoSize
,
1356 ctx
->DriverFlags
.NewAtomicBuffer
,
1357 USAGE_ATOMIC_COUNTER_BUFFER
);
1361 * Bind a buffer object to a uniform block binding point.
1362 * As above, but offset = 0.
1365 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
1367 struct gl_buffer_object
*bufObj
)
1369 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
1370 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1374 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
1377 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1379 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1383 * Bind a buffer object to a shader storage block binding point.
1384 * As above, but offset = 0.
1387 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
1389 struct gl_buffer_object
*bufObj
)
1391 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
1392 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1396 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
1399 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1401 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1405 * Bind a buffer object to a shader storage block binding point.
1406 * As above, but offset = 0.
1409 bind_buffer_base_atomic_buffer(struct gl_context
*ctx
,
1411 struct gl_buffer_object
*bufObj
)
1413 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
1414 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1418 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
1421 bind_atomic_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1423 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1427 * Delete a set of buffer objects.
1429 * \param n Number of buffer objects to delete.
1430 * \param ids Array of \c n buffer object IDs.
1433 delete_buffers(struct gl_context
*ctx
, GLsizei n
, const GLuint
*ids
)
1435 FLUSH_VERTICES(ctx
, 0);
1437 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1439 for (GLsizei i
= 0; i
< n
; i
++) {
1440 struct gl_buffer_object
*bufObj
=
1441 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1443 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1446 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1448 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1450 /* unbind any vertex pointers bound to this buffer */
1451 for (j
= 0; j
< ARRAY_SIZE(vao
->BufferBinding
); j
++) {
1452 unbind(ctx
, vao
, j
, bufObj
);
1455 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1456 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1458 if (vao
->IndexBufferObj
== bufObj
) {
1459 bind_buffer_object(ctx
, &vao
->IndexBufferObj
, 0);
1462 /* unbind ARB_draw_indirect binding point */
1463 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1464 bind_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, 0);
1467 /* unbind ARB_indirect_parameters binding point */
1468 if (ctx
->ParameterBuffer
== bufObj
) {
1469 bind_buffer_object(ctx
, &ctx
->ParameterBuffer
, 0);
1472 /* unbind ARB_compute_shader binding point */
1473 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1474 bind_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, 0);
1477 /* unbind ARB_copy_buffer binding points */
1478 if (ctx
->CopyReadBuffer
== bufObj
) {
1479 bind_buffer_object(ctx
, &ctx
->CopyReadBuffer
, 0);
1481 if (ctx
->CopyWriteBuffer
== bufObj
) {
1482 bind_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, 0);
1485 /* unbind transform feedback binding points */
1486 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1487 bind_buffer_object(ctx
, &ctx
->TransformFeedback
.CurrentBuffer
, 0);
1489 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1490 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1491 _mesa_bind_buffer_base_transform_feedback(ctx
,
1492 ctx
->TransformFeedback
.CurrentObject
,
1497 /* unbind UBO binding points */
1498 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1499 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1500 bind_buffer_base_uniform_buffer(ctx
, j
, NULL
);
1504 if (ctx
->UniformBuffer
== bufObj
) {
1505 bind_buffer_object(ctx
, &ctx
->UniformBuffer
, 0);
1508 /* unbind SSBO binding points */
1509 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1510 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1511 bind_buffer_base_shader_storage_buffer(ctx
, j
, NULL
);
1515 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1516 bind_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, 0);
1519 /* unbind Atomci Buffer binding points */
1520 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1521 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1522 bind_buffer_base_atomic_buffer(ctx
, j
, NULL
);
1526 if (ctx
->AtomicBuffer
== bufObj
) {
1527 bind_buffer_object(ctx
, &ctx
->AtomicBuffer
, 0);
1530 /* unbind any pixel pack/unpack pointers bound to this buffer */
1531 if (ctx
->Pack
.BufferObj
== bufObj
) {
1532 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1534 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1535 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1538 if (ctx
->Texture
.BufferObject
== bufObj
) {
1539 bind_buffer_object(ctx
, &ctx
->Texture
.BufferObject
, 0);
1542 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1543 bind_buffer_object(ctx
, &ctx
->ExternalVirtualMemoryBuffer
, 0);
1546 /* unbind query buffer binding point */
1547 if (ctx
->QueryBuffer
== bufObj
) {
1548 bind_buffer_object(ctx
, &ctx
->QueryBuffer
, 0);
1551 /* The ID is immediately freed for re-use */
1552 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1553 /* Make sure we do not run into the classic ABA problem on bind.
1554 * We don't want to allow re-binding a buffer object that's been
1555 * "deleted" by glDeleteBuffers().
1557 * The explicit rebinding to the default object in the current context
1558 * prevents the above in the current context, but another context
1559 * sharing the same objects might suffer from this problem.
1560 * The alternative would be to do the hash lookup in any case on bind
1561 * which would introduce more runtime overhead than this.
1563 bufObj
->DeletePending
= GL_TRUE
;
1564 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1568 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1573 _mesa_DeleteBuffers_no_error(GLsizei n
, const GLuint
*ids
)
1575 GET_CURRENT_CONTEXT(ctx
);
1576 delete_buffers(ctx
, n
, ids
);
1581 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1583 GET_CURRENT_CONTEXT(ctx
);
1586 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1590 delete_buffers(ctx
, n
, ids
);
1595 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1596 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1600 create_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1603 struct gl_buffer_object
*buf
;
1609 * This must be atomic (generation and allocation of buffer object IDs)
1611 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1613 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1615 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1616 * DummyBufferObject. Otherwise, create a new buffer object and insert
1619 for (int i
= 0; i
< n
; i
++) {
1620 buffers
[i
] = first
+ i
;
1622 assert(ctx
->Driver
.NewBufferObject
);
1623 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1625 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCreateBuffers");
1626 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1631 buf
= &DummyBufferObject
;
1633 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1636 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1641 create_buffers_err(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1643 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1645 if (MESA_VERBOSE
& VERBOSE_API
)
1646 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1649 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1653 create_buffers(ctx
, n
, buffers
, dsa
);
1657 * Generate a set of unique buffer object IDs and store them in \c buffers.
1659 * \param n Number of IDs to generate.
1660 * \param buffers Array of \c n locations to store the IDs.
1663 _mesa_GenBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1665 GET_CURRENT_CONTEXT(ctx
);
1666 create_buffers(ctx
, n
, buffers
, false);
1671 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1673 GET_CURRENT_CONTEXT(ctx
);
1674 create_buffers_err(ctx
, n
, buffers
, false);
1678 * Create a set of buffer objects and store their unique IDs in \c buffers.
1680 * \param n Number of IDs to generate.
1681 * \param buffers Array of \c n locations to store the IDs.
1684 _mesa_CreateBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1686 GET_CURRENT_CONTEXT(ctx
);
1687 create_buffers(ctx
, n
, buffers
, true);
1692 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1694 GET_CURRENT_CONTEXT(ctx
);
1695 create_buffers_err(ctx
, n
, buffers
, true);
1700 * Determine if ID is the name of a buffer object.
1702 * \param id ID of the potential buffer object.
1703 * \return \c GL_TRUE if \c id is the name of a buffer object,
1704 * \c GL_FALSE otherwise.
1706 GLboolean GLAPIENTRY
1707 _mesa_IsBuffer(GLuint id
)
1709 struct gl_buffer_object
*bufObj
;
1710 GET_CURRENT_CONTEXT(ctx
);
1711 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1713 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1715 return bufObj
&& bufObj
!= &DummyBufferObject
;
1720 validate_buffer_storage(struct gl_context
*ctx
,
1721 struct gl_buffer_object
*bufObj
, GLsizeiptr size
,
1722 GLbitfield flags
, const char *func
)
1725 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1729 GLbitfield valid_flags
= GL_MAP_READ_BIT
|
1731 GL_MAP_PERSISTENT_BIT
|
1732 GL_MAP_COHERENT_BIT
|
1733 GL_DYNAMIC_STORAGE_BIT
|
1734 GL_CLIENT_STORAGE_BIT
;
1736 if (ctx
->Extensions
.ARB_sparse_buffer
)
1737 valid_flags
|= GL_SPARSE_STORAGE_BIT_ARB
;
1739 if (flags
& ~valid_flags
) {
1740 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1744 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1746 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1747 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1748 * MAP_READ_BIT or MAP_WRITE_BIT."
1750 if (flags
& GL_SPARSE_STORAGE_BIT_ARB
&&
1751 flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) {
1752 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(SPARSE_STORAGE and READ/WRITE)", func
);
1756 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1757 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1758 _mesa_error(ctx
, GL_INVALID_VALUE
,
1759 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1763 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1764 _mesa_error(ctx
, GL_INVALID_VALUE
,
1765 "%s(COHERENT and flags!=PERSISTENT)", func
);
1769 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
1770 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1779 buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1780 struct gl_memory_object
*memObj
, GLenum target
,
1781 GLsizeiptr size
, const GLvoid
*data
, GLbitfield flags
,
1782 GLuint64 offset
, const char *func
)
1786 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1787 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1789 FLUSH_VERTICES(ctx
, 0);
1791 bufObj
->Written
= GL_TRUE
;
1792 bufObj
->Immutable
= GL_TRUE
;
1793 bufObj
->MinMaxCacheDirty
= true;
1796 assert(ctx
->Driver
.BufferDataMem
);
1797 res
= ctx
->Driver
.BufferDataMem(ctx
, target
, size
, memObj
, offset
,
1798 GL_DYNAMIC_DRAW
, bufObj
);
1801 assert(ctx
->Driver
.BufferData
);
1802 res
= ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1807 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1808 /* Even though the interaction between AMD_pinned_memory and
1809 * glBufferStorage is not described in the spec, Graham Sellers
1810 * said that it should behave the same as glBufferData.
1812 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1815 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1821 static ALWAYS_INLINE
void
1822 inlined_buffer_storage(GLenum target
, GLuint buffer
, GLsizeiptr size
,
1823 const GLvoid
*data
, GLbitfield flags
,
1824 GLuint memory
, GLuint64 offset
,
1825 bool dsa
, bool mem
, bool no_error
, const char *func
)
1827 GET_CURRENT_CONTEXT(ctx
);
1828 struct gl_buffer_object
*bufObj
;
1829 struct gl_memory_object
*memObj
= NULL
;
1833 if (!ctx
->Extensions
.EXT_memory_object
) {
1834 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
1838 /* From the EXT_external_objects spec:
1840 * "An INVALID_VALUE error is generated by BufferStorageMemEXT and
1841 * NamedBufferStorageMemEXT if <memory> is 0, or ..."
1844 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(memory == 0)", func
);
1848 memObj
= _mesa_lookup_memory_object(ctx
, memory
);
1852 /* From the EXT_external_objects spec:
1854 * "An INVALID_OPERATION error is generated if <memory> names a
1855 * valid memory object which has no associated memory."
1857 if (!no_error
&& !memObj
->Immutable
) {
1858 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no associated memory)",
1866 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1868 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
1874 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
1875 bufObj
= *bufObjPtr
;
1877 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
1883 if (no_error
|| validate_buffer_storage(ctx
, bufObj
, size
, flags
, func
))
1884 buffer_storage(ctx
, bufObj
, memObj
, target
, size
, data
, flags
, offset
, func
);
1889 _mesa_BufferStorage_no_error(GLenum target
, GLsizeiptr size
,
1890 const GLvoid
*data
, GLbitfield flags
)
1892 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1893 false, false, true, "glBufferStorage");
1898 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1901 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1902 false, false, false, "glBufferStorage");
1906 _mesa_NamedBufferStorageEXT(GLuint buffer
, GLsizeiptr size
,
1907 const GLvoid
*data
, GLbitfield flags
)
1909 GET_CURRENT_CONTEXT(ctx
);
1911 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1912 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1913 &bufObj
, "glNamedBufferStorageEXT"))
1916 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1917 true, false, false, "glNamedBufferStorageEXT");
1922 _mesa_BufferStorageMemEXT(GLenum target
, GLsizeiptr size
,
1923 GLuint memory
, GLuint64 offset
)
1925 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1926 false, true, false, "glBufferStorageMemEXT");
1931 _mesa_BufferStorageMemEXT_no_error(GLenum target
, GLsizeiptr size
,
1932 GLuint memory
, GLuint64 offset
)
1934 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1935 false, true, true, "glBufferStorageMemEXT");
1940 _mesa_NamedBufferStorage_no_error(GLuint buffer
, GLsizeiptr size
,
1941 const GLvoid
*data
, GLbitfield flags
)
1943 /* In direct state access, buffer objects have an unspecified target
1944 * since they are not required to be bound.
1946 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1947 true, false, true, "glNamedBufferStorage");
1952 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1955 /* In direct state access, buffer objects have an unspecified target
1956 * since they are not required to be bound.
1958 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1959 true, false, false, "glNamedBufferStorage");
1963 _mesa_NamedBufferStorageMemEXT(GLuint buffer
, GLsizeiptr size
,
1964 GLuint memory
, GLuint64 offset
)
1966 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1967 true, true, false, "glNamedBufferStorageMemEXT");
1972 _mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer
, GLsizeiptr size
,
1973 GLuint memory
, GLuint64 offset
)
1975 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1976 true, true, true, "glNamedBufferStorageMemEXT");
1980 static ALWAYS_INLINE
void
1981 buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1982 GLenum target
, GLsizeiptr size
, const GLvoid
*data
, GLenum usage
,
1983 const char *func
, bool no_error
)
1987 if (MESA_VERBOSE
& VERBOSE_API
) {
1988 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1990 _mesa_enum_to_string(target
),
1991 (long int) size
, data
,
1992 _mesa_enum_to_string(usage
));
1997 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
2002 case GL_STREAM_DRAW_ARB
:
2003 valid_usage
= (ctx
->API
!= API_OPENGLES
);
2005 case GL_STATIC_DRAW_ARB
:
2006 case GL_DYNAMIC_DRAW_ARB
:
2009 case GL_STREAM_READ_ARB
:
2010 case GL_STREAM_COPY_ARB
:
2011 case GL_STATIC_READ_ARB
:
2012 case GL_STATIC_COPY_ARB
:
2013 case GL_DYNAMIC_READ_ARB
:
2014 case GL_DYNAMIC_COPY_ARB
:
2015 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
2018 valid_usage
= false;
2023 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
2024 _mesa_enum_to_string(usage
));
2028 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
2029 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
2034 /* Unmap the existing buffer. We'll replace it now. Not an error. */
2035 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
2037 FLUSH_VERTICES(ctx
, 0);
2039 bufObj
->Written
= GL_TRUE
;
2040 bufObj
->MinMaxCacheDirty
= true;
2043 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
2044 bufObj
->Name
, size
, data
, usage
);
2051 assert(ctx
->Driver
.BufferData
);
2052 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
2055 GL_DYNAMIC_STORAGE_BIT
,
2057 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
2059 /* From GL_AMD_pinned_memory:
2061 * INVALID_OPERATION is generated by BufferData if <target> is
2062 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
2063 * mapped to the GPU address space.
2065 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2068 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2074 buffer_data_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2075 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2076 GLenum usage
, const char *func
)
2078 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, false);
2082 buffer_data_no_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2083 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2084 GLenum usage
, const char *func
)
2086 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, true);
2090 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2091 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2092 GLenum usage
, const char *func
)
2094 buffer_data_error(ctx
, bufObj
, target
, size
, data
, usage
, func
);
2098 _mesa_BufferData_no_error(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2101 GET_CURRENT_CONTEXT(ctx
);
2103 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2104 buffer_data_no_error(ctx
, *bufObj
, target
, size
, data
, usage
,
2109 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
2110 const GLvoid
*data
, GLenum usage
)
2112 GET_CURRENT_CONTEXT(ctx
);
2113 struct gl_buffer_object
*bufObj
;
2115 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
2119 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
2124 _mesa_NamedBufferData_no_error(GLuint buffer
, GLsizeiptr size
,
2125 const GLvoid
*data
, GLenum usage
)
2127 GET_CURRENT_CONTEXT(ctx
);
2129 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2130 buffer_data_no_error(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2131 "glNamedBufferData");
2135 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2138 GET_CURRENT_CONTEXT(ctx
);
2139 struct gl_buffer_object
*bufObj
;
2141 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
2145 /* In direct state access, buffer objects have an unspecified target since
2146 * they are not required to be bound.
2148 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2149 "glNamedBufferData");
2153 _mesa_NamedBufferDataEXT(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2156 GET_CURRENT_CONTEXT(ctx
);
2157 struct gl_buffer_object
*bufObj
;
2160 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2161 "glNamedBufferDataEXT(buffer=0)");
2165 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2166 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2167 &bufObj
, "glNamedBufferDataEXT"))
2170 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2171 "glNamedBufferDataEXT");
2175 validate_buffer_sub_data(struct gl_context
*ctx
,
2176 struct gl_buffer_object
*bufObj
,
2177 GLintptr offset
, GLsizeiptr size
,
2180 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2182 /* error already recorded */
2186 if (bufObj
->Immutable
&&
2187 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
2188 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2192 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2193 bufObj
->Usage
== GL_STATIC_COPY
) &&
2194 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
- 1) {
2195 /* If the application declared the buffer as static draw/copy or stream
2196 * draw, it should not be frequently modified with glBufferSubData.
2198 BUFFER_USAGE_WARNING(ctx
,
2199 "using %s(buffer %u, offset %u, size %u) to "
2200 "update a %s buffer",
2201 func
, bufObj
->Name
, offset
, size
,
2202 _mesa_enum_to_string(bufObj
->Usage
));
2210 * Implementation for glBufferSubData and glNamedBufferSubData.
2212 * \param ctx GL context.
2213 * \param bufObj The buffer object.
2214 * \param offset Offset of the first byte of the subdata range.
2215 * \param size Size, in bytes, of the subdata range.
2216 * \param data The data store.
2217 * \param func Name of calling function for recording errors.
2221 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2222 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
)
2227 bufObj
->NumSubDataCalls
++;
2228 bufObj
->Written
= GL_TRUE
;
2229 bufObj
->MinMaxCacheDirty
= true;
2231 assert(ctx
->Driver
.BufferSubData
);
2232 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
2236 static ALWAYS_INLINE
void
2237 buffer_sub_data(GLenum target
, GLuint buffer
, GLintptr offset
,
2238 GLsizeiptr size
, const GLvoid
*data
,
2239 bool dsa
, bool no_error
, const char *func
)
2241 GET_CURRENT_CONTEXT(ctx
);
2242 struct gl_buffer_object
*bufObj
;
2246 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2248 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
2254 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2255 bufObj
= *bufObjPtr
;
2257 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
2263 if (no_error
|| validate_buffer_sub_data(ctx
, bufObj
, offset
, size
, func
))
2264 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2269 _mesa_BufferSubData_no_error(GLenum target
, GLintptr offset
,
2270 GLsizeiptr size
, const GLvoid
*data
)
2272 buffer_sub_data(target
, 0, offset
, size
, data
, false, true,
2278 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
2279 GLsizeiptr size
, const GLvoid
*data
)
2281 buffer_sub_data(target
, 0, offset
, size
, data
, false, false,
2286 _mesa_NamedBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
2287 GLsizeiptr size
, const GLvoid
*data
)
2289 buffer_sub_data(0, buffer
, offset
, size
, data
, true, true,
2290 "glNamedBufferSubData");
2294 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
2295 GLsizeiptr size
, const GLvoid
*data
)
2297 buffer_sub_data(0, buffer
, offset
, size
, data
, true, false,
2298 "glNamedBufferSubData");
2302 _mesa_NamedBufferSubDataEXT(GLuint buffer
, GLintptr offset
,
2303 GLsizeiptr size
, const GLvoid
*data
)
2305 GET_CURRENT_CONTEXT(ctx
);
2306 struct gl_buffer_object
*bufObj
;
2309 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2310 "glNamedBufferSubDataEXT(buffer=0)");
2314 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2315 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2316 &bufObj
, "glNamedBufferSubDataEXT"))
2319 if (validate_buffer_sub_data(ctx
, bufObj
, offset
, size
,
2320 "glNamedBufferSubDataEXT")) {
2321 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2327 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
2328 GLsizeiptr size
, GLvoid
*data
)
2330 GET_CURRENT_CONTEXT(ctx
);
2331 struct gl_buffer_object
*bufObj
;
2333 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
2334 GL_INVALID_OPERATION
);
2338 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2339 "glGetBufferSubData")) {
2343 assert(ctx
->Driver
.GetBufferSubData
);
2344 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2348 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
2349 GLsizeiptr size
, GLvoid
*data
)
2351 GET_CURRENT_CONTEXT(ctx
);
2352 struct gl_buffer_object
*bufObj
;
2354 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2355 "glGetNamedBufferSubData");
2359 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2360 "glGetNamedBufferSubData")) {
2364 assert(ctx
->Driver
.GetBufferSubData
);
2365 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2370 _mesa_GetNamedBufferSubDataEXT(GLuint buffer
, GLintptr offset
,
2371 GLsizeiptr size
, GLvoid
*data
)
2373 GET_CURRENT_CONTEXT(ctx
);
2374 struct gl_buffer_object
*bufObj
;
2377 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2378 "glGetNamedBufferSubDataEXT(buffer=0)");
2382 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2383 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2384 &bufObj
, "glGetNamedBufferSubDataEXT"))
2387 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2388 "glGetNamedBufferSubDataEXT")) {
2392 assert(ctx
->Driver
.GetBufferSubData
);
2393 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2397 * \param subdata true if caller is *SubData, false if *Data
2399 static ALWAYS_INLINE
void
2400 clear_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2401 GLenum internalformat
, GLintptr offset
, GLsizeiptr size
,
2402 GLenum format
, GLenum type
, const GLvoid
*data
,
2403 const char *func
, bool subdata
, bool no_error
)
2405 mesa_format mesaFormat
;
2406 GLubyte clearValue
[MAX_PIXEL_BYTES
];
2407 GLsizeiptr clearValueSize
;
2409 /* This checks for disallowed mappings. */
2410 if (!no_error
&& !buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2416 mesaFormat
= _mesa_get_texbuffer_format(ctx
, internalformat
);
2418 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
2419 format
, type
, func
);
2422 if (mesaFormat
== MESA_FORMAT_NONE
)
2425 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
2427 (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0)) {
2428 _mesa_error(ctx
, GL_INVALID_VALUE
,
2429 "%s(offset or size is not a multiple of "
2430 "internalformat size)", func
);
2434 /* Bail early. Negative size has already been checked. */
2438 bufObj
->MinMaxCacheDirty
= true;
2441 /* clear to zeros, per the spec */
2442 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2443 NULL
, clearValueSize
, bufObj
);
2447 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
2448 format
, type
, data
, func
)) {
2452 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2453 clearValue
, clearValueSize
, bufObj
);
2457 clear_buffer_sub_data_error(struct gl_context
*ctx
,
2458 struct gl_buffer_object
*bufObj
,
2459 GLenum internalformat
, GLintptr offset
,
2460 GLsizeiptr size
, GLenum format
, GLenum type
,
2461 const GLvoid
*data
, const char *func
, bool subdata
)
2463 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2464 type
, data
, func
, subdata
, false);
2469 clear_buffer_sub_data_no_error(struct gl_context
*ctx
,
2470 struct gl_buffer_object
*bufObj
,
2471 GLenum internalformat
, GLintptr offset
,
2472 GLsizeiptr size
, GLenum format
, GLenum type
,
2473 const GLvoid
*data
, const char *func
,
2476 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2477 type
, data
, func
, subdata
, true);
2482 _mesa_ClearBufferData_no_error(GLenum target
, GLenum internalformat
,
2483 GLenum format
, GLenum type
, const GLvoid
*data
)
2485 GET_CURRENT_CONTEXT(ctx
);
2487 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2488 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, 0,
2489 (*bufObj
)->Size
, format
, type
, data
,
2490 "glClearBufferData", false);
2495 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
2496 GLenum type
, const GLvoid
*data
)
2498 GET_CURRENT_CONTEXT(ctx
);
2499 struct gl_buffer_object
*bufObj
;
2501 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
2505 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2506 format
, type
, data
, "glClearBufferData", false);
2511 _mesa_ClearNamedBufferData_no_error(GLuint buffer
, GLenum internalformat
,
2512 GLenum format
, GLenum type
,
2515 GET_CURRENT_CONTEXT(ctx
);
2517 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2518 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2519 format
, type
, data
, "glClearNamedBufferData",
2525 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
2526 GLenum format
, GLenum type
, const GLvoid
*data
)
2528 GET_CURRENT_CONTEXT(ctx
);
2529 struct gl_buffer_object
*bufObj
;
2531 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
2535 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2536 format
, type
, data
, "glClearNamedBufferData",
2542 _mesa_ClearNamedBufferDataEXT(GLuint buffer
, GLenum internalformat
,
2543 GLenum format
, GLenum type
, const GLvoid
*data
)
2545 GET_CURRENT_CONTEXT(ctx
);
2546 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2547 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2548 &bufObj
, "glClearNamedBufferDataEXT"))
2551 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2552 format
, type
, data
, "glClearNamedBufferDataEXT",
2558 _mesa_ClearBufferSubData_no_error(GLenum target
, GLenum internalformat
,
2559 GLintptr offset
, GLsizeiptr size
,
2560 GLenum format
, GLenum type
,
2563 GET_CURRENT_CONTEXT(ctx
);
2565 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2566 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, offset
, size
,
2567 format
, type
, data
, "glClearBufferSubData",
2573 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
2574 GLintptr offset
, GLsizeiptr size
,
2575 GLenum format
, GLenum type
,
2578 GET_CURRENT_CONTEXT(ctx
);
2579 struct gl_buffer_object
*bufObj
;
2581 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
2585 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2586 format
, type
, data
, "glClearBufferSubData",
2592 _mesa_ClearNamedBufferSubData_no_error(GLuint buffer
, GLenum internalformat
,
2593 GLintptr offset
, GLsizeiptr size
,
2594 GLenum format
, GLenum type
,
2597 GET_CURRENT_CONTEXT(ctx
);
2599 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2600 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, offset
, size
,
2602 "glClearNamedBufferSubData", true);
2607 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
2608 GLintptr offset
, GLsizeiptr size
,
2609 GLenum format
, GLenum type
,
2612 GET_CURRENT_CONTEXT(ctx
);
2613 struct gl_buffer_object
*bufObj
;
2615 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2616 "glClearNamedBufferSubData");
2620 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2621 format
, type
, data
, "glClearNamedBufferSubData",
2626 _mesa_ClearNamedBufferSubDataEXT(GLuint buffer
, GLenum internalformat
,
2627 GLintptr offset
, GLsizeiptr size
,
2628 GLenum format
, GLenum type
,
2631 GET_CURRENT_CONTEXT(ctx
);
2632 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2633 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2634 &bufObj
, "glClearNamedBufferSubDataEXT"))
2637 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2638 format
, type
, data
, "glClearNamedBufferSubDataEXT",
2643 unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
2645 GLboolean status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2646 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2647 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2648 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2649 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2655 validate_and_unmap_buffer(struct gl_context
*ctx
,
2656 struct gl_buffer_object
*bufObj
,
2659 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2661 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2662 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2663 "%s(buffer is not mapped)", func
);
2668 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2669 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2671 /* check that last 100 bytes are still = magic value */
2672 for (i
= 0; i
< 100; i
++) {
2673 GLuint pos
= bufObj
->Size
- i
- 1;
2674 if (buf
[pos
] != 123) {
2675 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2676 " at position %d (value = %u)\n",
2684 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2685 GLuint i
, unchanged
= 0;
2686 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2688 /* check which bytes changed */
2689 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2690 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2697 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2698 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2703 return unmap_buffer(ctx
, bufObj
);
2706 GLboolean GLAPIENTRY
2707 _mesa_UnmapBuffer_no_error(GLenum target
)
2709 GET_CURRENT_CONTEXT(ctx
);
2710 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2711 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2713 return unmap_buffer(ctx
, bufObj
);
2716 GLboolean GLAPIENTRY
2717 _mesa_UnmapBuffer(GLenum target
)
2719 GET_CURRENT_CONTEXT(ctx
);
2720 struct gl_buffer_object
*bufObj
;
2722 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2726 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2729 GLboolean GLAPIENTRY
2730 _mesa_UnmapNamedBufferEXT_no_error(GLuint buffer
)
2732 GET_CURRENT_CONTEXT(ctx
);
2733 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2735 return unmap_buffer(ctx
, bufObj
);
2738 GLboolean GLAPIENTRY
2739 _mesa_UnmapNamedBufferEXT(GLuint buffer
)
2741 GET_CURRENT_CONTEXT(ctx
);
2742 struct gl_buffer_object
*bufObj
;
2745 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2746 "glUnmapNamedBufferEXT(buffer=0)");
2750 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2754 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2759 get_buffer_parameter(struct gl_context
*ctx
,
2760 struct gl_buffer_object
*bufObj
, GLenum pname
,
2761 GLint64
*params
, const char *func
)
2764 case GL_BUFFER_SIZE_ARB
:
2765 *params
= bufObj
->Size
;
2767 case GL_BUFFER_USAGE_ARB
:
2768 *params
= bufObj
->Usage
;
2770 case GL_BUFFER_ACCESS_ARB
:
2771 *params
= simplified_access_mode(ctx
,
2772 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2774 case GL_BUFFER_MAPPED_ARB
:
2775 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2777 case GL_BUFFER_ACCESS_FLAGS
:
2778 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2780 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2782 case GL_BUFFER_MAP_OFFSET
:
2783 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2785 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2787 case GL_BUFFER_MAP_LENGTH
:
2788 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2790 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2792 case GL_BUFFER_IMMUTABLE_STORAGE
:
2793 if (!ctx
->Extensions
.ARB_buffer_storage
)
2795 *params
= bufObj
->Immutable
;
2797 case GL_BUFFER_STORAGE_FLAGS
:
2798 if (!ctx
->Extensions
.ARB_buffer_storage
)
2800 *params
= bufObj
->StorageFlags
;
2809 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2810 _mesa_enum_to_string(pname
));
2815 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2817 GET_CURRENT_CONTEXT(ctx
);
2818 struct gl_buffer_object
*bufObj
;
2821 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2822 GL_INVALID_OPERATION
);
2826 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2827 "glGetBufferParameteriv"))
2828 return; /* Error already recorded. */
2830 *params
= (GLint
) parameter
;
2834 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2836 GET_CURRENT_CONTEXT(ctx
);
2837 struct gl_buffer_object
*bufObj
;
2840 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2841 GL_INVALID_OPERATION
);
2845 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2846 "glGetBufferParameteri64v"))
2847 return; /* Error already recorded. */
2849 *params
= parameter
;
2853 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2855 GET_CURRENT_CONTEXT(ctx
);
2856 struct gl_buffer_object
*bufObj
;
2859 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2860 "glGetNamedBufferParameteriv");
2864 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2865 "glGetNamedBufferParameteriv"))
2866 return; /* Error already recorded. */
2868 *params
= (GLint
) parameter
;
2872 _mesa_GetNamedBufferParameterivEXT(GLuint buffer
, GLenum pname
, GLint
*params
)
2874 GET_CURRENT_CONTEXT(ctx
);
2875 struct gl_buffer_object
*bufObj
;
2879 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2880 "glGetNamedBufferParameterivEXT: buffer=0");
2884 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2885 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2886 &bufObj
, "glGetNamedBufferParameterivEXT"))
2889 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2890 "glGetNamedBufferParameterivEXT"))
2891 return; /* Error already recorded. */
2893 *params
= (GLint
) parameter
;
2897 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2900 GET_CURRENT_CONTEXT(ctx
);
2901 struct gl_buffer_object
*bufObj
;
2904 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2905 "glGetNamedBufferParameteri64v");
2909 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2910 "glGetNamedBufferParameteri64v"))
2911 return; /* Error already recorded. */
2913 *params
= parameter
;
2918 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2920 GET_CURRENT_CONTEXT(ctx
);
2921 struct gl_buffer_object
*bufObj
;
2923 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2924 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2925 "GL_BUFFER_MAP_POINTER)");
2929 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2930 GL_INVALID_OPERATION
);
2934 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2938 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2940 GET_CURRENT_CONTEXT(ctx
);
2941 struct gl_buffer_object
*bufObj
;
2943 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2944 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2945 "GL_BUFFER_MAP_POINTER)");
2949 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2950 "glGetNamedBufferPointerv");
2954 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2958 _mesa_GetNamedBufferPointervEXT(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2960 GET_CURRENT_CONTEXT(ctx
);
2961 struct gl_buffer_object
*bufObj
;
2964 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2965 "glGetNamedBufferPointervEXT(buffer=0)");
2968 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2969 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointervEXT(pname != "
2970 "GL_BUFFER_MAP_POINTER)");
2974 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2975 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2976 &bufObj
, "glGetNamedBufferPointervEXT"))
2979 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2983 copy_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*src
,
2984 struct gl_buffer_object
*dst
, GLintptr readOffset
,
2985 GLintptr writeOffset
, GLsizeiptr size
, const char *func
)
2987 if (_mesa_check_disallowed_mapping(src
)) {
2988 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2989 "%s(readBuffer is mapped)", func
);
2993 if (_mesa_check_disallowed_mapping(dst
)) {
2994 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2995 "%s(writeBuffer is mapped)", func
);
2999 if (readOffset
< 0) {
3000 _mesa_error(ctx
, GL_INVALID_VALUE
,
3001 "%s(readOffset %d < 0)", func
, (int) readOffset
);
3005 if (writeOffset
< 0) {
3006 _mesa_error(ctx
, GL_INVALID_VALUE
,
3007 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
3012 _mesa_error(ctx
, GL_INVALID_VALUE
,
3013 "%s(size %d < 0)", func
, (int) size
);
3017 if (readOffset
+ size
> src
->Size
) {
3018 _mesa_error(ctx
, GL_INVALID_VALUE
,
3019 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
3020 (int) readOffset
, (int) size
, (int) src
->Size
);
3024 if (writeOffset
+ size
> dst
->Size
) {
3025 _mesa_error(ctx
, GL_INVALID_VALUE
,
3026 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
3027 (int) writeOffset
, (int) size
, (int) dst
->Size
);
3032 if (readOffset
+ size
<= writeOffset
) {
3035 else if (writeOffset
+ size
<= readOffset
) {
3039 /* overlapping src/dst is illegal */
3040 _mesa_error(ctx
, GL_INVALID_VALUE
,
3041 "%s(overlapping src/dst)", func
);
3046 dst
->MinMaxCacheDirty
= true;
3048 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
3052 _mesa_CopyBufferSubData_no_error(GLenum readTarget
, GLenum writeTarget
,
3053 GLintptr readOffset
, GLintptr writeOffset
,
3056 GET_CURRENT_CONTEXT(ctx
);
3058 struct gl_buffer_object
**src_ptr
= get_buffer_target(ctx
, readTarget
);
3059 struct gl_buffer_object
*src
= *src_ptr
;
3061 struct gl_buffer_object
**dst_ptr
= get_buffer_target(ctx
, writeTarget
);
3062 struct gl_buffer_object
*dst
= *dst_ptr
;
3064 dst
->MinMaxCacheDirty
= true;
3065 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
3070 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
3071 GLintptr readOffset
, GLintptr writeOffset
,
3074 GET_CURRENT_CONTEXT(ctx
);
3075 struct gl_buffer_object
*src
, *dst
;
3077 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
3078 GL_INVALID_OPERATION
);
3082 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
3083 GL_INVALID_OPERATION
);
3087 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
3088 "glCopyBufferSubData");
3092 _mesa_NamedCopyBufferSubDataEXT(GLuint readBuffer
, GLuint writeBuffer
,
3093 GLintptr readOffset
, GLintptr writeOffset
,
3096 GET_CURRENT_CONTEXT(ctx
);
3097 struct gl_buffer_object
*src
, *dst
;
3099 src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
3100 if (!_mesa_handle_bind_buffer_gen(ctx
, readBuffer
,
3102 "glNamedCopyBufferSubDataEXT"))
3105 dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
3106 if (!_mesa_handle_bind_buffer_gen(ctx
, writeBuffer
,
3108 "glNamedCopyBufferSubDataEXT"))
3111 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
3112 "glNamedCopyBufferSubDataEXT");
3116 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer
, GLuint writeBuffer
,
3117 GLintptr readOffset
,
3118 GLintptr writeOffset
, GLsizeiptr size
)
3120 GET_CURRENT_CONTEXT(ctx
);
3122 struct gl_buffer_object
*src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
3123 struct gl_buffer_object
*dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
3125 dst
->MinMaxCacheDirty
= true;
3126 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
3131 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
3132 GLintptr readOffset
, GLintptr writeOffset
,
3135 GET_CURRENT_CONTEXT(ctx
);
3136 struct gl_buffer_object
*src
, *dst
;
3138 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
3139 "glCopyNamedBufferSubData");
3143 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
3144 "glCopyNamedBufferSubData");
3148 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
3149 "glCopyNamedBufferSubData");
3153 validate_map_buffer_range(struct gl_context
*ctx
,
3154 struct gl_buffer_object
*bufObj
, GLintptr offset
,
3155 GLsizeiptr length
, GLbitfield access
,
3158 GLbitfield allowed_access
;
3160 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, false);
3163 _mesa_error(ctx
, GL_INVALID_VALUE
,
3164 "%s(offset %ld < 0)", func
, (long) offset
);
3169 _mesa_error(ctx
, GL_INVALID_VALUE
,
3170 "%s(length %ld < 0)", func
, (long) length
);
3174 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
3176 * "An INVALID_OPERATION error is generated for any of the following
3179 * * <length> is zero."
3181 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
3182 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
3186 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
3190 allowed_access
= GL_MAP_READ_BIT
|
3192 GL_MAP_INVALIDATE_RANGE_BIT
|
3193 GL_MAP_INVALIDATE_BUFFER_BIT
|
3194 GL_MAP_FLUSH_EXPLICIT_BIT
|
3195 GL_MAP_UNSYNCHRONIZED_BIT
;
3197 if (ctx
->Extensions
.ARB_buffer_storage
) {
3198 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
3199 GL_MAP_COHERENT_BIT
;
3202 if (access
& ~allowed_access
) {
3203 /* generate an error if any bits other than those allowed are set */
3204 _mesa_error(ctx
, GL_INVALID_VALUE
,
3205 "%s(access has undefined bits set)", func
);
3209 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
3210 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3211 "%s(access indicates neither read or write)", func
);
3215 if ((access
& GL_MAP_READ_BIT
) &&
3216 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
3217 GL_MAP_INVALIDATE_BUFFER_BIT
|
3218 GL_MAP_UNSYNCHRONIZED_BIT
))) {
3219 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3220 "%s(read access with disallowed bits)", func
);
3224 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
3225 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
3226 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3227 "%s(access has flush explicit without write)", func
);
3231 if (access
& GL_MAP_READ_BIT
&&
3232 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
3233 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3234 "%s(buffer does not allow read access)", func
);
3238 if (access
& GL_MAP_WRITE_BIT
&&
3239 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
3240 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3241 "%s(buffer does not allow write access)", func
);
3245 if (access
& GL_MAP_COHERENT_BIT
&&
3246 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
3247 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3248 "%s(buffer does not allow coherent access)", func
);
3252 if (access
& GL_MAP_PERSISTENT_BIT
&&
3253 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
3254 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3255 "%s(buffer does not allow persistent access)", func
);
3259 if (offset
+ length
> bufObj
->Size
) {
3260 _mesa_error(ctx
, GL_INVALID_VALUE
,
3261 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
3262 (unsigned long) offset
, (unsigned long) length
,
3263 (unsigned long) bufObj
->Size
);
3267 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3268 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3269 "%s(buffer already mapped)", func
);
3273 if (access
& GL_MAP_WRITE_BIT
) {
3274 bufObj
->NumMapBufferWriteCalls
++;
3275 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
3276 bufObj
->Usage
== GL_STATIC_COPY
) &&
3277 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
3278 BUFFER_USAGE_WARNING(ctx
,
3279 "using %s(buffer %u, offset %u, length %u) to "
3280 "update a %s buffer",
3281 func
, bufObj
->Name
, offset
, length
,
3282 _mesa_enum_to_string(bufObj
->Usage
));
3290 map_buffer_range(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
3291 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
3294 if (!bufObj
->Size
) {
3295 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
3299 assert(ctx
->Driver
.MapBufferRange
);
3300 void *map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
3303 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
3306 /* The driver callback should have set all these fields.
3307 * This is important because other modules (like VBO) might call
3308 * the driver function directly.
3310 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
3311 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
3312 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
3313 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
3316 if (access
& GL_MAP_WRITE_BIT
) {
3317 bufObj
->Written
= GL_TRUE
;
3318 bufObj
->MinMaxCacheDirty
= true;
3322 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3323 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
3324 bufObj
->Name
, bufObj
->Size
, access
);
3325 /* Access must be write only */
3326 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
3328 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
3329 for (i
= 0; i
< bufObj
->Size
; i
++)
3336 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3337 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
3339 /* buffer is 100 bytes larger than requested, fill with magic value */
3340 for (i
= 0; i
< 100; i
++) {
3341 buf
[bufObj
->Size
- i
- 1] = 123;
3350 _mesa_MapBufferRange_no_error(GLenum target
, GLintptr offset
,
3351 GLsizeiptr length
, GLbitfield access
)
3353 GET_CURRENT_CONTEXT(ctx
);
3355 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3356 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3358 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3359 "glMapBufferRange");
3363 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
3366 GET_CURRENT_CONTEXT(ctx
);
3367 struct gl_buffer_object
*bufObj
;
3369 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3370 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3371 "glMapBufferRange(ARB_map_buffer_range not supported)");
3375 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
3379 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3380 "glMapBufferRange"))
3383 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3384 "glMapBufferRange");
3388 _mesa_MapNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3389 GLsizeiptr length
, GLbitfield access
)
3391 GET_CURRENT_CONTEXT(ctx
);
3392 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3394 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3395 "glMapNamedBufferRange");
3399 map_named_buffer_range(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3400 GLbitfield access
, bool dsa_ext
, const char *func
)
3402 GET_CURRENT_CONTEXT(ctx
);
3403 struct gl_buffer_object
*bufObj
= NULL
;
3405 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3406 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3407 "%s(ARB_map_buffer_range not supported)", func
);
3412 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3413 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
, &bufObj
, func
))
3416 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
3421 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
, func
))
3424 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
, func
);
3428 _mesa_MapNamedBufferRangeEXT(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3431 GET_CURRENT_CONTEXT(ctx
);
3433 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3434 "glMapNamedBufferRangeEXT(buffer=0)");
3437 return map_named_buffer_range(buffer
, offset
, length
, access
, true,
3438 "glMapNamedBufferRangeEXT");
3442 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3445 return map_named_buffer_range(buffer
, offset
, length
, access
, false,
3446 "glMapNamedBufferRange");
3450 * Converts GLenum access from MapBuffer and MapNamedBuffer into
3451 * flags for input to map_buffer_range.
3453 * \return true if the type of requested access is permissible.
3456 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
3460 case GL_READ_ONLY_ARB
:
3461 *flags
= GL_MAP_READ_BIT
;
3462 return _mesa_is_desktop_gl(ctx
);
3463 case GL_WRITE_ONLY_ARB
:
3464 *flags
= GL_MAP_WRITE_BIT
;
3466 case GL_READ_WRITE_ARB
:
3467 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
3468 return _mesa_is_desktop_gl(ctx
);
3476 _mesa_MapBuffer_no_error(GLenum target
, GLenum access
)
3478 GET_CURRENT_CONTEXT(ctx
);
3480 GLbitfield accessFlags
;
3481 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3483 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3484 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3486 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3491 _mesa_MapBuffer(GLenum target
, GLenum access
)
3493 GET_CURRENT_CONTEXT(ctx
);
3494 struct gl_buffer_object
*bufObj
;
3495 GLbitfield accessFlags
;
3497 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3498 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
3502 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
3506 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3510 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3515 _mesa_MapNamedBuffer_no_error(GLuint buffer
, GLenum access
)
3517 GET_CURRENT_CONTEXT(ctx
);
3519 GLbitfield accessFlags
;
3520 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3522 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3524 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3525 "glMapNamedBuffer");
3529 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
3531 GET_CURRENT_CONTEXT(ctx
);
3532 struct gl_buffer_object
*bufObj
;
3533 GLbitfield accessFlags
;
3535 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3536 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
3540 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
3544 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3545 "glMapNamedBuffer"))
3548 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3549 "glMapNamedBuffer");
3553 _mesa_MapNamedBufferEXT(GLuint buffer
, GLenum access
)
3555 GET_CURRENT_CONTEXT(ctx
);
3557 GLbitfield accessFlags
;
3559 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3560 "glMapNamedBufferEXT(buffer=0)");
3563 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3564 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBufferEXT(invalid access)");
3568 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3569 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3570 &bufObj
, "glMapNamedBufferEXT"))
3573 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3574 "glMapNamedBufferEXT"))
3577 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3578 "glMapNamedBufferEXT");
3582 flush_mapped_buffer_range(struct gl_context
*ctx
,
3583 struct gl_buffer_object
*bufObj
,
3584 GLintptr offset
, GLsizeiptr length
,
3587 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3588 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3589 "%s(ARB_map_buffer_range not supported)", func
);
3594 _mesa_error(ctx
, GL_INVALID_VALUE
,
3595 "%s(offset %ld < 0)", func
, (long) offset
);
3600 _mesa_error(ctx
, GL_INVALID_VALUE
,
3601 "%s(length %ld < 0)", func
, (long) length
);
3605 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3606 /* buffer is not mapped */
3607 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3608 "%s(buffer is not mapped)", func
);
3612 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
3613 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
3614 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3615 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
3619 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
3620 _mesa_error(ctx
, GL_INVALID_VALUE
,
3621 "%s(offset %ld + length %ld > mapped length %ld)", func
,
3622 (long) offset
, (long) length
,
3623 (long) bufObj
->Mappings
[MAP_USER
].Length
);
3627 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
3629 if (ctx
->Driver
.FlushMappedBufferRange
)
3630 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3635 _mesa_FlushMappedBufferRange_no_error(GLenum target
, GLintptr offset
,
3638 GET_CURRENT_CONTEXT(ctx
);
3639 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3640 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3642 if (ctx
->Driver
.FlushMappedBufferRange
)
3643 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3648 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
3651 GET_CURRENT_CONTEXT(ctx
);
3652 struct gl_buffer_object
*bufObj
;
3654 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
3655 GL_INVALID_OPERATION
);
3659 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3660 "glFlushMappedBufferRange");
3664 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3667 GET_CURRENT_CONTEXT(ctx
);
3668 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3670 if (ctx
->Driver
.FlushMappedBufferRange
)
3671 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3676 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
3679 GET_CURRENT_CONTEXT(ctx
);
3680 struct gl_buffer_object
*bufObj
;
3682 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
3683 "glFlushMappedNamedBufferRange");
3687 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3688 "glFlushMappedNamedBufferRange");
3692 _mesa_FlushMappedNamedBufferRangeEXT(GLuint buffer
, GLintptr offset
,
3695 GET_CURRENT_CONTEXT(ctx
);
3696 struct gl_buffer_object
*bufObj
;
3699 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3700 "glFlushMappedNamedBufferRangeEXT(buffer=0)");
3704 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3705 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3706 &bufObj
, "glFlushMappedNamedBufferRangeEXT"))
3709 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3710 "glFlushMappedNamedBufferRangeEXT");
3714 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
, GLuint index
,
3715 struct gl_buffer_object
*bufObj
,
3716 GLintptr offset
, GLsizeiptr size
)
3723 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3724 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3728 * Bind a region of a buffer object to a uniform block binding point.
3729 * \param index the uniform buffer binding point index
3730 * \param bufObj the buffer object
3731 * \param offset offset to the start of buffer object region
3732 * \param size size of the buffer object region
3735 bind_buffer_range_uniform_buffer_err(struct gl_context
*ctx
, GLuint index
,
3736 struct gl_buffer_object
*bufObj
,
3737 GLintptr offset
, GLsizeiptr size
)
3739 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3740 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3744 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3745 _mesa_error(ctx
, GL_INVALID_VALUE
,
3746 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3747 ctx
->Const
.UniformBufferOffsetAlignment
);
3751 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3755 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3757 struct gl_buffer_object
*bufObj
,
3766 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3767 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3771 * Bind a region of a buffer object to a shader storage block binding point.
3772 * \param index the shader storage buffer binding point index
3773 * \param bufObj the buffer object
3774 * \param offset offset to the start of buffer object region
3775 * \param size size of the buffer object region
3778 bind_buffer_range_shader_storage_buffer_err(struct gl_context
*ctx
,
3780 struct gl_buffer_object
*bufObj
,
3781 GLintptr offset
, GLsizeiptr size
)
3783 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3784 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3788 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3789 _mesa_error(ctx
, GL_INVALID_VALUE
,
3790 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3791 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3795 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3799 bind_buffer_range_atomic_buffer(struct gl_context
*ctx
, GLuint index
,
3800 struct gl_buffer_object
*bufObj
,
3801 GLintptr offset
, GLsizeiptr size
)
3808 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3809 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3813 * Bind a region of a buffer object to an atomic storage block binding point.
3814 * \param index the shader storage buffer binding point index
3815 * \param bufObj the buffer object
3816 * \param offset offset to the start of buffer object region
3817 * \param size size of the buffer object region
3820 bind_buffer_range_atomic_buffer_err(struct gl_context
*ctx
,
3822 struct gl_buffer_object
*bufObj
,
3823 GLintptr offset
, GLsizeiptr size
)
3825 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3826 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3830 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3831 _mesa_error(ctx
, GL_INVALID_VALUE
,
3832 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3833 ATOMIC_COUNTER_SIZE
);
3837 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
3841 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3843 const GLintptr
*offsets
,
3844 const GLsizeiptr
*sizes
)
3846 if (offsets
[index
] < 0) {
3847 /* The ARB_multi_bind spec says:
3849 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3850 * value in <offsets> is less than zero (per binding)."
3852 _mesa_error(ctx
, GL_INVALID_VALUE
,
3853 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3854 index
, (int64_t) offsets
[index
]);
3858 if (sizes
[index
] <= 0) {
3859 /* The ARB_multi_bind spec says:
3861 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3862 * value in <sizes> is less than or equal to zero (per binding)."
3864 _mesa_error(ctx
, GL_INVALID_VALUE
,
3865 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3866 index
, (int64_t) sizes
[index
]);
3874 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3875 GLuint first
, GLsizei count
,
3878 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3879 _mesa_error(ctx
, GL_INVALID_ENUM
,
3880 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3884 /* The ARB_multi_bind_spec says:
3886 * "An INVALID_OPERATION error is generated if <first> + <count> is
3887 * greater than the number of target-specific indexed binding points,
3888 * as described in section 6.7.1."
3890 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3891 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3892 "%s(first=%u + count=%d > the value of "
3893 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3894 caller
, first
, count
,
3895 ctx
->Const
.MaxUniformBufferBindings
);
3903 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3904 GLuint first
, GLsizei count
,
3907 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3908 _mesa_error(ctx
, GL_INVALID_ENUM
,
3909 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3913 /* The ARB_multi_bind_spec says:
3915 * "An INVALID_OPERATION error is generated if <first> + <count> is
3916 * greater than the number of target-specific indexed binding points,
3917 * as described in section 6.7.1."
3919 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3920 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3921 "%s(first=%u + count=%d > the value of "
3922 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3923 caller
, first
, count
,
3924 ctx
->Const
.MaxShaderStorageBufferBindings
);
3932 * Unbind all uniform buffers in the range
3933 * <first> through <first>+<count>-1
3936 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3938 for (int i
= 0; i
< count
; i
++)
3939 set_buffer_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3940 NULL
, -1, -1, GL_TRUE
, 0);
3944 * Unbind all shader storage buffers in the range
3945 * <first> through <first>+<count>-1
3948 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3951 for (int i
= 0; i
< count
; i
++)
3952 set_buffer_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3953 NULL
, -1, -1, GL_TRUE
, 0);
3957 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3958 const GLuint
*buffers
,
3960 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3963 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3966 /* Assume that at least one binding will be changed */
3967 FLUSH_VERTICES(ctx
, 0);
3968 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3971 /* The ARB_multi_bind spec says:
3973 * "If <buffers> is NULL, all bindings from <first> through
3974 * <first>+<count>-1 are reset to their unbound (zero) state.
3975 * In this case, the offsets and sizes associated with the
3976 * binding points are set to default values, ignoring
3977 * <offsets> and <sizes>."
3979 unbind_uniform_buffers(ctx
, first
, count
);
3983 /* Note that the error semantics for multi-bind commands differ from
3984 * those of other GL commands.
3986 * The Issues section in the ARB_multi_bind spec says:
3988 * "(11) Typically, OpenGL specifies that if an error is generated by a
3989 * command, that command has no effect. This is somewhat
3990 * unfortunate for multi-bind commands, because it would require a
3991 * first pass to scan the entire list of bound objects for errors
3992 * and then a second pass to actually perform the bindings.
3993 * Should we have different error semantics?
3995 * RESOLVED: Yes. In this specification, when the parameters for
3996 * one of the <count> binding points are invalid, that binding point
3997 * is not updated and an error will be generated. However, other
3998 * binding points in the same command will be updated if their
3999 * parameters are valid and no other error occurs."
4002 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4004 for (int i
= 0; i
< count
; i
++) {
4005 struct gl_buffer_binding
*binding
=
4006 &ctx
->UniformBufferBindings
[first
+ i
];
4007 GLintptr offset
= 0;
4008 GLsizeiptr size
= 0;
4011 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4014 /* The ARB_multi_bind spec says:
4016 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4017 * pair of values in <offsets> and <sizes> does not respectively
4018 * satisfy the constraints described for those parameters for the
4019 * specified target, as described in section 6.7.1 (per binding)."
4021 * Section 6.7.1 refers to table 6.5, which says:
4023 * "┌───────────────────────────────────────────────────────────────┐
4024 * │ Uniform buffer array bindings (see sec. 7.6) │
4025 * ├─────────────────────┬─────────────────────────────────────────┤
4027 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
4028 * │ │ OFFSET_ALIGNMENT │
4030 * │ size restriction │ none │
4031 * └─────────────────────┴─────────────────────────────────────────┘"
4033 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
4034 _mesa_error(ctx
, GL_INVALID_VALUE
,
4035 "glBindBuffersRange(offsets[%u]=%" PRId64
4036 " is misaligned; it must be a multiple of the value of "
4037 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
4038 "target=GL_UNIFORM_BUFFER)",
4039 i
, (int64_t) offsets
[i
],
4040 ctx
->Const
.UniformBufferOffsetAlignment
);
4044 offset
= offsets
[i
];
4048 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4049 binding
, offset
, size
, range
,
4050 USAGE_UNIFORM_BUFFER
);
4053 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4057 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
4058 GLsizei count
, const GLuint
*buffers
,
4060 const GLintptr
*offsets
,
4061 const GLsizeiptr
*sizes
,
4064 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
4067 /* Assume that at least one binding will be changed */
4068 FLUSH_VERTICES(ctx
, 0);
4069 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
4072 /* The ARB_multi_bind spec says:
4074 * "If <buffers> is NULL, all bindings from <first> through
4075 * <first>+<count>-1 are reset to their unbound (zero) state.
4076 * In this case, the offsets and sizes associated with the
4077 * binding points are set to default values, ignoring
4078 * <offsets> and <sizes>."
4080 unbind_shader_storage_buffers(ctx
, first
, count
);
4084 /* Note that the error semantics for multi-bind commands differ from
4085 * those of other GL commands.
4087 * The Issues section in the ARB_multi_bind spec says:
4089 * "(11) Typically, OpenGL specifies that if an error is generated by a
4090 * command, that command has no effect. This is somewhat
4091 * unfortunate for multi-bind commands, because it would require a
4092 * first pass to scan the entire list of bound objects for errors
4093 * and then a second pass to actually perform the bindings.
4094 * Should we have different error semantics?
4096 * RESOLVED: Yes. In this specification, when the parameters for
4097 * one of the <count> binding points are invalid, that binding point
4098 * is not updated and an error will be generated. However, other
4099 * binding points in the same command will be updated if their
4100 * parameters are valid and no other error occurs."
4103 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4105 for (int i
= 0; i
< count
; i
++) {
4106 struct gl_buffer_binding
*binding
=
4107 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
4108 GLintptr offset
= 0;
4109 GLsizeiptr size
= 0;
4112 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4115 /* The ARB_multi_bind spec says:
4117 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4118 * pair of values in <offsets> and <sizes> does not respectively
4119 * satisfy the constraints described for those parameters for the
4120 * specified target, as described in section 6.7.1 (per binding)."
4122 * Section 6.7.1 refers to table 6.5, which says:
4124 * "┌───────────────────────────────────────────────────────────────┐
4125 * │ Shader storage buffer array bindings (see sec. 7.8) │
4126 * ├─────────────────────┬─────────────────────────────────────────┤
4128 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
4129 * │ │ BUFFER_OFFSET_ALIGNMENT │
4131 * │ size restriction │ none │
4132 * └─────────────────────┴─────────────────────────────────────────┘"
4134 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
4135 _mesa_error(ctx
, GL_INVALID_VALUE
,
4136 "glBindBuffersRange(offsets[%u]=%" PRId64
4137 " is misaligned; it must be a multiple of the value of "
4138 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
4139 "target=GL_SHADER_STORAGE_BUFFER)",
4140 i
, (int64_t) offsets
[i
],
4141 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
4145 offset
= offsets
[i
];
4149 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4150 binding
, offset
, size
, range
,
4151 USAGE_SHADER_STORAGE_BUFFER
);
4154 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4158 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
4159 struct gl_transform_feedback_object
*tfObj
,
4160 GLuint first
, GLsizei count
, const char *caller
)
4162 if (!ctx
->Extensions
.EXT_transform_feedback
) {
4163 _mesa_error(ctx
, GL_INVALID_ENUM
,
4164 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
4168 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
4170 * "An INVALID_OPERATION error is generated :
4173 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
4174 * FEEDBACK_BUFFER and transform feedback is currently active."
4176 * We assume that this is also meant to apply to BindBuffersRange
4177 * and BindBuffersBase.
4179 if (tfObj
->Active
) {
4180 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4181 "%s(Changing transform feedback buffers while "
4182 "transform feedback is active)", caller
);
4186 /* The ARB_multi_bind_spec says:
4188 * "An INVALID_OPERATION error is generated if <first> + <count> is
4189 * greater than the number of target-specific indexed binding points,
4190 * as described in section 6.7.1."
4192 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
4193 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4194 "%s(first=%u + count=%d > the value of "
4195 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
4196 caller
, first
, count
,
4197 ctx
->Const
.MaxTransformFeedbackBuffers
);
4205 * Unbind all transform feedback buffers in the range
4206 * <first> through <first>+<count>-1
4209 unbind_xfb_buffers(struct gl_context
*ctx
,
4210 struct gl_transform_feedback_object
*tfObj
,
4211 GLuint first
, GLsizei count
)
4213 for (int i
= 0; i
< count
; i
++)
4214 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
4219 bind_xfb_buffers(struct gl_context
*ctx
,
4220 GLuint first
, GLsizei count
,
4221 const GLuint
*buffers
,
4223 const GLintptr
*offsets
,
4224 const GLsizeiptr
*sizes
,
4227 struct gl_transform_feedback_object
*tfObj
=
4228 ctx
->TransformFeedback
.CurrentObject
;
4230 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
4233 /* Assume that at least one binding will be changed */
4234 FLUSH_VERTICES(ctx
, 0);
4235 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
4238 /* The ARB_multi_bind spec says:
4240 * "If <buffers> is NULL, all bindings from <first> through
4241 * <first>+<count>-1 are reset to their unbound (zero) state.
4242 * In this case, the offsets and sizes associated with the
4243 * binding points are set to default values, ignoring
4244 * <offsets> and <sizes>."
4246 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
4250 /* Note that the error semantics for multi-bind commands differ from
4251 * those of other GL commands.
4253 * The Issues section in the ARB_multi_bind spec says:
4255 * "(11) Typically, OpenGL specifies that if an error is generated by a
4256 * command, that command has no effect. This is somewhat
4257 * unfortunate for multi-bind commands, because it would require a
4258 * first pass to scan the entire list of bound objects for errors
4259 * and then a second pass to actually perform the bindings.
4260 * Should we have different error semantics?
4262 * RESOLVED: Yes. In this specification, when the parameters for
4263 * one of the <count> binding points are invalid, that binding point
4264 * is not updated and an error will be generated. However, other
4265 * binding points in the same command will be updated if their
4266 * parameters are valid and no other error occurs."
4269 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4271 for (int i
= 0; i
< count
; i
++) {
4272 const GLuint index
= first
+ i
;
4273 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
4274 struct gl_buffer_object
*bufObj
;
4275 GLintptr offset
= 0;
4276 GLsizeiptr size
= 0;
4279 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4282 /* The ARB_multi_bind spec says:
4284 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4285 * pair of values in <offsets> and <sizes> does not respectively
4286 * satisfy the constraints described for those parameters for the
4287 * specified target, as described in section 6.7.1 (per binding)."
4289 * Section 6.7.1 refers to table 6.5, which says:
4291 * "┌───────────────────────────────────────────────────────────────┐
4292 * │ Transform feedback array bindings (see sec. 13.2.2) │
4293 * ├───────────────────────┬───────────────────────────────────────┤
4295 * │ offset restriction │ multiple of 4 │
4297 * │ size restriction │ multiple of 4 │
4298 * └───────────────────────┴───────────────────────────────────────┘"
4300 if (offsets
[i
] & 0x3) {
4301 _mesa_error(ctx
, GL_INVALID_VALUE
,
4302 "glBindBuffersRange(offsets[%u]=%" PRId64
4303 " is misaligned; it must be a multiple of 4 when "
4304 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4305 i
, (int64_t) offsets
[i
]);
4309 if (sizes
[i
] & 0x3) {
4310 _mesa_error(ctx
, GL_INVALID_VALUE
,
4311 "glBindBuffersRange(sizes[%u]=%" PRId64
4312 " is misaligned; it must be a multiple of 4 when "
4313 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4314 i
, (int64_t) sizes
[i
]);
4318 offset
= offsets
[i
];
4322 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
4323 bufObj
= boundBufObj
;
4326 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
,
4332 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
4336 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4340 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
4341 GLuint first
, GLsizei count
,
4344 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
4345 _mesa_error(ctx
, GL_INVALID_ENUM
,
4346 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
4350 /* The ARB_multi_bind_spec says:
4352 * "An INVALID_OPERATION error is generated if <first> + <count> is
4353 * greater than the number of target-specific indexed binding points,
4354 * as described in section 6.7.1."
4356 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
4357 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4358 "%s(first=%u + count=%d > the value of "
4359 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4360 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
4368 * Unbind all atomic counter buffers in the range
4369 * <first> through <first>+<count>-1
4372 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
4374 for (int i
= 0; i
< count
; i
++)
4375 set_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
4376 NULL
, -1, -1, GL_TRUE
, 0);
4380 bind_atomic_buffers(struct gl_context
*ctx
,
4383 const GLuint
*buffers
,
4385 const GLintptr
*offsets
,
4386 const GLsizeiptr
*sizes
,
4389 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
4392 /* Assume that at least one binding will be changed */
4393 FLUSH_VERTICES(ctx
, 0);
4394 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
4397 /* The ARB_multi_bind spec says:
4399 * "If <buffers> is NULL, all bindings from <first> through
4400 * <first>+<count>-1 are reset to their unbound (zero) state.
4401 * In this case, the offsets and sizes associated with the
4402 * binding points are set to default values, ignoring
4403 * <offsets> and <sizes>."
4405 unbind_atomic_buffers(ctx
, first
, count
);
4409 /* Note that the error semantics for multi-bind commands differ from
4410 * those of other GL commands.
4412 * The Issues section in the ARB_multi_bind spec says:
4414 * "(11) Typically, OpenGL specifies that if an error is generated by a
4415 * command, that command has no effect. This is somewhat
4416 * unfortunate for multi-bind commands, because it would require a
4417 * first pass to scan the entire list of bound objects for errors
4418 * and then a second pass to actually perform the bindings.
4419 * Should we have different error semantics?
4421 * RESOLVED: Yes. In this specification, when the parameters for
4422 * one of the <count> binding points are invalid, that binding point
4423 * is not updated and an error will be generated. However, other
4424 * binding points in the same command will be updated if their
4425 * parameters are valid and no other error occurs."
4428 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4430 for (int i
= 0; i
< count
; i
++) {
4431 struct gl_buffer_binding
*binding
=
4432 &ctx
->AtomicBufferBindings
[first
+ i
];
4433 GLintptr offset
= 0;
4434 GLsizeiptr size
= 0;
4437 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4440 /* The ARB_multi_bind spec says:
4442 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4443 * pair of values in <offsets> and <sizes> does not respectively
4444 * satisfy the constraints described for those parameters for the
4445 * specified target, as described in section 6.7.1 (per binding)."
4447 * Section 6.7.1 refers to table 6.5, which says:
4449 * "┌───────────────────────────────────────────────────────────────┐
4450 * │ Atomic counter array bindings (see sec. 7.7.2) │
4451 * ├───────────────────────┬───────────────────────────────────────┤
4453 * │ offset restriction │ multiple of 4 │
4455 * │ size restriction │ none │
4456 * └───────────────────────┴───────────────────────────────────────┘"
4458 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
4459 _mesa_error(ctx
, GL_INVALID_VALUE
,
4460 "glBindBuffersRange(offsets[%u]=%" PRId64
4461 " is misaligned; it must be a multiple of %d when "
4462 "target=GL_ATOMIC_COUNTER_BUFFER)",
4463 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
4467 offset
= offsets
[i
];
4471 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4472 binding
, offset
, size
, range
,
4473 USAGE_ATOMIC_COUNTER_BUFFER
);
4476 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4479 static ALWAYS_INLINE
void
4480 bind_buffer_range(GLenum target
, GLuint index
, GLuint buffer
, GLintptr offset
,
4481 GLsizeiptr size
, bool no_error
)
4483 GET_CURRENT_CONTEXT(ctx
);
4484 struct gl_buffer_object
*bufObj
;
4486 if (MESA_VERBOSE
& VERBOSE_API
) {
4487 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4488 _mesa_enum_to_string(target
), index
, buffer
,
4489 (unsigned long) offset
, (unsigned long) size
);
4495 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4496 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4497 &bufObj
, "glBindBufferRange"))
4500 if (!no_error
&& !bufObj
) {
4501 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4502 "glBindBufferRange(invalid buffer=%u)", buffer
);
4509 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4510 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4511 index
, bufObj
, offset
, size
);
4513 case GL_UNIFORM_BUFFER
:
4514 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
4516 case GL_SHADER_STORAGE_BUFFER
:
4517 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
,
4520 case GL_ATOMIC_COUNTER_BUFFER
:
4521 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
4524 unreachable("invalid BindBufferRange target with KHR_no_error");
4529 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
4536 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4537 if (!_mesa_validate_buffer_range_xfb(ctx
,
4538 ctx
->TransformFeedback
.CurrentObject
,
4539 index
, bufObj
, offset
, size
,
4543 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4544 index
, bufObj
, offset
, size
);
4546 case GL_UNIFORM_BUFFER
:
4547 bind_buffer_range_uniform_buffer_err(ctx
, index
, bufObj
, offset
,
4550 case GL_SHADER_STORAGE_BUFFER
:
4551 bind_buffer_range_shader_storage_buffer_err(ctx
, index
, bufObj
,
4554 case GL_ATOMIC_COUNTER_BUFFER
:
4555 bind_buffer_range_atomic_buffer_err(ctx
, index
, bufObj
,
4559 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
4566 _mesa_BindBufferRange_no_error(GLenum target
, GLuint index
, GLuint buffer
,
4567 GLintptr offset
, GLsizeiptr size
)
4569 bind_buffer_range(target
, index
, buffer
, offset
, size
, true);
4573 _mesa_BindBufferRange(GLenum target
, GLuint index
,
4574 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
4576 bind_buffer_range(target
, index
, buffer
, offset
, size
, false);
4580 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
4582 GET_CURRENT_CONTEXT(ctx
);
4583 struct gl_buffer_object
*bufObj
;
4585 if (MESA_VERBOSE
& VERBOSE_API
) {
4586 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
4587 _mesa_enum_to_string(target
), index
, buffer
);
4593 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4594 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4595 &bufObj
, "glBindBufferBase"))
4599 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4600 "glBindBufferBase(invalid buffer=%u)", buffer
);
4605 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4606 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4608 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4609 * zero and size equal to the size of buffer."
4611 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4613 * "If the parameter (starting offset or size) was not specified when the
4614 * buffer object was bound, zero is returned."
4616 * What happens if the size of the buffer changes? Does the size of the
4617 * buffer at the moment glBindBufferBase was called still play a role, like
4618 * the first quote would imply, or is the size meaningless in the
4619 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4620 * core spec page 45 says:
4622 * "It is equivalent to calling BindBufferRange with offset zero, while
4623 * size is determined by the size of the bound buffer at the time the
4626 * My interpretation is that the GL 4.1 spec was a clarification of the
4627 * behavior, not a change. In particular, this choice will only make
4628 * rendering work in cases where it would have had undefined results.
4632 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4633 _mesa_bind_buffer_base_transform_feedback(ctx
,
4634 ctx
->TransformFeedback
.CurrentObject
,
4635 index
, bufObj
, false);
4637 case GL_UNIFORM_BUFFER
:
4638 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4640 case GL_SHADER_STORAGE_BUFFER
:
4641 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4643 case GL_ATOMIC_COUNTER_BUFFER
:
4644 bind_buffer_base_atomic_buffer(ctx
, index
, bufObj
);
4647 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4653 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4654 const GLuint
*buffers
,
4655 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4657 GET_CURRENT_CONTEXT(ctx
);
4659 if (MESA_VERBOSE
& VERBOSE_API
) {
4660 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4661 _mesa_enum_to_string(target
), first
, count
,
4662 buffers
, offsets
, sizes
);
4666 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4667 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4668 "glBindBuffersRange");
4670 case GL_UNIFORM_BUFFER
:
4671 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4672 "glBindBuffersRange");
4674 case GL_SHADER_STORAGE_BUFFER
:
4675 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4676 "glBindBuffersRange");
4678 case GL_ATOMIC_COUNTER_BUFFER
:
4679 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4680 "glBindBuffersRange");
4683 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4684 _mesa_enum_to_string(target
));
4690 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4691 const GLuint
*buffers
)
4693 GET_CURRENT_CONTEXT(ctx
);
4695 if (MESA_VERBOSE
& VERBOSE_API
) {
4696 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4697 _mesa_enum_to_string(target
), first
, count
, buffers
);
4701 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4702 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4703 "glBindBuffersBase");
4705 case GL_UNIFORM_BUFFER
:
4706 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4707 "glBindBuffersBase");
4709 case GL_SHADER_STORAGE_BUFFER
:
4710 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4711 "glBindBuffersBase");
4713 case GL_ATOMIC_COUNTER_BUFFER
:
4714 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4715 "glBindBuffersBase");
4718 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4719 _mesa_enum_to_string(target
));
4724 static ALWAYS_INLINE
void
4725 invalidate_buffer_subdata(struct gl_context
*ctx
,
4726 struct gl_buffer_object
*bufObj
, GLintptr offset
,
4729 if (ctx
->Driver
.InvalidateBufferSubData
)
4730 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4734 _mesa_InvalidateBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
4737 GET_CURRENT_CONTEXT(ctx
);
4739 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4740 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4744 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4747 GET_CURRENT_CONTEXT(ctx
);
4748 struct gl_buffer_object
*bufObj
;
4749 const GLintptr end
= offset
+ length
;
4751 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4752 * Profile) spec says:
4754 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4755 * name of an existing buffer object."
4757 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4758 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4759 _mesa_error(ctx
, GL_INVALID_VALUE
,
4760 "glInvalidateBufferSubData(name = %u) invalid object",
4765 /* The GL_ARB_invalidate_subdata spec says:
4767 * "An INVALID_VALUE error is generated if <offset> or <length> is
4768 * negative, or if <offset> + <length> is greater than the value of
4771 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
4772 _mesa_error(ctx
, GL_INVALID_VALUE
,
4773 "glInvalidateBufferSubData(invalid offset or length)");
4777 /* The OpenGL 4.4 (Core Profile) spec says:
4779 * "An INVALID_OPERATION error is generated if buffer is currently
4780 * mapped by MapBuffer or if the invalidate range intersects the range
4781 * currently mapped by MapBufferRange, unless it was mapped
4782 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4784 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4785 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4786 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4787 "glInvalidateBufferSubData(intersection with mapped "
4792 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4796 _mesa_InvalidateBufferData_no_error(GLuint buffer
)
4798 GET_CURRENT_CONTEXT(ctx
);
4800 struct gl_buffer_object
*bufObj
=_mesa_lookup_bufferobj(ctx
, buffer
);
4801 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4805 _mesa_InvalidateBufferData(GLuint buffer
)
4807 GET_CURRENT_CONTEXT(ctx
);
4808 struct gl_buffer_object
*bufObj
;
4810 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4811 * Profile) spec says:
4813 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4814 * name of an existing buffer object."
4816 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4817 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4818 _mesa_error(ctx
, GL_INVALID_VALUE
,
4819 "glInvalidateBufferData(name = %u) invalid object",
4824 /* The OpenGL 4.4 (Core Profile) spec says:
4826 * "An INVALID_OPERATION error is generated if buffer is currently
4827 * mapped by MapBuffer or if the invalidate range intersects the range
4828 * currently mapped by MapBufferRange, unless it was mapped
4829 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4831 if (_mesa_check_disallowed_mapping(bufObj
)) {
4832 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4833 "glInvalidateBufferData(intersection with mapped "
4838 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4842 buffer_page_commitment(struct gl_context
*ctx
,
4843 struct gl_buffer_object
*bufferObj
,
4844 GLintptr offset
, GLsizeiptr size
,
4845 GLboolean commit
, const char *func
)
4847 if (!(bufferObj
->StorageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)) {
4848 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(not a sparse buffer object)",
4853 if (size
< 0 || size
> bufferObj
->Size
||
4854 offset
< 0 || offset
> bufferObj
->Size
- size
) {
4855 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(out of bounds)",
4860 /* The GL_ARB_sparse_buffer extension specification says:
4862 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4863 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4864 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4865 * not extend to the end of the buffer's data store."
4867 if (offset
% ctx
->Const
.SparseBufferPageSize
!= 0) {
4868 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset not aligned to page size)",
4873 if (size
% ctx
->Const
.SparseBufferPageSize
!= 0 &&
4874 offset
+ size
!= bufferObj
->Size
) {
4875 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size not aligned to page size)",
4880 ctx
->Driver
.BufferPageCommitment(ctx
, bufferObj
, offset
, size
, commit
);
4884 _mesa_BufferPageCommitmentARB(GLenum target
, GLintptr offset
, GLsizeiptr size
,
4887 GET_CURRENT_CONTEXT(ctx
);
4888 struct gl_buffer_object
*bufferObj
;
4890 bufferObj
= get_buffer(ctx
, "glBufferPageCommitmentARB", target
,
4895 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4896 "glBufferPageCommitmentARB");
4900 _mesa_NamedBufferPageCommitmentARB(GLuint buffer
, GLintptr offset
,
4901 GLsizeiptr size
, GLboolean commit
)
4903 GET_CURRENT_CONTEXT(ctx
);
4904 struct gl_buffer_object
*bufferObj
;
4906 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4907 if (!bufferObj
|| bufferObj
== &DummyBufferObject
) {
4908 /* Note: the extension spec is not clear about the excpected error value. */
4909 _mesa_error(ctx
, GL_INVALID_VALUE
,
4910 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4915 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4916 "glNamedBufferPageCommitmentARB");
4920 _mesa_NamedBufferPageCommitmentEXT(GLuint buffer
, GLintptr offset
,
4921 GLsizeiptr size
, GLboolean commit
)
4923 GET_CURRENT_CONTEXT(ctx
);
4924 struct gl_buffer_object
*bufferObj
;
4926 /* Use NamedBuffer* functions logic from EXT_direct_state_access */
4928 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4929 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
, &bufferObj
,
4930 "glNamedBufferPageCommitmentEXT"))
4933 /* GL_EXT_direct_state_access says about NamedBuffer* functions:
4935 * There is no buffer corresponding to the name zero, these commands
4936 * generate the INVALID_OPERATION error if the buffer parameter is
4939 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4940 "glNamedBufferPageCommitmentEXT(buffer = 0)");
4943 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4944 "glNamedBufferPageCommitmentEXT");