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"
41 #include "bufferobj.h"
42 #include "externalobjects.h"
45 #include "glformats.h"
47 #include "transformfeedback.h"
53 /*#define BOUNDS_CHECK*/
57 * We count the number of buffer modification calls to check for
58 * inefficient buffer use. This is the number of such calls before we
61 #define BUFFER_WARNING_CALL_COUNT 4
65 * Helper to warn of possible performance issues, such as frequently
66 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
70 buffer_usage_warning(struct gl_context
*ctx
, GLuint
*id
, const char *fmt
, ...)
75 _mesa_gl_vdebug(ctx
, id
,
76 MESA_DEBUG_SOURCE_API
,
77 MESA_DEBUG_TYPE_PERFORMANCE
,
78 MESA_DEBUG_SEVERITY_MEDIUM
,
83 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
85 static GLuint id = 0; \
86 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
91 * Used as a placeholder for buffer objects between glGenBuffers() and
92 * glBindBuffer() so that glIsBuffer() can work correctly.
94 static struct gl_buffer_object DummyBufferObject
;
98 * Return pointer to address of a buffer object target.
99 * \param ctx the GL context
100 * \param target the buffer object target to be retrieved.
101 * \return pointer to pointer to the buffer object bound to \c target in the
102 * specified context or \c NULL if \c target is invalid.
104 static inline struct gl_buffer_object
**
105 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
107 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
109 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
110 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
114 case GL_ARRAY_BUFFER_ARB
:
115 return &ctx
->Array
.ArrayBufferObj
;
116 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
117 return &ctx
->Array
.VAO
->IndexBufferObj
;
118 case GL_PIXEL_PACK_BUFFER_EXT
:
119 return &ctx
->Pack
.BufferObj
;
120 case GL_PIXEL_UNPACK_BUFFER_EXT
:
121 return &ctx
->Unpack
.BufferObj
;
122 case GL_COPY_READ_BUFFER
:
123 return &ctx
->CopyReadBuffer
;
124 case GL_COPY_WRITE_BUFFER
:
125 return &ctx
->CopyWriteBuffer
;
126 case GL_QUERY_BUFFER
:
127 if (_mesa_has_ARB_query_buffer_object(ctx
))
128 return &ctx
->QueryBuffer
;
130 case GL_DRAW_INDIRECT_BUFFER
:
131 if ((ctx
->API
== API_OPENGL_CORE
&&
132 ctx
->Extensions
.ARB_draw_indirect
) ||
133 _mesa_is_gles31(ctx
)) {
134 return &ctx
->DrawIndirectBuffer
;
137 case GL_PARAMETER_BUFFER_ARB
:
138 if (_mesa_has_ARB_indirect_parameters(ctx
)) {
139 return &ctx
->ParameterBuffer
;
142 case GL_DISPATCH_INDIRECT_BUFFER
:
143 if (_mesa_has_compute_shaders(ctx
)) {
144 return &ctx
->DispatchIndirectBuffer
;
147 case GL_TRANSFORM_FEEDBACK_BUFFER
:
148 if (ctx
->Extensions
.EXT_transform_feedback
) {
149 return &ctx
->TransformFeedback
.CurrentBuffer
;
152 case GL_TEXTURE_BUFFER
:
153 if (_mesa_has_ARB_texture_buffer_object(ctx
) ||
154 _mesa_has_OES_texture_buffer(ctx
)) {
155 return &ctx
->Texture
.BufferObject
;
158 case GL_UNIFORM_BUFFER
:
159 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
160 return &ctx
->UniformBuffer
;
163 case GL_SHADER_STORAGE_BUFFER
:
164 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
165 return &ctx
->ShaderStorageBuffer
;
168 case GL_ATOMIC_COUNTER_BUFFER
:
169 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
170 return &ctx
->AtomicBuffer
;
173 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
174 if (ctx
->Extensions
.AMD_pinned_memory
) {
175 return &ctx
->ExternalVirtualMemoryBuffer
;
186 * Get the buffer object bound to the specified target in a GL context.
187 * \param ctx the GL context
188 * \param target the buffer object target to be retrieved.
189 * \param error the GL error to record if target is illegal.
190 * \return pointer to the buffer object bound to \c target in the
191 * specified context or \c NULL if \c target is invalid.
193 static inline struct gl_buffer_object
*
194 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
197 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
200 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
204 if (!_mesa_is_bufferobj(*bufObj
)) {
205 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
214 * Convert a GLbitfield describing the mapped buffer access flags
215 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
218 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
220 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
221 if ((access
& rwFlags
) == rwFlags
)
222 return GL_READ_WRITE
;
223 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
225 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
226 return GL_WRITE_ONLY
;
228 /* Otherwise, AccessFlags is zero (the default state).
230 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
232 * Name Type Initial Value Legal Values
234 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
237 * However, table 6.8 in the GL_OES_mapbuffer extension says:
239 * Get Value Type Get Command Value Description
240 * --------- ---- ----------- ----- -----------
241 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
243 * The difference is because GL_OES_mapbuffer only supports mapping buffers
248 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
253 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
255 * The regions do not overlap if and only if the end of the given
256 * region is before the mapped region or the start of the given region
257 * is after the mapped region.
259 * \param obj Buffer object target on which to operate.
260 * \param offset Offset of the first byte of the subdata range.
261 * \param size Size, in bytes, of the subdata range.
262 * \return true if ranges overlap, false otherwise
266 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
267 GLintptr offset
, GLsizeiptr size
)
269 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
270 const GLintptr end
= offset
+ size
;
271 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
272 obj
->Mappings
[MAP_USER
].Length
;
274 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
283 * Tests the subdata range parameters and sets the GL error code for
284 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
285 * \c glClearBufferSubData.
287 * \param ctx GL context.
288 * \param bufObj The buffer object.
289 * \param offset Offset of the first byte of the subdata range.
290 * \param size Size, in bytes, of the subdata range.
291 * \param mappedRange If true, checks if an overlapping range is mapped.
292 * If false, checks if buffer is mapped.
293 * \param caller Name of calling function for recording errors.
294 * \return false if error, true otherwise
296 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
299 buffer_object_subdata_range_good(struct gl_context
*ctx
,
300 const struct gl_buffer_object
*bufObj
,
301 GLintptr offset
, GLsizeiptr size
,
302 bool mappedRange
, const char *caller
)
305 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
310 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
314 if (offset
+ size
> bufObj
->Size
) {
315 _mesa_error(ctx
, GL_INVALID_VALUE
,
316 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
317 (unsigned long) offset
,
318 (unsigned long) size
,
319 (unsigned long) bufObj
->Size
);
323 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
327 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
328 _mesa_error(ctx
, GL_INVALID_OPERATION
,
329 "%s(range is mapped without persistent bit)",
335 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
336 _mesa_error(ctx
, GL_INVALID_OPERATION
,
337 "%s(buffer is mapped without persistent bit)",
348 * Test the format and type parameters and set the GL error code for
349 * \c glClearBufferData and \c glClearBufferSubData.
351 * \param ctx GL context.
352 * \param internalformat Format to which the data is to be converted.
353 * \param format Format of the supplied data.
354 * \param type Type of the supplied data.
355 * \param caller Name of calling function for recording errors.
356 * \return If internalformat, format and type are legal the mesa_format
357 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
359 * \sa glClearBufferData and glClearBufferSubData
362 validate_clear_buffer_format(struct gl_context
*ctx
,
363 GLenum internalformat
,
364 GLenum format
, GLenum type
,
367 mesa_format mesaFormat
;
368 GLenum errorFormatType
;
370 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
371 if (mesaFormat
== MESA_FORMAT_NONE
) {
372 _mesa_error(ctx
, GL_INVALID_ENUM
,
373 "%s(invalid internalformat)", caller
);
374 return MESA_FORMAT_NONE
;
377 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
378 * EXT_texture_integer there is no conversion between integer and
379 * non-integer formats
381 if (_mesa_is_enum_format_signed_int(format
) !=
382 _mesa_is_format_integer_color(mesaFormat
)) {
383 _mesa_error(ctx
, GL_INVALID_OPERATION
,
384 "%s(integer vs non-integer)", caller
);
385 return MESA_FORMAT_NONE
;
388 if (!_mesa_is_color_format(format
)) {
389 _mesa_error(ctx
, GL_INVALID_ENUM
,
390 "%s(format is not a color format)", caller
);
391 return MESA_FORMAT_NONE
;
394 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
395 if (errorFormatType
!= GL_NO_ERROR
) {
396 _mesa_error(ctx
, GL_INVALID_ENUM
,
397 "%s(invalid format or type)", caller
);
398 return MESA_FORMAT_NONE
;
406 * Convert user-specified clear value to the specified internal format.
408 * \param ctx GL context.
409 * \param internalformat Format to which the data is converted.
410 * \param clearValue Points to the converted clear value.
411 * \param format Format of the supplied data.
412 * \param type Type of the supplied data.
413 * \param data Data which is to be converted to internalformat.
414 * \param caller Name of calling function for recording errors.
415 * \return true if data could be converted, false otherwise.
417 * \sa glClearBufferData, glClearBufferSubData
420 convert_clear_buffer_data(struct gl_context
*ctx
,
421 mesa_format internalformat
,
422 GLubyte
*clearValue
, GLenum format
, GLenum type
,
423 const GLvoid
*data
, const char *caller
)
425 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
427 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
428 0, &clearValue
, 1, 1, 1,
429 format
, type
, data
, &ctx
->Unpack
)) {
433 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
440 * Allocate and initialize a new buffer object.
442 * Default callback for the \c dd_function_table::NewBufferObject() hook.
444 static struct gl_buffer_object
*
445 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
447 struct gl_buffer_object
*obj
= MALLOC_STRUCT(gl_buffer_object
);
451 _mesa_initialize_buffer_object(ctx
, obj
, name
);
457 * Delete a buffer object.
459 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
462 _mesa_delete_buffer_object(struct gl_context
*ctx
,
463 struct gl_buffer_object
*bufObj
)
467 vbo_delete_minmax_cache(bufObj
);
468 _mesa_align_free(bufObj
->Data
);
470 /* assign strange values here to help w/ debugging */
471 bufObj
->RefCount
= -1000;
474 mtx_destroy(&bufObj
->Mutex
);
482 * Set ptr to bufObj w/ reference counting.
483 * This is normally only called from the _mesa_reference_buffer_object() macro
484 * when there's a real pointer change.
487 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
488 struct gl_buffer_object
**ptr
,
489 struct gl_buffer_object
*bufObj
)
492 /* Unreference the old buffer */
493 GLboolean deleteFlag
= GL_FALSE
;
494 struct gl_buffer_object
*oldObj
= *ptr
;
496 mtx_lock(&oldObj
->Mutex
);
497 assert(oldObj
->RefCount
> 0);
499 deleteFlag
= (oldObj
->RefCount
== 0);
500 mtx_unlock(&oldObj
->Mutex
);
503 assert(ctx
->Driver
.DeleteBuffer
);
504 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
512 /* reference new buffer */
513 mtx_lock(&bufObj
->Mutex
);
514 assert(bufObj
->RefCount
> 0);
518 mtx_unlock(&bufObj
->Mutex
);
524 * Get the value of MESA_NO_MINMAX_CACHE.
527 get_no_minmax_cache()
529 static bool read
= false;
530 static bool disable
= false;
533 disable
= env_var_as_boolean("MESA_NO_MINMAX_CACHE", false);
542 * Initialize a buffer object to default values.
545 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
546 struct gl_buffer_object
*obj
,
549 memset(obj
, 0, sizeof(struct gl_buffer_object
));
550 mtx_init(&obj
->Mutex
, mtx_plain
);
553 obj
->Usage
= GL_STATIC_DRAW_ARB
;
555 if (get_no_minmax_cache())
556 obj
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
562 * Callback called from _mesa_HashWalk()
565 count_buffer_size(GLuint key
, void *data
, void *userData
)
567 const struct gl_buffer_object
*bufObj
=
568 (const struct gl_buffer_object
*) data
;
569 GLuint
*total
= (GLuint
*) userData
;
572 *total
= *total
+ bufObj
->Size
;
577 * Compute total size (in bytes) of all buffer objects for the given context.
578 * For debugging purposes.
581 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
585 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
592 * Allocate space for and store data in a buffer object. Any data that was
593 * previously stored in the buffer object is lost. If \c data is \c NULL,
594 * memory will be allocated, but no copy will occur.
596 * This is the default callback for \c dd_function_table::BufferData()
597 * Note that all GL error checking will have been done already.
599 * \param ctx GL context.
600 * \param target Buffer object target on which to operate.
601 * \param size Size, in bytes, of the new data store.
602 * \param data Pointer to the data to store in the buffer object. This
603 * pointer may be \c NULL.
604 * \param usage Hints about how the data will be used.
605 * \param bufObj Object to be used.
607 * \return GL_TRUE for success, GL_FALSE for failure
608 * \sa glBufferDataARB, dd_function_table::BufferData.
611 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptr size
,
612 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
613 struct gl_buffer_object
*bufObj
)
619 _mesa_align_free( bufObj
->Data
);
621 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
623 bufObj
->Data
= (GLubyte
*) new_data
;
625 bufObj
->Usage
= usage
;
626 bufObj
->StorageFlags
= storageFlags
;
629 memcpy( bufObj
->Data
, data
, size
);
641 * Replace data in a subrange of buffer object. If the data range
642 * specified by \c size + \c offset extends beyond the end of the buffer or
643 * if \c data is \c NULL, no copy is performed.
645 * This is the default callback for \c dd_function_table::BufferSubData()
646 * Note that all GL error checking will have been done already.
648 * \param ctx GL context.
649 * \param offset Offset of the first byte to be modified.
650 * \param size Size, in bytes, of the data range.
651 * \param data Pointer to the data to store in the buffer object.
652 * \param bufObj Object to be used.
654 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
657 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptr offset
,
658 GLsizeiptr size
, const GLvoid
*data
,
659 struct gl_buffer_object
*bufObj
)
663 /* this should have been caught in _mesa_BufferSubData() */
664 assert(size
+ offset
<= bufObj
->Size
);
667 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
673 * Retrieve data from a subrange of buffer object. If the data range
674 * specified by \c size + \c offset extends beyond the end of the buffer or
675 * if \c data is \c NULL, no copy is performed.
677 * This is the default callback for \c dd_function_table::GetBufferSubData()
678 * Note that all GL error checking will have been done already.
680 * \param ctx GL context.
681 * \param target Buffer object target on which to operate.
682 * \param offset Offset of the first byte to be fetched.
683 * \param size Size, in bytes, of the data range.
684 * \param data Destination for data
685 * \param bufObj Object to be used.
687 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
690 buffer_get_subdata(struct gl_context
*ctx
, GLintptrARB offset
,
691 GLsizeiptrARB size
, GLvoid
*data
,
692 struct gl_buffer_object
*bufObj
)
696 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
697 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
703 * Clear a subrange of the buffer object with copies of the supplied data.
704 * If data is NULL the buffer is filled with zeros.
706 * This is the default callback for \c dd_function_table::ClearBufferSubData()
707 * Note that all GL error checking will have been done already.
709 * \param ctx GL context.
710 * \param offset Offset of the first byte to be cleared.
711 * \param size Size, in bytes, of the to be cleared range.
712 * \param clearValue Source of the data.
713 * \param clearValueSize Size, in bytes, of the supplied data.
714 * \param bufObj Object to be cleared.
716 * \sa glClearBufferSubData, glClearBufferData and
717 * dd_function_table::ClearBufferSubData.
720 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
721 GLintptr offset
, GLsizeiptr size
,
722 const GLvoid
*clearValue
,
723 GLsizeiptr clearValueSize
,
724 struct gl_buffer_object
*bufObj
)
729 assert(ctx
->Driver
.MapBufferRange
);
730 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
732 GL_MAP_INVALIDATE_RANGE_BIT
,
733 bufObj
, MAP_INTERNAL
);
736 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
740 if (clearValue
== NULL
) {
741 /* Clear with zeros, per the spec */
742 memset(dest
, 0, size
);
743 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
747 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
748 memcpy(dest
, clearValue
, clearValueSize
);
749 dest
+= clearValueSize
;
752 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
757 * Default fallback for \c dd_function_table::MapBufferRange().
758 * Called via glMapBufferRange().
761 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
762 GLsizeiptr length
, GLbitfield access
,
763 struct gl_buffer_object
*bufObj
,
764 gl_map_buffer_index index
)
767 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
768 /* Just return a direct pointer to the data */
769 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
770 bufObj
->Mappings
[index
].Length
= length
;
771 bufObj
->Mappings
[index
].Offset
= offset
;
772 bufObj
->Mappings
[index
].AccessFlags
= access
;
773 return bufObj
->Mappings
[index
].Pointer
;
778 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
779 * Called via glFlushMappedBufferRange().
782 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
783 GLintptr offset
, GLsizeiptr length
,
784 struct gl_buffer_object
*obj
,
785 gl_map_buffer_index index
)
797 * Default callback for \c dd_function_table::UnmapBuffer().
799 * The input parameters will have been already tested for errors.
801 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
804 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
805 gl_map_buffer_index index
)
808 /* XXX we might assert here that bufObj->Pointer is non-null */
809 bufObj
->Mappings
[index
].Pointer
= NULL
;
810 bufObj
->Mappings
[index
].Length
= 0;
811 bufObj
->Mappings
[index
].Offset
= 0;
812 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
818 * Default fallback for \c dd_function_table::CopyBufferSubData().
819 * Called via glCopyBufferSubData().
822 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
823 struct gl_buffer_object
*src
,
824 struct gl_buffer_object
*dst
,
825 GLintptr readOffset
, GLintptr writeOffset
,
828 GLubyte
*srcPtr
, *dstPtr
;
831 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
833 GL_MAP_WRITE_BIT
, src
,
839 srcPtr
+= readOffset
;
840 dstPtr
+= writeOffset
;
842 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
843 GL_MAP_READ_BIT
, src
,
845 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
847 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
851 /* Note: the src and dst regions will never overlap. Trying to do so
852 * would generate GL_INVALID_VALUE earlier.
854 if (srcPtr
&& dstPtr
)
855 memcpy(dstPtr
, srcPtr
, size
);
857 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
859 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
865 * Initialize the state associated with buffer objects
868 _mesa_init_buffer_objects( struct gl_context
*ctx
)
872 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
873 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
874 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
876 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
877 ctx
->Shared
->NullBufferObj
);
879 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
880 ctx
->Shared
->NullBufferObj
);
881 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
882 ctx
->Shared
->NullBufferObj
);
884 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
885 ctx
->Shared
->NullBufferObj
);
887 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
888 ctx
->Shared
->NullBufferObj
);
890 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
891 ctx
->Shared
->NullBufferObj
);
893 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
894 ctx
->Shared
->NullBufferObj
);
896 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
,
897 ctx
->Shared
->NullBufferObj
);
899 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
,
900 ctx
->Shared
->NullBufferObj
);
902 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
,
903 ctx
->Shared
->NullBufferObj
);
905 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
906 _mesa_reference_buffer_object(ctx
,
907 &ctx
->UniformBufferBindings
[i
].BufferObject
,
908 ctx
->Shared
->NullBufferObj
);
909 ctx
->UniformBufferBindings
[i
].Offset
= -1;
910 ctx
->UniformBufferBindings
[i
].Size
= -1;
913 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
914 _mesa_reference_buffer_object(ctx
,
915 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
916 ctx
->Shared
->NullBufferObj
);
917 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
918 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
921 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
922 _mesa_reference_buffer_object(ctx
,
923 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
924 ctx
->Shared
->NullBufferObj
);
925 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
926 ctx
->AtomicBufferBindings
[i
].Size
= 0;
932 _mesa_free_buffer_objects( struct gl_context
*ctx
)
936 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
938 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
939 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
941 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
943 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
945 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
947 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
949 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
, NULL
);
951 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
953 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
, NULL
);
955 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
956 _mesa_reference_buffer_object(ctx
,
957 &ctx
->UniformBufferBindings
[i
].BufferObject
,
961 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
962 _mesa_reference_buffer_object(ctx
,
963 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
967 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
968 _mesa_reference_buffer_object(ctx
,
969 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
976 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
978 struct gl_buffer_object
**buf_handle
,
981 struct gl_buffer_object
*buf
= *buf_handle
;
983 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
)) {
984 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
988 if (!buf
|| buf
== &DummyBufferObject
) {
989 /* If this is a new buffer object id, or one which was generated but
990 * never used before, allocate a buffer object now.
992 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
994 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
997 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
1005 * Bind the specified target to buffer for the specified context.
1006 * Called by glBindBuffer() and other functions.
1009 bind_buffer_object(struct gl_context
*ctx
,
1010 struct gl_buffer_object
**bindTarget
, GLuint buffer
)
1012 struct gl_buffer_object
*oldBufObj
;
1013 struct gl_buffer_object
*newBufObj
= NULL
;
1017 /* Get pointer to old buffer object (to be unbound) */
1018 oldBufObj
= *bindTarget
;
1019 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
1020 return; /* rebinding the same buffer object- no change */
1023 * Get pointer to new buffer object (newBufObj)
1026 /* The spec says there's not a buffer object named 0, but we use
1027 * one internally because it simplifies things.
1029 newBufObj
= ctx
->Shared
->NullBufferObj
;
1032 /* non-default buffer object */
1033 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1034 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1035 &newBufObj
, "glBindBuffer"))
1039 /* record usage history */
1040 if (bindTarget
== &ctx
->Pack
.BufferObj
) {
1041 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1044 /* bind new buffer */
1045 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1050 * Update the default buffer objects in the given context to reference those
1051 * specified in the shared state and release those referencing the old
1055 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1057 /* Bind the NullBufferObj to remove references to those
1058 * in the shared context hash table.
1060 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1061 bind_buffer_object(ctx
, &ctx
->Array
.VAO
->IndexBufferObj
, 0);
1062 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1063 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1069 * Return the gl_buffer_object for the given ID.
1070 * Always return NULL for ID 0.
1072 struct gl_buffer_object
*
1073 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1078 return (struct gl_buffer_object
*)
1079 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1083 struct gl_buffer_object
*
1084 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1089 return (struct gl_buffer_object
*)
1090 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1094 * A convenience function for direct state access functions that throws
1095 * GL_INVALID_OPERATION if buffer is not the name of an existing
1098 struct gl_buffer_object
*
1099 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1102 struct gl_buffer_object
*bufObj
;
1104 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1105 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1106 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1107 "%s(non-existent buffer object %u)", caller
, buffer
);
1116 * Look up a buffer object for a multi-bind function.
1118 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1119 * of generating an error if the buffer ID is not zero or the name
1120 * of an existing buffer object.
1122 * If the buffer ID refers to an existing buffer object, a pointer
1123 * to the buffer object is returned. If the ID is zero, a pointer
1124 * to the shared NullBufferObj is returned. If the ID is not zero
1125 * and does not refer to a valid buffer object, this function
1128 * This function assumes that the caller has already locked the
1129 * hash table mutex by calling
1130 * _mesa_HashLockMutex(ctx->Shared->BufferObjects).
1132 struct gl_buffer_object
*
1133 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1134 const GLuint
*buffers
,
1135 GLuint index
, const char *caller
)
1137 struct gl_buffer_object
*bufObj
;
1139 if (buffers
[index
] != 0) {
1140 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1142 /* The multi-bind functions don't create the buffer objects
1143 when they don't exist. */
1144 if (bufObj
== &DummyBufferObject
)
1147 bufObj
= ctx
->Shared
->NullBufferObj
;
1150 /* The ARB_multi_bind spec says:
1152 * "An INVALID_OPERATION error is generated if any value
1153 * in <buffers> is not zero or the name of an existing
1154 * buffer object (per binding)."
1156 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1157 "%s(buffers[%u]=%u is not zero or the name "
1158 "of an existing buffer object)",
1159 caller
, index
, buffers
[index
]);
1167 * If *ptr points to obj, set ptr = the Null/default buffer object.
1168 * This is a helper for buffer object deletion.
1169 * The GL spec says that deleting a buffer object causes it to get
1170 * unbound from all arrays in the current context.
1173 unbind(struct gl_context
*ctx
,
1174 struct gl_vertex_array_object
*vao
, unsigned index
,
1175 struct gl_buffer_object
*obj
)
1177 if (vao
->BufferBinding
[index
].BufferObj
== obj
) {
1178 _mesa_bind_vertex_buffer(ctx
, vao
, index
, ctx
->Shared
->NullBufferObj
,
1179 vao
->BufferBinding
[index
].Offset
,
1180 vao
->BufferBinding
[index
].Stride
);
1186 * Plug default/fallback buffer object functions into the device
1190 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1192 /* GL_ARB_vertex/pixel_buffer_object */
1193 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1194 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1195 driver
->BufferData
= buffer_data_fallback
;
1196 driver
->BufferSubData
= buffer_sub_data_fallback
;
1197 driver
->GetBufferSubData
= buffer_get_subdata
;
1198 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1200 /* GL_ARB_clear_buffer_object */
1201 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1203 /* GL_ARB_map_buffer_range */
1204 driver
->MapBufferRange
= map_buffer_range_fallback
;
1205 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1207 /* GL_ARB_copy_buffer */
1208 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1213 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1214 struct gl_buffer_object
*bufObj
)
1216 for (int i
= 0; i
< MAP_COUNT
; i
++) {
1217 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1218 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1219 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1220 bufObj
->Mappings
[i
].AccessFlags
= 0;
1226 /**********************************************************************/
1228 /**********************************************************************/
1231 _mesa_BindBuffer_no_error(GLenum target
, GLuint buffer
)
1233 GET_CURRENT_CONTEXT(ctx
);
1235 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1236 bind_buffer_object(ctx
, bindTarget
, buffer
);
1241 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1243 GET_CURRENT_CONTEXT(ctx
);
1245 if (MESA_VERBOSE
& VERBOSE_API
) {
1246 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1247 _mesa_enum_to_string(target
), buffer
);
1250 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1252 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target %s)",
1253 _mesa_enum_to_string(target
));
1257 bind_buffer_object(ctx
, bindTarget
, buffer
);
1261 * Binds a buffer object to an atomic buffer binding point.
1263 * The caller is responsible for validating the offset,
1264 * flushing the vertices and updating NewDriverState.
1267 set_atomic_buffer_binding(struct gl_context
*ctx
,
1268 struct gl_atomic_buffer_binding
*binding
,
1269 struct gl_buffer_object
*bufObj
,
1273 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1275 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
1276 binding
->Offset
= 0;
1279 binding
->Offset
= offset
;
1280 binding
->Size
= size
;
1281 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
1286 * Binds a buffer object to a uniform buffer binding point.
1288 * The caller is responsible for flushing vertices and updating
1292 set_ubo_binding(struct gl_context
*ctx
,
1293 struct gl_uniform_buffer_binding
*binding
,
1294 struct gl_buffer_object
*bufObj
,
1299 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1301 binding
->Offset
= offset
;
1302 binding
->Size
= size
;
1303 binding
->AutomaticSize
= autoSize
;
1305 /* If this is a real buffer object, mark it has having been used
1306 * at some point as a UBO.
1309 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
1313 * Binds a buffer object to a shader storage buffer binding point.
1315 * The caller is responsible for flushing vertices and updating
1319 set_ssbo_binding(struct gl_context
*ctx
,
1320 struct gl_shader_storage_buffer_binding
*binding
,
1321 struct gl_buffer_object
*bufObj
,
1326 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1328 binding
->Offset
= offset
;
1329 binding
->Size
= size
;
1330 binding
->AutomaticSize
= autoSize
;
1332 /* If this is a real buffer object, mark it has having been used
1333 * at some point as a SSBO.
1336 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
1340 * Binds a buffer object to a uniform buffer binding point.
1342 * Unlike set_ubo_binding(), this function also flushes vertices
1343 * and updates NewDriverState. It also checks if the binding
1344 * has actually changed before updating it.
1347 bind_uniform_buffer(struct gl_context
*ctx
,
1349 struct gl_buffer_object
*bufObj
,
1354 struct gl_uniform_buffer_binding
*binding
=
1355 &ctx
->UniformBufferBindings
[index
];
1357 if (binding
->BufferObject
== bufObj
&&
1358 binding
->Offset
== offset
&&
1359 binding
->Size
== size
&&
1360 binding
->AutomaticSize
== autoSize
) {
1364 FLUSH_VERTICES(ctx
, 0);
1365 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
1367 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
1371 * Binds a buffer object to a shader storage buffer binding point.
1373 * Unlike set_ssbo_binding(), this function also flushes vertices
1374 * and updates NewDriverState. It also checks if the binding
1375 * has actually changed before updating it.
1378 bind_shader_storage_buffer(struct gl_context
*ctx
,
1380 struct gl_buffer_object
*bufObj
,
1385 struct gl_shader_storage_buffer_binding
*binding
=
1386 &ctx
->ShaderStorageBufferBindings
[index
];
1388 if (binding
->BufferObject
== bufObj
&&
1389 binding
->Offset
== offset
&&
1390 binding
->Size
== size
&&
1391 binding
->AutomaticSize
== autoSize
) {
1395 FLUSH_VERTICES(ctx
, 0);
1396 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
1398 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
1402 * Bind a buffer object to a uniform block binding point.
1403 * As above, but offset = 0.
1406 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
1408 struct gl_buffer_object
*bufObj
)
1410 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
1411 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1415 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
1417 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1418 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1420 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1424 * Bind a buffer object to a shader storage block binding point.
1425 * As above, but offset = 0.
1428 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
1430 struct gl_buffer_object
*bufObj
)
1432 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
1433 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1437 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
1439 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1440 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1442 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1446 bind_atomic_buffer(struct gl_context
*ctx
, unsigned index
,
1447 struct gl_buffer_object
*bufObj
, GLintptr offset
,
1450 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
1452 struct gl_atomic_buffer_binding
*binding
=
1453 &ctx
->AtomicBufferBindings
[index
];
1454 if (binding
->BufferObject
== bufObj
&&
1455 binding
->Offset
== offset
&&
1456 binding
->Size
== size
) {
1460 FLUSH_VERTICES(ctx
, 0);
1461 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
1463 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
1467 * Delete a set of buffer objects.
1469 * \param n Number of buffer objects to delete.
1470 * \param ids Array of \c n buffer object IDs.
1473 delete_buffers(struct gl_context
*ctx
, GLsizei n
, const GLuint
*ids
)
1475 FLUSH_VERTICES(ctx
, 0);
1477 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1479 for (GLsizei i
= 0; i
< n
; i
++) {
1480 struct gl_buffer_object
*bufObj
=
1481 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1483 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1486 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1488 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1490 /* unbind any vertex pointers bound to this buffer */
1491 for (j
= 0; j
< ARRAY_SIZE(vao
->BufferBinding
); j
++) {
1492 unbind(ctx
, vao
, j
, bufObj
);
1495 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1496 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1498 if (vao
->IndexBufferObj
== bufObj
) {
1499 bind_buffer_object(ctx
, &vao
->IndexBufferObj
, 0);
1502 /* unbind ARB_draw_indirect binding point */
1503 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1504 bind_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, 0);
1507 /* unbind ARB_indirect_parameters binding point */
1508 if (ctx
->ParameterBuffer
== bufObj
) {
1509 bind_buffer_object(ctx
, &ctx
->ParameterBuffer
, 0);
1512 /* unbind ARB_compute_shader binding point */
1513 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1514 bind_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, 0);
1517 /* unbind ARB_copy_buffer binding points */
1518 if (ctx
->CopyReadBuffer
== bufObj
) {
1519 bind_buffer_object(ctx
, &ctx
->CopyReadBuffer
, 0);
1521 if (ctx
->CopyWriteBuffer
== bufObj
) {
1522 bind_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, 0);
1525 /* unbind transform feedback binding points */
1526 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1527 bind_buffer_object(ctx
, &ctx
->TransformFeedback
.CurrentBuffer
, 0);
1529 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1530 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1531 _mesa_bind_buffer_base_transform_feedback(ctx
,
1532 ctx
->TransformFeedback
.CurrentObject
,
1533 j
, ctx
->Shared
->NullBufferObj
,
1538 /* unbind UBO binding points */
1539 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1540 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1541 bind_buffer_base_uniform_buffer(ctx
, j
,
1542 ctx
->Shared
->NullBufferObj
);
1546 if (ctx
->UniformBuffer
== bufObj
) {
1547 bind_buffer_object(ctx
, &ctx
->UniformBuffer
, 0);
1550 /* unbind SSBO binding points */
1551 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1552 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1553 bind_buffer_base_shader_storage_buffer(ctx
, j
,
1554 ctx
->Shared
->NullBufferObj
);
1558 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1559 bind_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, 0);
1562 /* unbind Atomci Buffer binding points */
1563 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1564 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1565 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1566 bind_atomic_buffer(ctx
, j
, ctx
->Shared
->NullBufferObj
, 0, 0);
1570 if (ctx
->AtomicBuffer
== bufObj
) {
1571 bind_buffer_object(ctx
, &ctx
->AtomicBuffer
, 0);
1574 /* unbind any pixel pack/unpack pointers bound to this buffer */
1575 if (ctx
->Pack
.BufferObj
== bufObj
) {
1576 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1578 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1579 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1582 if (ctx
->Texture
.BufferObject
== bufObj
) {
1583 bind_buffer_object(ctx
, &ctx
->Texture
.BufferObject
, 0);
1586 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1587 bind_buffer_object(ctx
, &ctx
->ExternalVirtualMemoryBuffer
, 0);
1590 /* unbind query buffer binding point */
1591 if (ctx
->QueryBuffer
== bufObj
) {
1592 bind_buffer_object(ctx
, &ctx
->QueryBuffer
, 0);
1595 /* The ID is immediately freed for re-use */
1596 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1597 /* Make sure we do not run into the classic ABA problem on bind.
1598 * We don't want to allow re-binding a buffer object that's been
1599 * "deleted" by glDeleteBuffers().
1601 * The explicit rebinding to the default object in the current context
1602 * prevents the above in the current context, but another context
1603 * sharing the same objects might suffer from this problem.
1604 * The alternative would be to do the hash lookup in any case on bind
1605 * which would introduce more runtime overhead than this.
1607 bufObj
->DeletePending
= GL_TRUE
;
1608 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1612 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1617 _mesa_DeleteBuffers_no_error(GLsizei n
, const GLuint
*ids
)
1619 GET_CURRENT_CONTEXT(ctx
);
1620 delete_buffers(ctx
, n
, ids
);
1625 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1627 GET_CURRENT_CONTEXT(ctx
);
1630 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1634 delete_buffers(ctx
, n
, ids
);
1639 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1640 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1644 create_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1647 struct gl_buffer_object
*buf
;
1653 * This must be atomic (generation and allocation of buffer object IDs)
1655 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1657 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1659 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1660 * DummyBufferObject. Otherwise, create a new buffer object and insert
1663 for (int i
= 0; i
< n
; i
++) {
1664 buffers
[i
] = first
+ i
;
1666 assert(ctx
->Driver
.NewBufferObject
);
1667 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1669 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCreateBuffers");
1670 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1675 buf
= &DummyBufferObject
;
1677 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1680 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1685 create_buffers_err(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1687 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1689 if (MESA_VERBOSE
& VERBOSE_API
)
1690 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1693 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1697 create_buffers(ctx
, n
, buffers
, dsa
);
1701 * Generate a set of unique buffer object IDs and store them in \c buffers.
1703 * \param n Number of IDs to generate.
1704 * \param buffers Array of \c n locations to store the IDs.
1707 _mesa_GenBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1709 GET_CURRENT_CONTEXT(ctx
);
1710 create_buffers(ctx
, n
, buffers
, false);
1715 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1717 GET_CURRENT_CONTEXT(ctx
);
1718 create_buffers_err(ctx
, n
, buffers
, false);
1722 * Create a set of buffer objects and store their unique IDs in \c buffers.
1724 * \param n Number of IDs to generate.
1725 * \param buffers Array of \c n locations to store the IDs.
1728 _mesa_CreateBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1730 GET_CURRENT_CONTEXT(ctx
);
1731 create_buffers(ctx
, n
, buffers
, true);
1736 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1738 GET_CURRENT_CONTEXT(ctx
);
1739 create_buffers_err(ctx
, n
, buffers
, true);
1744 * Determine if ID is the name of a buffer object.
1746 * \param id ID of the potential buffer object.
1747 * \return \c GL_TRUE if \c id is the name of a buffer object,
1748 * \c GL_FALSE otherwise.
1750 GLboolean GLAPIENTRY
1751 _mesa_IsBuffer(GLuint id
)
1753 struct gl_buffer_object
*bufObj
;
1754 GET_CURRENT_CONTEXT(ctx
);
1755 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1757 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1759 return bufObj
&& bufObj
!= &DummyBufferObject
;
1764 validate_buffer_storage(struct gl_context
*ctx
,
1765 struct gl_buffer_object
*bufObj
, GLsizeiptr size
,
1766 GLbitfield flags
, const char *func
)
1769 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1773 GLbitfield valid_flags
= GL_MAP_READ_BIT
|
1775 GL_MAP_PERSISTENT_BIT
|
1776 GL_MAP_COHERENT_BIT
|
1777 GL_DYNAMIC_STORAGE_BIT
|
1778 GL_CLIENT_STORAGE_BIT
;
1780 if (ctx
->Extensions
.ARB_sparse_buffer
)
1781 valid_flags
|= GL_SPARSE_STORAGE_BIT_ARB
;
1783 if (flags
& ~valid_flags
) {
1784 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1788 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1790 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1791 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1792 * MAP_READ_BIT or MAP_WRITE_BIT."
1794 if (flags
& GL_SPARSE_STORAGE_BIT_ARB
&&
1795 flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) {
1796 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(SPARSE_STORAGE and READ/WRITE)", func
);
1800 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1801 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1802 _mesa_error(ctx
, GL_INVALID_VALUE
,
1803 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1807 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1808 _mesa_error(ctx
, GL_INVALID_VALUE
,
1809 "%s(COHERENT and flags!=PERSISTENT)", func
);
1813 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
1814 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1823 buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1824 struct gl_memory_object
*memObj
, GLenum target
,
1825 GLsizeiptr size
, const GLvoid
*data
, GLbitfield flags
,
1826 GLuint64 offset
, const char *func
)
1830 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1831 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1833 FLUSH_VERTICES(ctx
, 0);
1835 bufObj
->Written
= GL_TRUE
;
1836 bufObj
->Immutable
= GL_TRUE
;
1837 bufObj
->MinMaxCacheDirty
= true;
1840 assert(ctx
->Driver
.BufferDataMem
);
1841 res
= ctx
->Driver
.BufferDataMem(ctx
, target
, size
, memObj
, offset
,
1842 GL_DYNAMIC_DRAW
, bufObj
);
1845 assert(ctx
->Driver
.BufferData
);
1846 res
= ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1851 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1852 /* Even though the interaction between AMD_pinned_memory and
1853 * glBufferStorage is not described in the spec, Graham Sellers
1854 * said that it should behave the same as glBufferData.
1856 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1859 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1865 static ALWAYS_INLINE
void
1866 inlined_buffer_storage(GLenum target
, GLuint buffer
, GLsizeiptr size
,
1867 const GLvoid
*data
, GLbitfield flags
,
1868 GLuint memory
, GLuint64 offset
,
1869 bool dsa
, bool mem
, bool no_error
, const char *func
)
1871 GET_CURRENT_CONTEXT(ctx
);
1872 struct gl_buffer_object
*bufObj
;
1873 struct gl_memory_object
*memObj
= NULL
;
1877 if (!ctx
->Extensions
.EXT_memory_object
) {
1878 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
1882 /* From the EXT_external_objects spec:
1884 * "An INVALID_VALUE error is generated by BufferStorageMemEXT and
1885 * NamedBufferStorageMemEXT if <memory> is 0, or ..."
1888 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(memory == 0)", func
);
1892 memObj
= _mesa_lookup_memory_object(ctx
, memory
);
1896 /* From the EXT_external_objects spec:
1898 * "An INVALID_OPERATION error is generated if <memory> names a
1899 * valid memory object which has no associated memory."
1901 if (!no_error
&& !memObj
->Immutable
) {
1902 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no associated memory)",
1910 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1912 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
1918 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
1919 bufObj
= *bufObjPtr
;
1921 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
1927 if (no_error
|| validate_buffer_storage(ctx
, bufObj
, size
, flags
, func
))
1928 buffer_storage(ctx
, bufObj
, memObj
, target
, size
, data
, flags
, offset
, func
);
1933 _mesa_BufferStorage_no_error(GLenum target
, GLsizeiptr size
,
1934 const GLvoid
*data
, GLbitfield flags
)
1936 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1937 false, false, true, "glBufferStorage");
1942 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1945 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1946 false, false, false, "glBufferStorage");
1951 _mesa_BufferStorageMemEXT(GLenum target
, GLsizeiptr size
,
1952 GLuint memory
, GLuint64 offset
)
1954 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1955 false, true, false, "glBufferStorageMemEXT");
1960 _mesa_BufferStorageMemEXT_no_error(GLenum target
, GLsizeiptr size
,
1961 GLuint memory
, GLuint64 offset
)
1963 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1964 false, true, true, "glBufferStorageMemEXT");
1969 _mesa_NamedBufferStorage_no_error(GLuint buffer
, GLsizeiptr size
,
1970 const GLvoid
*data
, GLbitfield flags
)
1972 /* In direct state access, buffer objects have an unspecified target
1973 * since they are not required to be bound.
1975 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1976 true, false, true, "glNamedBufferStorage");
1981 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1984 /* In direct state access, buffer objects have an unspecified target
1985 * since they are not required to be bound.
1987 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1988 true, false, false, "glNamedBufferStorage");
1992 _mesa_NamedBufferStorageMemEXT(GLuint buffer
, GLsizeiptr size
,
1993 GLuint memory
, GLuint64 offset
)
1995 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1996 true, true, false, "glNamedBufferStorageMemEXT");
2001 _mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer
, GLsizeiptr size
,
2002 GLuint memory
, GLuint64 offset
)
2004 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
2005 true, true, true, "glNamedBufferStorageMemEXT");
2009 static ALWAYS_INLINE
void
2010 buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2011 GLenum target
, GLsizeiptr size
, const GLvoid
*data
, GLenum usage
,
2012 const char *func
, bool no_error
)
2016 if (MESA_VERBOSE
& VERBOSE_API
) {
2017 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
2019 _mesa_enum_to_string(target
),
2020 (long int) size
, data
,
2021 _mesa_enum_to_string(usage
));
2026 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
2031 case GL_STREAM_DRAW_ARB
:
2032 valid_usage
= (ctx
->API
!= API_OPENGLES
);
2034 case GL_STATIC_DRAW_ARB
:
2035 case GL_DYNAMIC_DRAW_ARB
:
2038 case GL_STREAM_READ_ARB
:
2039 case GL_STREAM_COPY_ARB
:
2040 case GL_STATIC_READ_ARB
:
2041 case GL_STATIC_COPY_ARB
:
2042 case GL_DYNAMIC_READ_ARB
:
2043 case GL_DYNAMIC_COPY_ARB
:
2044 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
2047 valid_usage
= false;
2052 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
2053 _mesa_enum_to_string(usage
));
2057 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
2058 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
2063 /* Unmap the existing buffer. We'll replace it now. Not an error. */
2064 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
2066 FLUSH_VERTICES(ctx
, 0);
2068 bufObj
->Written
= GL_TRUE
;
2069 bufObj
->MinMaxCacheDirty
= true;
2072 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
2073 bufObj
->Name
, size
, data
, usage
);
2080 assert(ctx
->Driver
.BufferData
);
2081 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
2084 GL_DYNAMIC_STORAGE_BIT
,
2086 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
2088 /* From GL_AMD_pinned_memory:
2090 * INVALID_OPERATION is generated by BufferData if <target> is
2091 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
2092 * mapped to the GPU address space.
2094 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2097 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2103 buffer_data_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2104 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2105 GLenum usage
, const char *func
)
2107 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, false);
2111 buffer_data_no_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2112 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2113 GLenum usage
, const char *func
)
2115 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, true);
2119 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2120 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2121 GLenum usage
, const char *func
)
2123 buffer_data_error(ctx
, bufObj
, target
, size
, data
, usage
, func
);
2127 _mesa_BufferData_no_error(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2130 GET_CURRENT_CONTEXT(ctx
);
2132 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2133 buffer_data_no_error(ctx
, *bufObj
, target
, size
, data
, usage
,
2138 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
2139 const GLvoid
*data
, GLenum usage
)
2141 GET_CURRENT_CONTEXT(ctx
);
2142 struct gl_buffer_object
*bufObj
;
2144 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
2148 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
2153 _mesa_NamedBufferData_no_error(GLuint buffer
, GLsizeiptr size
,
2154 const GLvoid
*data
, GLenum usage
)
2156 GET_CURRENT_CONTEXT(ctx
);
2158 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2159 buffer_data_no_error(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2160 "glNamedBufferData");
2164 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2167 GET_CURRENT_CONTEXT(ctx
);
2168 struct gl_buffer_object
*bufObj
;
2170 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
2174 /* In direct state access, buffer objects have an unspecified target since
2175 * they are not required to be bound.
2177 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2178 "glNamedBufferData");
2183 validate_buffer_sub_data(struct gl_context
*ctx
,
2184 struct gl_buffer_object
*bufObj
,
2185 GLintptr offset
, GLsizeiptr size
,
2188 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2190 /* error already recorded */
2194 if (bufObj
->Immutable
&&
2195 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
2196 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2200 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2201 bufObj
->Usage
== GL_STATIC_COPY
) &&
2202 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
- 1) {
2203 /* If the application declared the buffer as static draw/copy or stream
2204 * draw, it should not be frequently modified with glBufferSubData.
2206 BUFFER_USAGE_WARNING(ctx
,
2207 "using %s(buffer %u, offset %u, size %u) to "
2208 "update a %s buffer",
2209 func
, bufObj
->Name
, offset
, size
,
2210 _mesa_enum_to_string(bufObj
->Usage
));
2218 * Implementation for glBufferSubData and glNamedBufferSubData.
2220 * \param ctx GL context.
2221 * \param bufObj The buffer object.
2222 * \param offset Offset of the first byte of the subdata range.
2223 * \param size Size, in bytes, of the subdata range.
2224 * \param data The data store.
2225 * \param func Name of calling function for recording errors.
2229 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2230 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
)
2235 bufObj
->NumSubDataCalls
++;
2236 bufObj
->Written
= GL_TRUE
;
2237 bufObj
->MinMaxCacheDirty
= true;
2239 assert(ctx
->Driver
.BufferSubData
);
2240 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
2244 static ALWAYS_INLINE
void
2245 buffer_sub_data(GLenum target
, GLuint buffer
, GLintptr offset
,
2246 GLsizeiptr size
, const GLvoid
*data
,
2247 bool dsa
, bool no_error
, const char *func
)
2249 GET_CURRENT_CONTEXT(ctx
);
2250 struct gl_buffer_object
*bufObj
;
2254 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2256 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
2262 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2263 bufObj
= *bufObjPtr
;
2265 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
2271 if (no_error
|| validate_buffer_sub_data(ctx
, bufObj
, offset
, size
, func
))
2272 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2277 _mesa_BufferSubData_no_error(GLenum target
, GLintptr offset
,
2278 GLsizeiptr size
, const GLvoid
*data
)
2280 buffer_sub_data(target
, 0, offset
, size
, data
, false, true,
2286 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
2287 GLsizeiptr size
, const GLvoid
*data
)
2289 buffer_sub_data(target
, 0, offset
, size
, data
, false, false,
2294 _mesa_NamedBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
2295 GLsizeiptr size
, const GLvoid
*data
)
2297 buffer_sub_data(0, buffer
, offset
, size
, data
, true, true,
2298 "glNamedBufferSubData");
2302 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
2303 GLsizeiptr size
, const GLvoid
*data
)
2305 buffer_sub_data(0, buffer
, offset
, size
, data
, true, false,
2306 "glNamedBufferSubData");
2311 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
2312 GLsizeiptr size
, GLvoid
*data
)
2314 GET_CURRENT_CONTEXT(ctx
);
2315 struct gl_buffer_object
*bufObj
;
2317 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
2318 GL_INVALID_OPERATION
);
2322 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2323 "glGetBufferSubData")) {
2327 assert(ctx
->Driver
.GetBufferSubData
);
2328 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2332 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
2333 GLsizeiptr size
, GLvoid
*data
)
2335 GET_CURRENT_CONTEXT(ctx
);
2336 struct gl_buffer_object
*bufObj
;
2338 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2339 "glGetNamedBufferSubData");
2343 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2344 "glGetNamedBufferSubData")) {
2348 assert(ctx
->Driver
.GetBufferSubData
);
2349 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2354 * \param subdata true if caller is *SubData, false if *Data
2356 static ALWAYS_INLINE
void
2357 clear_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2358 GLenum internalformat
, GLintptr offset
, GLsizeiptr size
,
2359 GLenum format
, GLenum type
, const GLvoid
*data
,
2360 const char *func
, bool subdata
, bool no_error
)
2362 mesa_format mesaFormat
;
2363 GLubyte clearValue
[MAX_PIXEL_BYTES
];
2364 GLsizeiptr clearValueSize
;
2366 /* This checks for disallowed mappings. */
2367 if (!no_error
&& !buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2373 mesaFormat
= _mesa_get_texbuffer_format(ctx
, internalformat
);
2375 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
2376 format
, type
, func
);
2379 if (mesaFormat
== MESA_FORMAT_NONE
)
2382 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
2384 (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0)) {
2385 _mesa_error(ctx
, GL_INVALID_VALUE
,
2386 "%s(offset or size is not a multiple of "
2387 "internalformat size)", func
);
2391 /* Bail early. Negative size has already been checked. */
2395 bufObj
->MinMaxCacheDirty
= true;
2398 /* clear to zeros, per the spec */
2399 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2400 NULL
, clearValueSize
, bufObj
);
2404 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
2405 format
, type
, data
, func
)) {
2409 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2410 clearValue
, clearValueSize
, bufObj
);
2414 clear_buffer_sub_data_error(struct gl_context
*ctx
,
2415 struct gl_buffer_object
*bufObj
,
2416 GLenum internalformat
, GLintptr offset
,
2417 GLsizeiptr size
, GLenum format
, GLenum type
,
2418 const GLvoid
*data
, const char *func
, bool subdata
)
2420 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2421 type
, data
, func
, subdata
, false);
2426 clear_buffer_sub_data_no_error(struct gl_context
*ctx
,
2427 struct gl_buffer_object
*bufObj
,
2428 GLenum internalformat
, GLintptr offset
,
2429 GLsizeiptr size
, GLenum format
, GLenum type
,
2430 const GLvoid
*data
, const char *func
,
2433 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2434 type
, data
, func
, subdata
, true);
2439 _mesa_ClearBufferData_no_error(GLenum target
, GLenum internalformat
,
2440 GLenum format
, GLenum type
, const GLvoid
*data
)
2442 GET_CURRENT_CONTEXT(ctx
);
2444 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2445 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, 0,
2446 (*bufObj
)->Size
, format
, type
, data
,
2447 "glClearBufferData", false);
2452 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
2453 GLenum type
, const GLvoid
*data
)
2455 GET_CURRENT_CONTEXT(ctx
);
2456 struct gl_buffer_object
*bufObj
;
2458 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
2462 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2463 format
, type
, data
, "glClearBufferData", false);
2468 _mesa_ClearNamedBufferData_no_error(GLuint buffer
, GLenum internalformat
,
2469 GLenum format
, GLenum type
,
2472 GET_CURRENT_CONTEXT(ctx
);
2474 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2475 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2476 format
, type
, data
, "glClearNamedBufferData",
2482 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
2483 GLenum format
, GLenum type
, const GLvoid
*data
)
2485 GET_CURRENT_CONTEXT(ctx
);
2486 struct gl_buffer_object
*bufObj
;
2488 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
2492 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2493 format
, type
, data
, "glClearNamedBufferData",
2499 _mesa_ClearBufferSubData_no_error(GLenum target
, GLenum internalformat
,
2500 GLintptr offset
, GLsizeiptr size
,
2501 GLenum format
, GLenum type
,
2504 GET_CURRENT_CONTEXT(ctx
);
2506 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2507 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, offset
, size
,
2508 format
, type
, data
, "glClearBufferSubData",
2514 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
2515 GLintptr offset
, GLsizeiptr size
,
2516 GLenum format
, GLenum type
,
2519 GET_CURRENT_CONTEXT(ctx
);
2520 struct gl_buffer_object
*bufObj
;
2522 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
2526 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2527 format
, type
, data
, "glClearBufferSubData",
2533 _mesa_ClearNamedBufferSubData_no_error(GLuint buffer
, GLenum internalformat
,
2534 GLintptr offset
, GLsizeiptr size
,
2535 GLenum format
, GLenum type
,
2538 GET_CURRENT_CONTEXT(ctx
);
2540 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2541 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, offset
, size
,
2543 "glClearNamedBufferSubData", true);
2548 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
2549 GLintptr offset
, GLsizeiptr size
,
2550 GLenum format
, GLenum type
,
2553 GET_CURRENT_CONTEXT(ctx
);
2554 struct gl_buffer_object
*bufObj
;
2556 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2557 "glClearNamedBufferSubData");
2561 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2562 format
, type
, data
, "glClearNamedBufferSubData",
2567 unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
2569 GLboolean status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2570 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2571 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2572 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2573 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2579 validate_and_unmap_buffer(struct gl_context
*ctx
,
2580 struct gl_buffer_object
*bufObj
,
2583 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2585 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2586 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2587 "%s(buffer is not mapped)", func
);
2592 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2593 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2595 /* check that last 100 bytes are still = magic value */
2596 for (i
= 0; i
< 100; i
++) {
2597 GLuint pos
= bufObj
->Size
- i
- 1;
2598 if (buf
[pos
] != 123) {
2599 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2600 " at position %d (value = %u)\n",
2608 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2609 GLuint i
, unchanged
= 0;
2610 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2612 /* check which bytes changed */
2613 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2614 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2621 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2622 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2627 return unmap_buffer(ctx
, bufObj
);
2630 GLboolean GLAPIENTRY
2631 _mesa_UnmapBuffer_no_error(GLenum target
)
2633 GET_CURRENT_CONTEXT(ctx
);
2634 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2635 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2637 return unmap_buffer(ctx
, bufObj
);
2640 GLboolean GLAPIENTRY
2641 _mesa_UnmapBuffer(GLenum target
)
2643 GET_CURRENT_CONTEXT(ctx
);
2644 struct gl_buffer_object
*bufObj
;
2646 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2650 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2653 GLboolean GLAPIENTRY
2654 _mesa_UnmapNamedBuffer_no_error(GLuint buffer
)
2656 GET_CURRENT_CONTEXT(ctx
);
2657 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2659 return unmap_buffer(ctx
, bufObj
);
2662 GLboolean GLAPIENTRY
2663 _mesa_UnmapNamedBuffer(GLuint buffer
)
2665 GET_CURRENT_CONTEXT(ctx
);
2666 struct gl_buffer_object
*bufObj
;
2668 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2672 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2677 get_buffer_parameter(struct gl_context
*ctx
,
2678 struct gl_buffer_object
*bufObj
, GLenum pname
,
2679 GLint64
*params
, const char *func
)
2682 case GL_BUFFER_SIZE_ARB
:
2683 *params
= bufObj
->Size
;
2685 case GL_BUFFER_USAGE_ARB
:
2686 *params
= bufObj
->Usage
;
2688 case GL_BUFFER_ACCESS_ARB
:
2689 *params
= simplified_access_mode(ctx
,
2690 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2692 case GL_BUFFER_MAPPED_ARB
:
2693 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2695 case GL_BUFFER_ACCESS_FLAGS
:
2696 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2698 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2700 case GL_BUFFER_MAP_OFFSET
:
2701 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2703 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2705 case GL_BUFFER_MAP_LENGTH
:
2706 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2708 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2710 case GL_BUFFER_IMMUTABLE_STORAGE
:
2711 if (!ctx
->Extensions
.ARB_buffer_storage
)
2713 *params
= bufObj
->Immutable
;
2715 case GL_BUFFER_STORAGE_FLAGS
:
2716 if (!ctx
->Extensions
.ARB_buffer_storage
)
2718 *params
= bufObj
->StorageFlags
;
2727 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2728 _mesa_enum_to_string(pname
));
2733 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2735 GET_CURRENT_CONTEXT(ctx
);
2736 struct gl_buffer_object
*bufObj
;
2739 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2740 GL_INVALID_OPERATION
);
2744 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2745 "glGetBufferParameteriv"))
2746 return; /* Error already recorded. */
2748 *params
= (GLint
) parameter
;
2752 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2754 GET_CURRENT_CONTEXT(ctx
);
2755 struct gl_buffer_object
*bufObj
;
2758 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2759 GL_INVALID_OPERATION
);
2763 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2764 "glGetBufferParameteri64v"))
2765 return; /* Error already recorded. */
2767 *params
= parameter
;
2771 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2773 GET_CURRENT_CONTEXT(ctx
);
2774 struct gl_buffer_object
*bufObj
;
2777 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2778 "glGetNamedBufferParameteriv");
2782 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2783 "glGetNamedBufferParameteriv"))
2784 return; /* Error already recorded. */
2786 *params
= (GLint
) parameter
;
2790 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2793 GET_CURRENT_CONTEXT(ctx
);
2794 struct gl_buffer_object
*bufObj
;
2797 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2798 "glGetNamedBufferParameteri64v");
2802 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2803 "glGetNamedBufferParameteri64v"))
2804 return; /* Error already recorded. */
2806 *params
= parameter
;
2811 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2813 GET_CURRENT_CONTEXT(ctx
);
2814 struct gl_buffer_object
*bufObj
;
2816 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2817 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2818 "GL_BUFFER_MAP_POINTER)");
2822 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2823 GL_INVALID_OPERATION
);
2827 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2831 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2833 GET_CURRENT_CONTEXT(ctx
);
2834 struct gl_buffer_object
*bufObj
;
2836 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2837 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2838 "GL_BUFFER_MAP_POINTER)");
2842 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2843 "glGetNamedBufferPointerv");
2847 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2852 copy_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*src
,
2853 struct gl_buffer_object
*dst
, GLintptr readOffset
,
2854 GLintptr writeOffset
, GLsizeiptr size
, const char *func
)
2856 if (_mesa_check_disallowed_mapping(src
)) {
2857 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2858 "%s(readBuffer is mapped)", func
);
2862 if (_mesa_check_disallowed_mapping(dst
)) {
2863 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2864 "%s(writeBuffer is mapped)", func
);
2868 if (readOffset
< 0) {
2869 _mesa_error(ctx
, GL_INVALID_VALUE
,
2870 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2874 if (writeOffset
< 0) {
2875 _mesa_error(ctx
, GL_INVALID_VALUE
,
2876 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2881 _mesa_error(ctx
, GL_INVALID_VALUE
,
2882 "%s(size %d < 0)", func
, (int) size
);
2886 if (readOffset
+ size
> src
->Size
) {
2887 _mesa_error(ctx
, GL_INVALID_VALUE
,
2888 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2889 (int) readOffset
, (int) size
, (int) src
->Size
);
2893 if (writeOffset
+ size
> dst
->Size
) {
2894 _mesa_error(ctx
, GL_INVALID_VALUE
,
2895 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2896 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2901 if (readOffset
+ size
<= writeOffset
) {
2904 else if (writeOffset
+ size
<= readOffset
) {
2908 /* overlapping src/dst is illegal */
2909 _mesa_error(ctx
, GL_INVALID_VALUE
,
2910 "%s(overlapping src/dst)", func
);
2915 dst
->MinMaxCacheDirty
= true;
2917 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2921 _mesa_CopyBufferSubData_no_error(GLenum readTarget
, GLenum writeTarget
,
2922 GLintptr readOffset
, GLintptr writeOffset
,
2925 GET_CURRENT_CONTEXT(ctx
);
2927 struct gl_buffer_object
**src_ptr
= get_buffer_target(ctx
, readTarget
);
2928 struct gl_buffer_object
*src
= *src_ptr
;
2930 struct gl_buffer_object
**dst_ptr
= get_buffer_target(ctx
, writeTarget
);
2931 struct gl_buffer_object
*dst
= *dst_ptr
;
2933 dst
->MinMaxCacheDirty
= true;
2934 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2939 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2940 GLintptr readOffset
, GLintptr writeOffset
,
2943 GET_CURRENT_CONTEXT(ctx
);
2944 struct gl_buffer_object
*src
, *dst
;
2946 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2947 GL_INVALID_OPERATION
);
2951 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2952 GL_INVALID_OPERATION
);
2956 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2957 "glCopyBufferSubData");
2961 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer
, GLuint writeBuffer
,
2962 GLintptr readOffset
,
2963 GLintptr writeOffset
, GLsizeiptr size
)
2965 GET_CURRENT_CONTEXT(ctx
);
2967 struct gl_buffer_object
*src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
2968 struct gl_buffer_object
*dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
2970 dst
->MinMaxCacheDirty
= true;
2971 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2976 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2977 GLintptr readOffset
, GLintptr writeOffset
,
2980 GET_CURRENT_CONTEXT(ctx
);
2981 struct gl_buffer_object
*src
, *dst
;
2983 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2984 "glCopyNamedBufferSubData");
2988 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2989 "glCopyNamedBufferSubData");
2993 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2994 "glCopyNamedBufferSubData");
2998 validate_map_buffer_range(struct gl_context
*ctx
,
2999 struct gl_buffer_object
*bufObj
, GLintptr offset
,
3000 GLsizeiptr length
, GLbitfield access
,
3003 GLbitfield allowed_access
;
3005 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, false);
3008 _mesa_error(ctx
, GL_INVALID_VALUE
,
3009 "%s(offset %ld < 0)", func
, (long) offset
);
3014 _mesa_error(ctx
, GL_INVALID_VALUE
,
3015 "%s(length %ld < 0)", func
, (long) length
);
3019 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
3021 * "An INVALID_OPERATION error is generated for any of the following
3024 * * <length> is zero."
3026 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
3027 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
3031 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
3035 allowed_access
= GL_MAP_READ_BIT
|
3037 GL_MAP_INVALIDATE_RANGE_BIT
|
3038 GL_MAP_INVALIDATE_BUFFER_BIT
|
3039 GL_MAP_FLUSH_EXPLICIT_BIT
|
3040 GL_MAP_UNSYNCHRONIZED_BIT
;
3042 if (ctx
->Extensions
.ARB_buffer_storage
) {
3043 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
3044 GL_MAP_COHERENT_BIT
;
3047 if (access
& ~allowed_access
) {
3048 /* generate an error if any bits other than those allowed are set */
3049 _mesa_error(ctx
, GL_INVALID_VALUE
,
3050 "%s(access has undefined bits set)", func
);
3054 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
3055 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3056 "%s(access indicates neither read or write)", func
);
3060 if ((access
& GL_MAP_READ_BIT
) &&
3061 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
3062 GL_MAP_INVALIDATE_BUFFER_BIT
|
3063 GL_MAP_UNSYNCHRONIZED_BIT
))) {
3064 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3065 "%s(read access with disallowed bits)", func
);
3069 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
3070 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
3071 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3072 "%s(access has flush explicit without write)", func
);
3076 if (access
& GL_MAP_READ_BIT
&&
3077 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
3078 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3079 "%s(buffer does not allow read access)", func
);
3083 if (access
& GL_MAP_WRITE_BIT
&&
3084 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
3085 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3086 "%s(buffer does not allow write access)", func
);
3090 if (access
& GL_MAP_COHERENT_BIT
&&
3091 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
3092 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3093 "%s(buffer does not allow coherent access)", func
);
3097 if (access
& GL_MAP_PERSISTENT_BIT
&&
3098 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
3099 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3100 "%s(buffer does not allow persistent access)", func
);
3104 if (offset
+ length
> bufObj
->Size
) {
3105 _mesa_error(ctx
, GL_INVALID_VALUE
,
3106 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
3107 (unsigned long) offset
, (unsigned long) length
,
3108 (unsigned long) bufObj
->Size
);
3112 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3113 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3114 "%s(buffer already mapped)", func
);
3118 if (access
& GL_MAP_WRITE_BIT
) {
3119 bufObj
->NumMapBufferWriteCalls
++;
3120 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
3121 bufObj
->Usage
== GL_STATIC_COPY
) &&
3122 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
3123 BUFFER_USAGE_WARNING(ctx
,
3124 "using %s(buffer %u, offset %u, length %u) to "
3125 "update a %s buffer",
3126 func
, bufObj
->Name
, offset
, length
,
3127 _mesa_enum_to_string(bufObj
->Usage
));
3135 map_buffer_range(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
3136 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
3139 if (!bufObj
->Size
) {
3140 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
3144 assert(ctx
->Driver
.MapBufferRange
);
3145 void *map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
3148 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
3151 /* The driver callback should have set all these fields.
3152 * This is important because other modules (like VBO) might call
3153 * the driver function directly.
3155 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
3156 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
3157 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
3158 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
3161 if (access
& GL_MAP_WRITE_BIT
) {
3162 bufObj
->Written
= GL_TRUE
;
3163 bufObj
->MinMaxCacheDirty
= true;
3167 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3168 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
3169 bufObj
->Name
, bufObj
->Size
, access
);
3170 /* Access must be write only */
3171 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
3173 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
3174 for (i
= 0; i
< bufObj
->Size
; i
++)
3181 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3182 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
3184 /* buffer is 100 bytes larger than requested, fill with magic value */
3185 for (i
= 0; i
< 100; i
++) {
3186 buf
[bufObj
->Size
- i
- 1] = 123;
3195 _mesa_MapBufferRange_no_error(GLenum target
, GLintptr offset
,
3196 GLsizeiptr length
, GLbitfield access
)
3198 GET_CURRENT_CONTEXT(ctx
);
3200 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3201 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3203 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3204 "glMapBufferRange");
3208 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
3211 GET_CURRENT_CONTEXT(ctx
);
3212 struct gl_buffer_object
*bufObj
;
3214 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3215 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3216 "glMapBufferRange(ARB_map_buffer_range not supported)");
3220 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
3224 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3225 "glMapBufferRange"))
3228 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3229 "glMapBufferRange");
3233 _mesa_MapNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3234 GLsizeiptr length
, GLbitfield access
)
3236 GET_CURRENT_CONTEXT(ctx
);
3237 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3239 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3240 "glMapNamedBufferRange");
3244 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3247 GET_CURRENT_CONTEXT(ctx
);
3248 struct gl_buffer_object
*bufObj
;
3250 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3251 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3252 "glMapNamedBufferRange("
3253 "ARB_map_buffer_range not supported)");
3257 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
3261 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3262 "glMapNamedBufferRange"))
3265 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3266 "glMapNamedBufferRange");
3270 * Converts GLenum access from MapBuffer and MapNamedBuffer into
3271 * flags for input to map_buffer_range.
3273 * \return true if the type of requested access is permissible.
3276 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
3280 case GL_READ_ONLY_ARB
:
3281 *flags
= GL_MAP_READ_BIT
;
3282 return _mesa_is_desktop_gl(ctx
);
3283 case GL_WRITE_ONLY_ARB
:
3284 *flags
= GL_MAP_WRITE_BIT
;
3286 case GL_READ_WRITE_ARB
:
3287 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
3288 return _mesa_is_desktop_gl(ctx
);
3295 _mesa_MapBuffer_no_error(GLenum target
, GLenum access
)
3297 GET_CURRENT_CONTEXT(ctx
);
3299 GLbitfield accessFlags
;
3300 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3302 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3303 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3305 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3310 _mesa_MapBuffer(GLenum target
, GLenum access
)
3312 GET_CURRENT_CONTEXT(ctx
);
3313 struct gl_buffer_object
*bufObj
;
3314 GLbitfield accessFlags
;
3316 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3317 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
3321 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
3325 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3329 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3334 _mesa_MapNamedBuffer_no_error(GLuint buffer
, GLenum access
)
3336 GET_CURRENT_CONTEXT(ctx
);
3338 GLbitfield accessFlags
;
3339 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3341 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3343 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3344 "glMapNamedBuffer");
3348 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
3350 GET_CURRENT_CONTEXT(ctx
);
3351 struct gl_buffer_object
*bufObj
;
3352 GLbitfield accessFlags
;
3354 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3355 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
3359 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
3363 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3364 "glMapNamedBuffer"))
3367 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3368 "glMapNamedBuffer");
3373 flush_mapped_buffer_range(struct gl_context
*ctx
,
3374 struct gl_buffer_object
*bufObj
,
3375 GLintptr offset
, GLsizeiptr length
,
3378 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3379 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3380 "%s(ARB_map_buffer_range not supported)", func
);
3385 _mesa_error(ctx
, GL_INVALID_VALUE
,
3386 "%s(offset %ld < 0)", func
, (long) offset
);
3391 _mesa_error(ctx
, GL_INVALID_VALUE
,
3392 "%s(length %ld < 0)", func
, (long) length
);
3396 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3397 /* buffer is not mapped */
3398 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3399 "%s(buffer is not mapped)", func
);
3403 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
3404 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
3405 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3406 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
3410 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
3411 _mesa_error(ctx
, GL_INVALID_VALUE
,
3412 "%s(offset %ld + length %ld > mapped length %ld)", func
,
3413 (long) offset
, (long) length
,
3414 (long) bufObj
->Mappings
[MAP_USER
].Length
);
3418 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
3420 if (ctx
->Driver
.FlushMappedBufferRange
)
3421 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3426 _mesa_FlushMappedBufferRange_no_error(GLenum target
, GLintptr offset
,
3429 GET_CURRENT_CONTEXT(ctx
);
3430 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3431 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3433 if (ctx
->Driver
.FlushMappedBufferRange
)
3434 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3439 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
3442 GET_CURRENT_CONTEXT(ctx
);
3443 struct gl_buffer_object
*bufObj
;
3445 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
3446 GL_INVALID_OPERATION
);
3450 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3451 "glFlushMappedBufferRange");
3455 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3458 GET_CURRENT_CONTEXT(ctx
);
3459 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3461 if (ctx
->Driver
.FlushMappedBufferRange
)
3462 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3467 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
3470 GET_CURRENT_CONTEXT(ctx
);
3471 struct gl_buffer_object
*bufObj
;
3473 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
3474 "glFlushMappedNamedBufferRange");
3478 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3479 "glFlushMappedNamedBufferRange");
3483 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
, GLuint index
,
3484 struct gl_buffer_object
*bufObj
,
3485 GLintptr offset
, GLsizeiptr size
)
3487 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3492 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3493 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3497 * Bind a region of a buffer object to a uniform block binding point.
3498 * \param index the uniform buffer binding point index
3499 * \param bufObj the buffer object
3500 * \param offset offset to the start of buffer object region
3501 * \param size size of the buffer object region
3504 bind_buffer_range_uniform_buffer_err(struct gl_context
*ctx
, GLuint index
,
3505 struct gl_buffer_object
*bufObj
,
3506 GLintptr offset
, GLsizeiptr size
)
3508 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3509 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3513 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3514 _mesa_error(ctx
, GL_INVALID_VALUE
,
3515 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3516 ctx
->Const
.UniformBufferOffsetAlignment
);
3520 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3524 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3526 struct gl_buffer_object
*bufObj
,
3530 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3535 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3536 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3540 * Bind a region of a buffer object to a shader storage block binding point.
3541 * \param index the shader storage buffer binding point index
3542 * \param bufObj the buffer object
3543 * \param offset offset to the start of buffer object region
3544 * \param size size of the buffer object region
3547 bind_buffer_range_shader_storage_buffer_err(struct gl_context
*ctx
,
3549 struct gl_buffer_object
*bufObj
,
3550 GLintptr offset
, GLsizeiptr size
)
3552 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3553 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3557 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3558 _mesa_error(ctx
, GL_INVALID_VALUE
,
3559 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3560 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3564 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3568 * Binds a buffer object to an atomic buffer binding point.
3570 * Unlike set_atomic_buffer_binding(), this function also validates the
3571 * index and offset, flushes vertices, and updates NewDriverState.
3572 * It also checks if the binding has actually changing before
3576 bind_atomic_buffer_err(struct gl_context
*ctx
, unsigned index
,
3577 struct gl_buffer_object
*bufObj
, GLintptr offset
,
3578 GLsizeiptr size
, const char *name
)
3580 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3581 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3585 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3586 _mesa_error(ctx
, GL_INVALID_VALUE
,
3587 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3588 ATOMIC_COUNTER_SIZE
);
3592 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
3596 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3598 const GLintptr
*offsets
,
3599 const GLsizeiptr
*sizes
)
3601 if (offsets
[index
] < 0) {
3602 /* The ARB_multi_bind spec says:
3604 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3605 * value in <offsets> is less than zero (per binding)."
3607 _mesa_error(ctx
, GL_INVALID_VALUE
,
3608 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3609 index
, (int64_t) offsets
[index
]);
3613 if (sizes
[index
] <= 0) {
3614 /* The ARB_multi_bind spec says:
3616 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3617 * value in <sizes> is less than or equal to zero (per binding)."
3619 _mesa_error(ctx
, GL_INVALID_VALUE
,
3620 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3621 index
, (int64_t) sizes
[index
]);
3629 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3630 GLuint first
, GLsizei count
,
3633 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3634 _mesa_error(ctx
, GL_INVALID_ENUM
,
3635 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3639 /* The ARB_multi_bind_spec says:
3641 * "An INVALID_OPERATION error is generated if <first> + <count> is
3642 * greater than the number of target-specific indexed binding points,
3643 * as described in section 6.7.1."
3645 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3646 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3647 "%s(first=%u + count=%d > the value of "
3648 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3649 caller
, first
, count
,
3650 ctx
->Const
.MaxUniformBufferBindings
);
3658 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3659 GLuint first
, GLsizei count
,
3662 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3663 _mesa_error(ctx
, GL_INVALID_ENUM
,
3664 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3668 /* The ARB_multi_bind_spec says:
3670 * "An INVALID_OPERATION error is generated if <first> + <count> is
3671 * greater than the number of target-specific indexed binding points,
3672 * as described in section 6.7.1."
3674 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3675 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3676 "%s(first=%u + count=%d > the value of "
3677 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3678 caller
, first
, count
,
3679 ctx
->Const
.MaxShaderStorageBufferBindings
);
3687 * Unbind all uniform buffers in the range
3688 * <first> through <first>+<count>-1
3691 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3693 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3695 for (int i
= 0; i
< count
; i
++)
3696 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3697 bufObj
, -1, -1, GL_TRUE
);
3701 * Unbind all shader storage buffers in the range
3702 * <first> through <first>+<count>-1
3705 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3708 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3710 for (int i
= 0; i
< count
; i
++)
3711 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3712 bufObj
, -1, -1, GL_TRUE
);
3716 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3717 const GLuint
*buffers
,
3719 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3722 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3725 /* Assume that at least one binding will be changed */
3726 FLUSH_VERTICES(ctx
, 0);
3727 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3730 /* The ARB_multi_bind spec says:
3732 * "If <buffers> is NULL, all bindings from <first> through
3733 * <first>+<count>-1 are reset to their unbound (zero) state.
3734 * In this case, the offsets and sizes associated with the
3735 * binding points are set to default values, ignoring
3736 * <offsets> and <sizes>."
3738 unbind_uniform_buffers(ctx
, first
, count
);
3742 /* Note that the error semantics for multi-bind commands differ from
3743 * those of other GL commands.
3745 * The Issues section in the ARB_multi_bind spec says:
3747 * "(11) Typically, OpenGL specifies that if an error is generated by a
3748 * command, that command has no effect. This is somewhat
3749 * unfortunate for multi-bind commands, because it would require a
3750 * first pass to scan the entire list of bound objects for errors
3751 * and then a second pass to actually perform the bindings.
3752 * Should we have different error semantics?
3754 * RESOLVED: Yes. In this specification, when the parameters for
3755 * one of the <count> binding points are invalid, that binding point
3756 * is not updated and an error will be generated. However, other
3757 * binding points in the same command will be updated if their
3758 * parameters are valid and no other error occurs."
3761 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3763 for (int i
= 0; i
< count
; i
++) {
3764 struct gl_uniform_buffer_binding
*binding
=
3765 &ctx
->UniformBufferBindings
[first
+ i
];
3766 struct gl_buffer_object
*bufObj
;
3767 GLintptr offset
= 0;
3768 GLsizeiptr size
= 0;
3771 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3774 /* The ARB_multi_bind spec says:
3776 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3777 * pair of values in <offsets> and <sizes> does not respectively
3778 * satisfy the constraints described for those parameters for the
3779 * specified target, as described in section 6.7.1 (per binding)."
3781 * Section 6.7.1 refers to table 6.5, which says:
3783 * "┌───────────────────────────────────────────────────────────────┐
3784 * │ Uniform buffer array bindings (see sec. 7.6) │
3785 * ├─────────────────────┬─────────────────────────────────────────┤
3787 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3788 * │ │ OFFSET_ALIGNMENT │
3790 * │ size restriction │ none │
3791 * └─────────────────────┴─────────────────────────────────────────┘"
3793 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3794 _mesa_error(ctx
, GL_INVALID_VALUE
,
3795 "glBindBuffersRange(offsets[%u]=%" PRId64
3796 " is misaligned; it must be a multiple of the value of "
3797 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3798 "target=GL_UNIFORM_BUFFER)",
3799 i
, (int64_t) offsets
[i
],
3800 ctx
->Const
.UniformBufferOffsetAlignment
);
3804 offset
= offsets
[i
];
3808 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3809 bufObj
= binding
->BufferObject
;
3811 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3814 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3815 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3817 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3821 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3825 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3826 GLsizei count
, const GLuint
*buffers
,
3828 const GLintptr
*offsets
,
3829 const GLsizeiptr
*sizes
,
3832 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3835 /* Assume that at least one binding will be changed */
3836 FLUSH_VERTICES(ctx
, 0);
3837 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3840 /* The ARB_multi_bind spec says:
3842 * "If <buffers> is NULL, all bindings from <first> through
3843 * <first>+<count>-1 are reset to their unbound (zero) state.
3844 * In this case, the offsets and sizes associated with the
3845 * binding points are set to default values, ignoring
3846 * <offsets> and <sizes>."
3848 unbind_shader_storage_buffers(ctx
, first
, count
);
3852 /* Note that the error semantics for multi-bind commands differ from
3853 * those of other GL commands.
3855 * The Issues section in the ARB_multi_bind spec says:
3857 * "(11) Typically, OpenGL specifies that if an error is generated by a
3858 * command, that command has no effect. This is somewhat
3859 * unfortunate for multi-bind commands, because it would require a
3860 * first pass to scan the entire list of bound objects for errors
3861 * and then a second pass to actually perform the bindings.
3862 * Should we have different error semantics?
3864 * RESOLVED: Yes. In this specification, when the parameters for
3865 * one of the <count> binding points are invalid, that binding point
3866 * is not updated and an error will be generated. However, other
3867 * binding points in the same command will be updated if their
3868 * parameters are valid and no other error occurs."
3871 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3873 for (int i
= 0; i
< count
; i
++) {
3874 struct gl_shader_storage_buffer_binding
*binding
=
3875 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3876 struct gl_buffer_object
*bufObj
;
3877 GLintptr offset
= 0;
3878 GLsizeiptr size
= 0;
3881 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3884 /* The ARB_multi_bind spec says:
3886 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3887 * pair of values in <offsets> and <sizes> does not respectively
3888 * satisfy the constraints described for those parameters for the
3889 * specified target, as described in section 6.7.1 (per binding)."
3891 * Section 6.7.1 refers to table 6.5, which says:
3893 * "┌───────────────────────────────────────────────────────────────┐
3894 * │ Shader storage buffer array bindings (see sec. 7.8) │
3895 * ├─────────────────────┬─────────────────────────────────────────┤
3897 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3898 * │ │ BUFFER_OFFSET_ALIGNMENT │
3900 * │ size restriction │ none │
3901 * └─────────────────────┴─────────────────────────────────────────┘"
3903 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3904 _mesa_error(ctx
, GL_INVALID_VALUE
,
3905 "glBindBuffersRange(offsets[%u]=%" PRId64
3906 " is misaligned; it must be a multiple of the value of "
3907 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3908 "target=GL_SHADER_STORAGE_BUFFER)",
3909 i
, (int64_t) offsets
[i
],
3910 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3914 offset
= offsets
[i
];
3918 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3919 bufObj
= binding
->BufferObject
;
3921 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3924 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3925 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3927 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3931 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3935 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3936 struct gl_transform_feedback_object
*tfObj
,
3937 GLuint first
, GLsizei count
, const char *caller
)
3939 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3940 _mesa_error(ctx
, GL_INVALID_ENUM
,
3941 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3945 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3947 * "An INVALID_OPERATION error is generated :
3950 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3951 * FEEDBACK_BUFFER and transform feedback is currently active."
3953 * We assume that this is also meant to apply to BindBuffersRange
3954 * and BindBuffersBase.
3956 if (tfObj
->Active
) {
3957 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3958 "%s(Changing transform feedback buffers while "
3959 "transform feedback is active)", caller
);
3963 /* The ARB_multi_bind_spec says:
3965 * "An INVALID_OPERATION error is generated if <first> + <count> is
3966 * greater than the number of target-specific indexed binding points,
3967 * as described in section 6.7.1."
3969 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3970 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3971 "%s(first=%u + count=%d > the value of "
3972 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3973 caller
, first
, count
,
3974 ctx
->Const
.MaxTransformFeedbackBuffers
);
3982 * Unbind all transform feedback buffers in the range
3983 * <first> through <first>+<count>-1
3986 unbind_xfb_buffers(struct gl_context
*ctx
,
3987 struct gl_transform_feedback_object
*tfObj
,
3988 GLuint first
, GLsizei count
)
3990 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3992 for (int i
= 0; i
< count
; i
++)
3993 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3998 bind_xfb_buffers(struct gl_context
*ctx
,
3999 GLuint first
, GLsizei count
,
4000 const GLuint
*buffers
,
4002 const GLintptr
*offsets
,
4003 const GLsizeiptr
*sizes
,
4006 struct gl_transform_feedback_object
*tfObj
=
4007 ctx
->TransformFeedback
.CurrentObject
;
4009 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
4012 /* Assume that at least one binding will be changed */
4013 FLUSH_VERTICES(ctx
, 0);
4014 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
4017 /* The ARB_multi_bind spec says:
4019 * "If <buffers> is NULL, all bindings from <first> through
4020 * <first>+<count>-1 are reset to their unbound (zero) state.
4021 * In this case, the offsets and sizes associated with the
4022 * binding points are set to default values, ignoring
4023 * <offsets> and <sizes>."
4025 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
4029 /* Note that the error semantics for multi-bind commands differ from
4030 * those of other GL commands.
4032 * The Issues section in the ARB_multi_bind spec says:
4034 * "(11) Typically, OpenGL specifies that if an error is generated by a
4035 * command, that command has no effect. This is somewhat
4036 * unfortunate for multi-bind commands, because it would require a
4037 * first pass to scan the entire list of bound objects for errors
4038 * and then a second pass to actually perform the bindings.
4039 * Should we have different error semantics?
4041 * RESOLVED: Yes. In this specification, when the parameters for
4042 * one of the <count> binding points are invalid, that binding point
4043 * is not updated and an error will be generated. However, other
4044 * binding points in the same command will be updated if their
4045 * parameters are valid and no other error occurs."
4048 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4050 for (int i
= 0; i
< count
; i
++) {
4051 const GLuint index
= first
+ i
;
4052 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
4053 struct gl_buffer_object
*bufObj
;
4054 GLintptr offset
= 0;
4055 GLsizeiptr size
= 0;
4058 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4061 /* The ARB_multi_bind spec says:
4063 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4064 * pair of values in <offsets> and <sizes> does not respectively
4065 * satisfy the constraints described for those parameters for the
4066 * specified target, as described in section 6.7.1 (per binding)."
4068 * Section 6.7.1 refers to table 6.5, which says:
4070 * "┌───────────────────────────────────────────────────────────────┐
4071 * │ Transform feedback array bindings (see sec. 13.2.2) │
4072 * ├───────────────────────┬───────────────────────────────────────┤
4074 * │ offset restriction │ multiple of 4 │
4076 * │ size restriction │ multiple of 4 │
4077 * └───────────────────────┴───────────────────────────────────────┘"
4079 if (offsets
[i
] & 0x3) {
4080 _mesa_error(ctx
, GL_INVALID_VALUE
,
4081 "glBindBuffersRange(offsets[%u]=%" PRId64
4082 " is misaligned; it must be a multiple of 4 when "
4083 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4084 i
, (int64_t) offsets
[i
]);
4088 if (sizes
[i
] & 0x3) {
4089 _mesa_error(ctx
, GL_INVALID_VALUE
,
4090 "glBindBuffersRange(sizes[%u]=%" PRId64
4091 " is misaligned; it must be a multiple of 4 when "
4092 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4093 i
, (int64_t) sizes
[i
]);
4097 offset
= offsets
[i
];
4101 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
4102 bufObj
= boundBufObj
;
4104 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
4107 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
4111 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4115 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
4116 GLuint first
, GLsizei count
,
4119 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
4120 _mesa_error(ctx
, GL_INVALID_ENUM
,
4121 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
4125 /* The ARB_multi_bind_spec says:
4127 * "An INVALID_OPERATION error is generated if <first> + <count> is
4128 * greater than the number of target-specific indexed binding points,
4129 * as described in section 6.7.1."
4131 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
4132 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4133 "%s(first=%u + count=%d > the value of "
4134 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4135 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
4143 * Unbind all atomic counter buffers in the range
4144 * <first> through <first>+<count>-1
4147 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
4149 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
4151 for (int i
= 0; i
< count
; i
++)
4152 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
4157 bind_atomic_buffers(struct gl_context
*ctx
,
4160 const GLuint
*buffers
,
4162 const GLintptr
*offsets
,
4163 const GLsizeiptr
*sizes
,
4166 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
4169 /* Assume that at least one binding will be changed */
4170 FLUSH_VERTICES(ctx
, 0);
4171 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
4174 /* The ARB_multi_bind spec says:
4176 * "If <buffers> is NULL, all bindings from <first> through
4177 * <first>+<count>-1 are reset to their unbound (zero) state.
4178 * In this case, the offsets and sizes associated with the
4179 * binding points are set to default values, ignoring
4180 * <offsets> and <sizes>."
4182 unbind_atomic_buffers(ctx
, first
, count
);
4186 /* Note that the error semantics for multi-bind commands differ from
4187 * those of other GL commands.
4189 * The Issues section in the ARB_multi_bind spec says:
4191 * "(11) Typically, OpenGL specifies that if an error is generated by a
4192 * command, that command has no effect. This is somewhat
4193 * unfortunate for multi-bind commands, because it would require a
4194 * first pass to scan the entire list of bound objects for errors
4195 * and then a second pass to actually perform the bindings.
4196 * Should we have different error semantics?
4198 * RESOLVED: Yes. In this specification, when the parameters for
4199 * one of the <count> binding points are invalid, that binding point
4200 * is not updated and an error will be generated. However, other
4201 * binding points in the same command will be updated if their
4202 * parameters are valid and no other error occurs."
4205 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4207 for (int i
= 0; i
< count
; i
++) {
4208 struct gl_atomic_buffer_binding
*binding
=
4209 &ctx
->AtomicBufferBindings
[first
+ i
];
4210 struct gl_buffer_object
*bufObj
;
4211 GLintptr offset
= 0;
4212 GLsizeiptr size
= 0;
4215 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4218 /* The ARB_multi_bind spec says:
4220 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4221 * pair of values in <offsets> and <sizes> does not respectively
4222 * satisfy the constraints described for those parameters for the
4223 * specified target, as described in section 6.7.1 (per binding)."
4225 * Section 6.7.1 refers to table 6.5, which says:
4227 * "┌───────────────────────────────────────────────────────────────┐
4228 * │ Atomic counter array bindings (see sec. 7.7.2) │
4229 * ├───────────────────────┬───────────────────────────────────────┤
4231 * │ offset restriction │ multiple of 4 │
4233 * │ size restriction │ none │
4234 * └───────────────────────┴───────────────────────────────────────┘"
4236 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
4237 _mesa_error(ctx
, GL_INVALID_VALUE
,
4238 "glBindBuffersRange(offsets[%u]=%" PRId64
4239 " is misaligned; it must be a multiple of %d when "
4240 "target=GL_ATOMIC_COUNTER_BUFFER)",
4241 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
4245 offset
= offsets
[i
];
4249 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
4250 bufObj
= binding
->BufferObject
;
4252 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
4255 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
4258 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4261 static ALWAYS_INLINE
void
4262 bind_buffer_range(GLenum target
, GLuint index
, GLuint buffer
, GLintptr offset
,
4263 GLsizeiptr size
, bool no_error
)
4265 GET_CURRENT_CONTEXT(ctx
);
4266 struct gl_buffer_object
*bufObj
;
4268 if (MESA_VERBOSE
& VERBOSE_API
) {
4269 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4270 _mesa_enum_to_string(target
), index
, buffer
,
4271 (unsigned long) offset
, (unsigned long) size
);
4275 bufObj
= ctx
->Shared
->NullBufferObj
;
4277 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4278 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4279 &bufObj
, "glBindBufferRange"))
4282 if (!no_error
&& !bufObj
) {
4283 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4284 "glBindBufferRange(invalid buffer=%u)", buffer
);
4291 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4292 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4293 index
, bufObj
, offset
, size
);
4295 case GL_UNIFORM_BUFFER
:
4296 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
4298 case GL_SHADER_STORAGE_BUFFER
:
4299 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
,
4302 case GL_ATOMIC_COUNTER_BUFFER
:
4303 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
4306 unreachable("invalid BindBufferRange target with KHR_no_error");
4311 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
4318 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4319 if (!_mesa_validate_buffer_range_xfb(ctx
,
4320 ctx
->TransformFeedback
.CurrentObject
,
4321 index
, bufObj
, offset
, size
,
4325 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4326 index
, bufObj
, offset
, size
);
4328 case GL_UNIFORM_BUFFER
:
4329 bind_buffer_range_uniform_buffer_err(ctx
, index
, bufObj
, offset
,
4332 case GL_SHADER_STORAGE_BUFFER
:
4333 bind_buffer_range_shader_storage_buffer_err(ctx
, index
, bufObj
,
4336 case GL_ATOMIC_COUNTER_BUFFER
:
4337 bind_atomic_buffer_err(ctx
, index
, bufObj
, offset
, size
,
4338 "glBindBufferRange");
4341 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
4348 _mesa_BindBufferRange_no_error(GLenum target
, GLuint index
, GLuint buffer
,
4349 GLintptr offset
, GLsizeiptr size
)
4351 bind_buffer_range(target
, index
, buffer
, offset
, size
, true);
4355 _mesa_BindBufferRange(GLenum target
, GLuint index
,
4356 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
4358 bind_buffer_range(target
, index
, buffer
, offset
, size
, false);
4362 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
4364 GET_CURRENT_CONTEXT(ctx
);
4365 struct gl_buffer_object
*bufObj
;
4367 if (MESA_VERBOSE
& VERBOSE_API
) {
4368 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
4369 _mesa_enum_to_string(target
), index
, buffer
);
4373 bufObj
= ctx
->Shared
->NullBufferObj
;
4375 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4376 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4377 &bufObj
, "glBindBufferBase"))
4381 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4382 "glBindBufferBase(invalid buffer=%u)", buffer
);
4387 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4388 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4390 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4391 * zero and size equal to the size of buffer."
4393 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4395 * "If the parameter (starting offset or size) was not specified when the
4396 * buffer object was bound, zero is returned."
4398 * What happens if the size of the buffer changes? Does the size of the
4399 * buffer at the moment glBindBufferBase was called still play a role, like
4400 * the first quote would imply, or is the size meaningless in the
4401 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4402 * core spec page 45 says:
4404 * "It is equivalent to calling BindBufferRange with offset zero, while
4405 * size is determined by the size of the bound buffer at the time the
4408 * My interpretation is that the GL 4.1 spec was a clarification of the
4409 * behavior, not a change. In particular, this choice will only make
4410 * rendering work in cases where it would have had undefined results.
4414 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4415 _mesa_bind_buffer_base_transform_feedback(ctx
,
4416 ctx
->TransformFeedback
.CurrentObject
,
4417 index
, bufObj
, false);
4419 case GL_UNIFORM_BUFFER
:
4420 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4422 case GL_SHADER_STORAGE_BUFFER
:
4423 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4425 case GL_ATOMIC_COUNTER_BUFFER
:
4426 bind_atomic_buffer_err(ctx
, index
, bufObj
, 0, 0,
4427 "glBindBufferBase");
4430 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4436 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4437 const GLuint
*buffers
,
4438 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4440 GET_CURRENT_CONTEXT(ctx
);
4442 if (MESA_VERBOSE
& VERBOSE_API
) {
4443 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4444 _mesa_enum_to_string(target
), first
, count
,
4445 buffers
, offsets
, sizes
);
4449 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4450 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4451 "glBindBuffersRange");
4453 case GL_UNIFORM_BUFFER
:
4454 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4455 "glBindBuffersRange");
4457 case GL_SHADER_STORAGE_BUFFER
:
4458 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4459 "glBindBuffersRange");
4461 case GL_ATOMIC_COUNTER_BUFFER
:
4462 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4463 "glBindBuffersRange");
4466 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4467 _mesa_enum_to_string(target
));
4473 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4474 const GLuint
*buffers
)
4476 GET_CURRENT_CONTEXT(ctx
);
4478 if (MESA_VERBOSE
& VERBOSE_API
) {
4479 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4480 _mesa_enum_to_string(target
), first
, count
, buffers
);
4484 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4485 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4486 "glBindBuffersBase");
4488 case GL_UNIFORM_BUFFER
:
4489 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4490 "glBindBuffersBase");
4492 case GL_SHADER_STORAGE_BUFFER
:
4493 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4494 "glBindBuffersBase");
4496 case GL_ATOMIC_COUNTER_BUFFER
:
4497 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4498 "glBindBuffersBase");
4501 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4502 _mesa_enum_to_string(target
));
4507 static ALWAYS_INLINE
void
4508 invalidate_buffer_subdata(struct gl_context
*ctx
,
4509 struct gl_buffer_object
*bufObj
, GLintptr offset
,
4512 if (ctx
->Driver
.InvalidateBufferSubData
)
4513 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4517 _mesa_InvalidateBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
4520 GET_CURRENT_CONTEXT(ctx
);
4522 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4523 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4527 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4530 GET_CURRENT_CONTEXT(ctx
);
4531 struct gl_buffer_object
*bufObj
;
4532 const GLintptr end
= offset
+ length
;
4534 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4535 * Profile) spec says:
4537 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4538 * name of an existing buffer object."
4540 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4541 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4542 _mesa_error(ctx
, GL_INVALID_VALUE
,
4543 "glInvalidateBufferSubData(name = %u) invalid object",
4548 /* The GL_ARB_invalidate_subdata spec says:
4550 * "An INVALID_VALUE error is generated if <offset> or <length> is
4551 * negative, or if <offset> + <length> is greater than the value of
4554 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
4555 _mesa_error(ctx
, GL_INVALID_VALUE
,
4556 "glInvalidateBufferSubData(invalid offset or length)");
4560 /* The OpenGL 4.4 (Core Profile) spec says:
4562 * "An INVALID_OPERATION error is generated if buffer is currently
4563 * mapped by MapBuffer or if the invalidate range intersects the range
4564 * currently mapped by MapBufferRange, unless it was mapped
4565 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4567 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4568 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4569 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4570 "glInvalidateBufferSubData(intersection with mapped "
4575 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4579 _mesa_InvalidateBufferData_no_error(GLuint buffer
)
4581 GET_CURRENT_CONTEXT(ctx
);
4583 struct gl_buffer_object
*bufObj
=_mesa_lookup_bufferobj(ctx
, buffer
);
4584 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4588 _mesa_InvalidateBufferData(GLuint buffer
)
4590 GET_CURRENT_CONTEXT(ctx
);
4591 struct gl_buffer_object
*bufObj
;
4593 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4594 * Profile) spec says:
4596 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4597 * name of an existing buffer object."
4599 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4600 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4601 _mesa_error(ctx
, GL_INVALID_VALUE
,
4602 "glInvalidateBufferData(name = %u) invalid object",
4607 /* The OpenGL 4.4 (Core Profile) spec says:
4609 * "An INVALID_OPERATION error is generated if buffer is currently
4610 * mapped by MapBuffer or if the invalidate range intersects the range
4611 * currently mapped by MapBufferRange, unless it was mapped
4612 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4614 if (_mesa_check_disallowed_mapping(bufObj
)) {
4615 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4616 "glInvalidateBufferData(intersection with mapped "
4621 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4625 buffer_page_commitment(struct gl_context
*ctx
,
4626 struct gl_buffer_object
*bufferObj
,
4627 GLintptr offset
, GLsizeiptr size
,
4628 GLboolean commit
, const char *func
)
4630 if (!(bufferObj
->StorageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)) {
4631 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(not a sparse buffer object)",
4636 if (size
< 0 || size
> bufferObj
->Size
||
4637 offset
< 0 || offset
> bufferObj
->Size
- size
) {
4638 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(out of bounds)",
4643 /* The GL_ARB_sparse_buffer extension specification says:
4645 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4646 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4647 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4648 * not extend to the end of the buffer's data store."
4650 if (offset
% ctx
->Const
.SparseBufferPageSize
!= 0) {
4651 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset not aligned to page size)",
4656 if (size
% ctx
->Const
.SparseBufferPageSize
!= 0 &&
4657 offset
+ size
!= bufferObj
->Size
) {
4658 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size not aligned to page size)",
4663 ctx
->Driver
.BufferPageCommitment(ctx
, bufferObj
, offset
, size
, commit
);
4667 _mesa_BufferPageCommitmentARB(GLenum target
, GLintptr offset
, GLsizeiptr size
,
4670 GET_CURRENT_CONTEXT(ctx
);
4671 struct gl_buffer_object
*bufferObj
;
4673 bufferObj
= get_buffer(ctx
, "glBufferPageCommitmentARB", target
,
4678 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4679 "glBufferPageCommitmentARB");
4683 _mesa_NamedBufferPageCommitmentARB(GLuint buffer
, GLintptr offset
,
4684 GLsizeiptr size
, GLboolean commit
)
4686 GET_CURRENT_CONTEXT(ctx
);
4687 struct gl_buffer_object
*bufferObj
;
4689 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4690 if (!bufferObj
|| bufferObj
== &DummyBufferObject
) {
4691 /* Note: the extension spec is not clear about the excpected error value. */
4692 _mesa_error(ctx
, GL_INVALID_VALUE
,
4693 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4698 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4699 "glNamedBufferPageCommitmentARB");