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
;
1876 /* From the EXT_external_objects spec:
1878 * "An INVALID_VALUE error is generated by BufferStorageMemEXT and
1879 * NamedBufferStorageMemEXT if <memory> is 0, or ..."
1881 if (!no_error
&& memory
== 0) {
1882 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(memory == 0)", func
);
1885 memObj
= _mesa_lookup_memory_object(ctx
, memory
);
1889 /* From the EXT_external_objects spec:
1891 * "An INVALID_OPERATION error is generated if <memory> names a
1892 * valid memory object which has no associated memory."
1894 if (!no_error
&& !memObj
->Immutable
) {
1895 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no associated memory)",
1903 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1905 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
1911 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
1912 bufObj
= *bufObjPtr
;
1914 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
1920 if (no_error
|| validate_buffer_storage(ctx
, bufObj
, size
, flags
, func
))
1921 buffer_storage(ctx
, bufObj
, memObj
, target
, size
, data
, flags
, offset
, func
);
1926 _mesa_BufferStorage_no_error(GLenum target
, GLsizeiptr size
,
1927 const GLvoid
*data
, GLbitfield flags
)
1929 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1930 false, false, true, "glBufferStorage");
1935 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1938 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1939 false, false, false, "glBufferStorage");
1944 _mesa_BufferStorageMemEXT(GLenum target
, GLsizeiptr size
,
1945 GLuint memory
, GLuint64 offset
)
1947 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1948 false, true, false, "glBufferStorageMemEXT");
1953 _mesa_BufferStorageMemEXT_no_error(GLenum target
, GLsizeiptr size
,
1954 GLuint memory
, GLuint64 offset
)
1956 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1957 false, true, true, "glBufferStorageMemEXT");
1962 _mesa_NamedBufferStorage_no_error(GLuint buffer
, GLsizeiptr size
,
1963 const GLvoid
*data
, GLbitfield flags
)
1965 /* In direct state access, buffer objects have an unspecified target
1966 * since they are not required to be bound.
1968 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1969 true, false, true, "glNamedBufferStorage");
1974 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1977 /* In direct state access, buffer objects have an unspecified target
1978 * since they are not required to be bound.
1980 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1981 true, false, false, "glNamedBufferStorage");
1985 _mesa_NamedBufferStorageMemEXT(GLuint buffer
, GLsizeiptr size
,
1986 GLuint memory
, GLuint64 offset
)
1988 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1989 true, true, false, "glNamedBufferStorageMemEXT");
1994 _mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer
, GLsizeiptr size
,
1995 GLuint memory
, GLuint64 offset
)
1997 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1998 true, true, true, "glNamedBufferStorageMemEXT");
2002 static ALWAYS_INLINE
void
2003 buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2004 GLenum target
, GLsizeiptr size
, const GLvoid
*data
, GLenum usage
,
2005 const char *func
, bool no_error
)
2009 if (MESA_VERBOSE
& VERBOSE_API
) {
2010 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
2012 _mesa_enum_to_string(target
),
2013 (long int) size
, data
,
2014 _mesa_enum_to_string(usage
));
2019 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
2024 case GL_STREAM_DRAW_ARB
:
2025 valid_usage
= (ctx
->API
!= API_OPENGLES
);
2027 case GL_STATIC_DRAW_ARB
:
2028 case GL_DYNAMIC_DRAW_ARB
:
2031 case GL_STREAM_READ_ARB
:
2032 case GL_STREAM_COPY_ARB
:
2033 case GL_STATIC_READ_ARB
:
2034 case GL_STATIC_COPY_ARB
:
2035 case GL_DYNAMIC_READ_ARB
:
2036 case GL_DYNAMIC_COPY_ARB
:
2037 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
2040 valid_usage
= false;
2045 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
2046 _mesa_enum_to_string(usage
));
2050 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
2051 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
2056 /* Unmap the existing buffer. We'll replace it now. Not an error. */
2057 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
2059 FLUSH_VERTICES(ctx
, 0);
2061 bufObj
->Written
= GL_TRUE
;
2062 bufObj
->MinMaxCacheDirty
= true;
2065 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
2066 bufObj
->Name
, size
, data
, usage
);
2073 assert(ctx
->Driver
.BufferData
);
2074 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
2077 GL_DYNAMIC_STORAGE_BIT
,
2079 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
2081 /* From GL_AMD_pinned_memory:
2083 * INVALID_OPERATION is generated by BufferData if <target> is
2084 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
2085 * mapped to the GPU address space.
2087 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2090 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2096 buffer_data_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2097 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2098 GLenum usage
, const char *func
)
2100 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, false);
2104 buffer_data_no_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2105 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2106 GLenum usage
, const char *func
)
2108 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, true);
2112 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2113 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2114 GLenum usage
, const char *func
)
2116 buffer_data_error(ctx
, bufObj
, target
, size
, data
, usage
, func
);
2120 _mesa_BufferData_no_error(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2123 GET_CURRENT_CONTEXT(ctx
);
2125 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2126 buffer_data_no_error(ctx
, *bufObj
, target
, size
, data
, usage
,
2131 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
2132 const GLvoid
*data
, GLenum usage
)
2134 GET_CURRENT_CONTEXT(ctx
);
2135 struct gl_buffer_object
*bufObj
;
2137 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
2141 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
2146 _mesa_NamedBufferData_no_error(GLuint buffer
, GLsizeiptr size
,
2147 const GLvoid
*data
, GLenum usage
)
2149 GET_CURRENT_CONTEXT(ctx
);
2151 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2152 buffer_data_no_error(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2153 "glNamedBufferData");
2157 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2160 GET_CURRENT_CONTEXT(ctx
);
2161 struct gl_buffer_object
*bufObj
;
2163 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
2167 /* In direct state access, buffer objects have an unspecified target since
2168 * they are not required to be bound.
2170 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2171 "glNamedBufferData");
2176 validate_buffer_sub_data(struct gl_context
*ctx
,
2177 struct gl_buffer_object
*bufObj
,
2178 GLintptr offset
, GLsizeiptr size
,
2181 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2183 /* error already recorded */
2187 if (bufObj
->Immutable
&&
2188 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
2189 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2193 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2194 bufObj
->Usage
== GL_STATIC_COPY
) &&
2195 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
- 1) {
2196 /* If the application declared the buffer as static draw/copy or stream
2197 * draw, it should not be frequently modified with glBufferSubData.
2199 BUFFER_USAGE_WARNING(ctx
,
2200 "using %s(buffer %u, offset %u, size %u) to "
2201 "update a %s buffer",
2202 func
, bufObj
->Name
, offset
, size
,
2203 _mesa_enum_to_string(bufObj
->Usage
));
2211 * Implementation for glBufferSubData and glNamedBufferSubData.
2213 * \param ctx GL context.
2214 * \param bufObj The buffer object.
2215 * \param offset Offset of the first byte of the subdata range.
2216 * \param size Size, in bytes, of the subdata range.
2217 * \param data The data store.
2218 * \param func Name of calling function for recording errors.
2222 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2223 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
)
2228 bufObj
->NumSubDataCalls
++;
2229 bufObj
->Written
= GL_TRUE
;
2230 bufObj
->MinMaxCacheDirty
= true;
2232 assert(ctx
->Driver
.BufferSubData
);
2233 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
2237 static ALWAYS_INLINE
void
2238 buffer_sub_data(GLenum target
, GLuint buffer
, GLintptr offset
,
2239 GLsizeiptr size
, const GLvoid
*data
,
2240 bool dsa
, bool no_error
, const char *func
)
2242 GET_CURRENT_CONTEXT(ctx
);
2243 struct gl_buffer_object
*bufObj
;
2247 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2249 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
2255 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2256 bufObj
= *bufObjPtr
;
2258 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
2264 if (no_error
|| validate_buffer_sub_data(ctx
, bufObj
, offset
, size
, func
))
2265 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2270 _mesa_BufferSubData_no_error(GLenum target
, GLintptr offset
,
2271 GLsizeiptr size
, const GLvoid
*data
)
2273 buffer_sub_data(target
, 0, offset
, size
, data
, false, true,
2279 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
2280 GLsizeiptr size
, const GLvoid
*data
)
2282 buffer_sub_data(target
, 0, offset
, size
, data
, false, false,
2287 _mesa_NamedBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
2288 GLsizeiptr size
, const GLvoid
*data
)
2290 buffer_sub_data(0, buffer
, offset
, size
, data
, true, true,
2291 "glNamedBufferSubData");
2295 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
2296 GLsizeiptr size
, const GLvoid
*data
)
2298 buffer_sub_data(0, buffer
, offset
, size
, data
, true, false,
2299 "glNamedBufferSubData");
2304 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
2305 GLsizeiptr size
, GLvoid
*data
)
2307 GET_CURRENT_CONTEXT(ctx
);
2308 struct gl_buffer_object
*bufObj
;
2310 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
2311 GL_INVALID_OPERATION
);
2315 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2316 "glGetBufferSubData")) {
2320 assert(ctx
->Driver
.GetBufferSubData
);
2321 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2325 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
2326 GLsizeiptr size
, GLvoid
*data
)
2328 GET_CURRENT_CONTEXT(ctx
);
2329 struct gl_buffer_object
*bufObj
;
2331 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2332 "glGetNamedBufferSubData");
2336 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2337 "glGetNamedBufferSubData")) {
2341 assert(ctx
->Driver
.GetBufferSubData
);
2342 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2347 * \param subdata true if caller is *SubData, false if *Data
2349 static ALWAYS_INLINE
void
2350 clear_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2351 GLenum internalformat
, GLintptr offset
, GLsizeiptr size
,
2352 GLenum format
, GLenum type
, const GLvoid
*data
,
2353 const char *func
, bool subdata
, bool no_error
)
2355 mesa_format mesaFormat
;
2356 GLubyte clearValue
[MAX_PIXEL_BYTES
];
2357 GLsizeiptr clearValueSize
;
2359 /* This checks for disallowed mappings. */
2360 if (!no_error
&& !buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2366 mesaFormat
= _mesa_get_texbuffer_format(ctx
, internalformat
);
2368 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
2369 format
, type
, func
);
2372 if (mesaFormat
== MESA_FORMAT_NONE
)
2375 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
2377 (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0)) {
2378 _mesa_error(ctx
, GL_INVALID_VALUE
,
2379 "%s(offset or size is not a multiple of "
2380 "internalformat size)", func
);
2384 /* Bail early. Negative size has already been checked. */
2388 bufObj
->MinMaxCacheDirty
= true;
2391 /* clear to zeros, per the spec */
2392 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2393 NULL
, clearValueSize
, bufObj
);
2397 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
2398 format
, type
, data
, func
)) {
2402 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2403 clearValue
, clearValueSize
, bufObj
);
2407 clear_buffer_sub_data_error(struct gl_context
*ctx
,
2408 struct gl_buffer_object
*bufObj
,
2409 GLenum internalformat
, GLintptr offset
,
2410 GLsizeiptr size
, GLenum format
, GLenum type
,
2411 const GLvoid
*data
, const char *func
, bool subdata
)
2413 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2414 type
, data
, func
, subdata
, false);
2419 clear_buffer_sub_data_no_error(struct gl_context
*ctx
,
2420 struct gl_buffer_object
*bufObj
,
2421 GLenum internalformat
, GLintptr offset
,
2422 GLsizeiptr size
, GLenum format
, GLenum type
,
2423 const GLvoid
*data
, const char *func
,
2426 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2427 type
, data
, func
, subdata
, true);
2432 _mesa_ClearBufferData_no_error(GLenum target
, GLenum internalformat
,
2433 GLenum format
, GLenum type
, const GLvoid
*data
)
2435 GET_CURRENT_CONTEXT(ctx
);
2437 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2438 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, 0,
2439 (*bufObj
)->Size
, format
, type
, data
,
2440 "glClearBufferData", false);
2445 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
2446 GLenum type
, const GLvoid
*data
)
2448 GET_CURRENT_CONTEXT(ctx
);
2449 struct gl_buffer_object
*bufObj
;
2451 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
2455 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2456 format
, type
, data
, "glClearBufferData", false);
2461 _mesa_ClearNamedBufferData_no_error(GLuint buffer
, GLenum internalformat
,
2462 GLenum format
, GLenum type
,
2465 GET_CURRENT_CONTEXT(ctx
);
2467 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2468 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2469 format
, type
, data
, "glClearNamedBufferData",
2475 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
2476 GLenum format
, GLenum type
, const GLvoid
*data
)
2478 GET_CURRENT_CONTEXT(ctx
);
2479 struct gl_buffer_object
*bufObj
;
2481 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
2485 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2486 format
, type
, data
, "glClearNamedBufferData",
2492 _mesa_ClearBufferSubData_no_error(GLenum target
, GLenum internalformat
,
2493 GLintptr offset
, GLsizeiptr size
,
2494 GLenum format
, GLenum type
,
2497 GET_CURRENT_CONTEXT(ctx
);
2499 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2500 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, offset
, size
,
2501 format
, type
, data
, "glClearBufferSubData",
2507 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
2508 GLintptr offset
, GLsizeiptr size
,
2509 GLenum format
, GLenum type
,
2512 GET_CURRENT_CONTEXT(ctx
);
2513 struct gl_buffer_object
*bufObj
;
2515 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
2519 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2520 format
, type
, data
, "glClearBufferSubData",
2526 _mesa_ClearNamedBufferSubData_no_error(GLuint buffer
, GLenum internalformat
,
2527 GLintptr offset
, GLsizeiptr size
,
2528 GLenum format
, GLenum type
,
2531 GET_CURRENT_CONTEXT(ctx
);
2533 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2534 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, offset
, size
,
2536 "glClearNamedBufferSubData", true);
2541 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
2542 GLintptr offset
, GLsizeiptr size
,
2543 GLenum format
, GLenum type
,
2546 GET_CURRENT_CONTEXT(ctx
);
2547 struct gl_buffer_object
*bufObj
;
2549 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2550 "glClearNamedBufferSubData");
2554 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2555 format
, type
, data
, "glClearNamedBufferSubData",
2560 unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
2562 GLboolean status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2563 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2564 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2565 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2566 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2572 validate_and_unmap_buffer(struct gl_context
*ctx
,
2573 struct gl_buffer_object
*bufObj
,
2576 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2578 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2579 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2580 "%s(buffer is not mapped)", func
);
2585 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2586 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2588 /* check that last 100 bytes are still = magic value */
2589 for (i
= 0; i
< 100; i
++) {
2590 GLuint pos
= bufObj
->Size
- i
- 1;
2591 if (buf
[pos
] != 123) {
2592 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2593 " at position %d (value = %u)\n",
2601 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2602 GLuint i
, unchanged
= 0;
2603 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2605 /* check which bytes changed */
2606 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2607 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2614 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2615 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2620 return unmap_buffer(ctx
, bufObj
);
2623 GLboolean GLAPIENTRY
2624 _mesa_UnmapBuffer_no_error(GLenum target
)
2626 GET_CURRENT_CONTEXT(ctx
);
2627 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2628 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2630 return unmap_buffer(ctx
, bufObj
);
2633 GLboolean GLAPIENTRY
2634 _mesa_UnmapBuffer(GLenum target
)
2636 GET_CURRENT_CONTEXT(ctx
);
2637 struct gl_buffer_object
*bufObj
;
2639 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2643 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2646 GLboolean GLAPIENTRY
2647 _mesa_UnmapNamedBuffer_no_error(GLuint buffer
)
2649 GET_CURRENT_CONTEXT(ctx
);
2650 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2652 return unmap_buffer(ctx
, bufObj
);
2655 GLboolean GLAPIENTRY
2656 _mesa_UnmapNamedBuffer(GLuint buffer
)
2658 GET_CURRENT_CONTEXT(ctx
);
2659 struct gl_buffer_object
*bufObj
;
2661 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2665 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2670 get_buffer_parameter(struct gl_context
*ctx
,
2671 struct gl_buffer_object
*bufObj
, GLenum pname
,
2672 GLint64
*params
, const char *func
)
2675 case GL_BUFFER_SIZE_ARB
:
2676 *params
= bufObj
->Size
;
2678 case GL_BUFFER_USAGE_ARB
:
2679 *params
= bufObj
->Usage
;
2681 case GL_BUFFER_ACCESS_ARB
:
2682 *params
= simplified_access_mode(ctx
,
2683 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2685 case GL_BUFFER_MAPPED_ARB
:
2686 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2688 case GL_BUFFER_ACCESS_FLAGS
:
2689 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2691 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2693 case GL_BUFFER_MAP_OFFSET
:
2694 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2696 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2698 case GL_BUFFER_MAP_LENGTH
:
2699 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2701 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2703 case GL_BUFFER_IMMUTABLE_STORAGE
:
2704 if (!ctx
->Extensions
.ARB_buffer_storage
)
2706 *params
= bufObj
->Immutable
;
2708 case GL_BUFFER_STORAGE_FLAGS
:
2709 if (!ctx
->Extensions
.ARB_buffer_storage
)
2711 *params
= bufObj
->StorageFlags
;
2720 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2721 _mesa_enum_to_string(pname
));
2726 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2728 GET_CURRENT_CONTEXT(ctx
);
2729 struct gl_buffer_object
*bufObj
;
2732 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2733 GL_INVALID_OPERATION
);
2737 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2738 "glGetBufferParameteriv"))
2739 return; /* Error already recorded. */
2741 *params
= (GLint
) parameter
;
2745 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2747 GET_CURRENT_CONTEXT(ctx
);
2748 struct gl_buffer_object
*bufObj
;
2751 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2752 GL_INVALID_OPERATION
);
2756 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2757 "glGetBufferParameteri64v"))
2758 return; /* Error already recorded. */
2760 *params
= parameter
;
2764 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2766 GET_CURRENT_CONTEXT(ctx
);
2767 struct gl_buffer_object
*bufObj
;
2770 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2771 "glGetNamedBufferParameteriv");
2775 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2776 "glGetNamedBufferParameteriv"))
2777 return; /* Error already recorded. */
2779 *params
= (GLint
) parameter
;
2783 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2786 GET_CURRENT_CONTEXT(ctx
);
2787 struct gl_buffer_object
*bufObj
;
2790 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2791 "glGetNamedBufferParameteri64v");
2795 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2796 "glGetNamedBufferParameteri64v"))
2797 return; /* Error already recorded. */
2799 *params
= parameter
;
2804 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2806 GET_CURRENT_CONTEXT(ctx
);
2807 struct gl_buffer_object
*bufObj
;
2809 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2810 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2811 "GL_BUFFER_MAP_POINTER)");
2815 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2816 GL_INVALID_OPERATION
);
2820 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2824 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2826 GET_CURRENT_CONTEXT(ctx
);
2827 struct gl_buffer_object
*bufObj
;
2829 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2830 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2831 "GL_BUFFER_MAP_POINTER)");
2835 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2836 "glGetNamedBufferPointerv");
2840 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2845 copy_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*src
,
2846 struct gl_buffer_object
*dst
, GLintptr readOffset
,
2847 GLintptr writeOffset
, GLsizeiptr size
, const char *func
)
2849 if (_mesa_check_disallowed_mapping(src
)) {
2850 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2851 "%s(readBuffer is mapped)", func
);
2855 if (_mesa_check_disallowed_mapping(dst
)) {
2856 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2857 "%s(writeBuffer is mapped)", func
);
2861 if (readOffset
< 0) {
2862 _mesa_error(ctx
, GL_INVALID_VALUE
,
2863 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2867 if (writeOffset
< 0) {
2868 _mesa_error(ctx
, GL_INVALID_VALUE
,
2869 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2874 _mesa_error(ctx
, GL_INVALID_VALUE
,
2875 "%s(size %d < 0)", func
, (int) size
);
2879 if (readOffset
+ size
> src
->Size
) {
2880 _mesa_error(ctx
, GL_INVALID_VALUE
,
2881 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2882 (int) readOffset
, (int) size
, (int) src
->Size
);
2886 if (writeOffset
+ size
> dst
->Size
) {
2887 _mesa_error(ctx
, GL_INVALID_VALUE
,
2888 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2889 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2894 if (readOffset
+ size
<= writeOffset
) {
2897 else if (writeOffset
+ size
<= readOffset
) {
2901 /* overlapping src/dst is illegal */
2902 _mesa_error(ctx
, GL_INVALID_VALUE
,
2903 "%s(overlapping src/dst)", func
);
2908 dst
->MinMaxCacheDirty
= true;
2910 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2914 _mesa_CopyBufferSubData_no_error(GLenum readTarget
, GLenum writeTarget
,
2915 GLintptr readOffset
, GLintptr writeOffset
,
2918 GET_CURRENT_CONTEXT(ctx
);
2920 struct gl_buffer_object
**src_ptr
= get_buffer_target(ctx
, readTarget
);
2921 struct gl_buffer_object
*src
= *src_ptr
;
2923 struct gl_buffer_object
**dst_ptr
= get_buffer_target(ctx
, writeTarget
);
2924 struct gl_buffer_object
*dst
= *dst_ptr
;
2926 dst
->MinMaxCacheDirty
= true;
2927 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2932 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2933 GLintptr readOffset
, GLintptr writeOffset
,
2936 GET_CURRENT_CONTEXT(ctx
);
2937 struct gl_buffer_object
*src
, *dst
;
2939 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2940 GL_INVALID_OPERATION
);
2944 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2945 GL_INVALID_OPERATION
);
2949 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2950 "glCopyBufferSubData");
2954 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer
, GLuint writeBuffer
,
2955 GLintptr readOffset
,
2956 GLintptr writeOffset
, GLsizeiptr size
)
2958 GET_CURRENT_CONTEXT(ctx
);
2960 struct gl_buffer_object
*src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
2961 struct gl_buffer_object
*dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
2963 dst
->MinMaxCacheDirty
= true;
2964 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2969 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2970 GLintptr readOffset
, GLintptr writeOffset
,
2973 GET_CURRENT_CONTEXT(ctx
);
2974 struct gl_buffer_object
*src
, *dst
;
2976 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2977 "glCopyNamedBufferSubData");
2981 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2982 "glCopyNamedBufferSubData");
2986 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2987 "glCopyNamedBufferSubData");
2991 validate_map_buffer_range(struct gl_context
*ctx
,
2992 struct gl_buffer_object
*bufObj
, GLintptr offset
,
2993 GLsizeiptr length
, GLbitfield access
,
2996 GLbitfield allowed_access
;
2998 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, false);
3001 _mesa_error(ctx
, GL_INVALID_VALUE
,
3002 "%s(offset %ld < 0)", func
, (long) offset
);
3007 _mesa_error(ctx
, GL_INVALID_VALUE
,
3008 "%s(length %ld < 0)", func
, (long) length
);
3012 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
3014 * "An INVALID_OPERATION error is generated for any of the following
3017 * * <length> is zero."
3019 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
3020 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
3024 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
3028 allowed_access
= GL_MAP_READ_BIT
|
3030 GL_MAP_INVALIDATE_RANGE_BIT
|
3031 GL_MAP_INVALIDATE_BUFFER_BIT
|
3032 GL_MAP_FLUSH_EXPLICIT_BIT
|
3033 GL_MAP_UNSYNCHRONIZED_BIT
;
3035 if (ctx
->Extensions
.ARB_buffer_storage
) {
3036 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
3037 GL_MAP_COHERENT_BIT
;
3040 if (access
& ~allowed_access
) {
3041 /* generate an error if any bits other than those allowed are set */
3042 _mesa_error(ctx
, GL_INVALID_VALUE
,
3043 "%s(access has undefined bits set)", func
);
3047 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
3048 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3049 "%s(access indicates neither read or write)", func
);
3053 if ((access
& GL_MAP_READ_BIT
) &&
3054 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
3055 GL_MAP_INVALIDATE_BUFFER_BIT
|
3056 GL_MAP_UNSYNCHRONIZED_BIT
))) {
3057 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3058 "%s(read access with disallowed bits)", func
);
3062 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
3063 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
3064 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3065 "%s(access has flush explicit without write)", func
);
3069 if (access
& GL_MAP_READ_BIT
&&
3070 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
3071 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3072 "%s(buffer does not allow read access)", func
);
3076 if (access
& GL_MAP_WRITE_BIT
&&
3077 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
3078 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3079 "%s(buffer does not allow write access)", func
);
3083 if (access
& GL_MAP_COHERENT_BIT
&&
3084 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
3085 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3086 "%s(buffer does not allow coherent access)", func
);
3090 if (access
& GL_MAP_PERSISTENT_BIT
&&
3091 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
3092 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3093 "%s(buffer does not allow persistent access)", func
);
3097 if (offset
+ length
> bufObj
->Size
) {
3098 _mesa_error(ctx
, GL_INVALID_VALUE
,
3099 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
3100 (unsigned long) offset
, (unsigned long) length
,
3101 (unsigned long) bufObj
->Size
);
3105 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3106 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3107 "%s(buffer already mapped)", func
);
3111 if (access
& GL_MAP_WRITE_BIT
) {
3112 bufObj
->NumMapBufferWriteCalls
++;
3113 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
3114 bufObj
->Usage
== GL_STATIC_COPY
) &&
3115 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
3116 BUFFER_USAGE_WARNING(ctx
,
3117 "using %s(buffer %u, offset %u, length %u) to "
3118 "update a %s buffer",
3119 func
, bufObj
->Name
, offset
, length
,
3120 _mesa_enum_to_string(bufObj
->Usage
));
3128 map_buffer_range(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
3129 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
3132 if (!bufObj
->Size
) {
3133 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
3137 assert(ctx
->Driver
.MapBufferRange
);
3138 void *map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
3141 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
3144 /* The driver callback should have set all these fields.
3145 * This is important because other modules (like VBO) might call
3146 * the driver function directly.
3148 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
3149 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
3150 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
3151 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
3154 if (access
& GL_MAP_WRITE_BIT
) {
3155 bufObj
->Written
= GL_TRUE
;
3156 bufObj
->MinMaxCacheDirty
= true;
3160 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3161 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
3162 bufObj
->Name
, bufObj
->Size
, access
);
3163 /* Access must be write only */
3164 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
3166 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
3167 for (i
= 0; i
< bufObj
->Size
; i
++)
3174 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3175 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
3177 /* buffer is 100 bytes larger than requested, fill with magic value */
3178 for (i
= 0; i
< 100; i
++) {
3179 buf
[bufObj
->Size
- i
- 1] = 123;
3188 _mesa_MapBufferRange_no_error(GLenum target
, GLintptr offset
,
3189 GLsizeiptr length
, GLbitfield access
)
3191 GET_CURRENT_CONTEXT(ctx
);
3193 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3194 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3196 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3197 "glMapBufferRange");
3201 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
3204 GET_CURRENT_CONTEXT(ctx
);
3205 struct gl_buffer_object
*bufObj
;
3207 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3208 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3209 "glMapBufferRange(ARB_map_buffer_range not supported)");
3213 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
3217 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3218 "glMapBufferRange"))
3221 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3222 "glMapBufferRange");
3226 _mesa_MapNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3227 GLsizeiptr length
, GLbitfield access
)
3229 GET_CURRENT_CONTEXT(ctx
);
3230 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3232 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3233 "glMapNamedBufferRange");
3237 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3240 GET_CURRENT_CONTEXT(ctx
);
3241 struct gl_buffer_object
*bufObj
;
3243 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3244 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3245 "glMapNamedBufferRange("
3246 "ARB_map_buffer_range not supported)");
3250 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
3254 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3255 "glMapNamedBufferRange"))
3258 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3259 "glMapNamedBufferRange");
3263 * Converts GLenum access from MapBuffer and MapNamedBuffer into
3264 * flags for input to map_buffer_range.
3266 * \return true if the type of requested access is permissible.
3269 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
3273 case GL_READ_ONLY_ARB
:
3274 *flags
= GL_MAP_READ_BIT
;
3275 return _mesa_is_desktop_gl(ctx
);
3276 case GL_WRITE_ONLY_ARB
:
3277 *flags
= GL_MAP_WRITE_BIT
;
3279 case GL_READ_WRITE_ARB
:
3280 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
3281 return _mesa_is_desktop_gl(ctx
);
3288 _mesa_MapBuffer_no_error(GLenum target
, GLenum access
)
3290 GET_CURRENT_CONTEXT(ctx
);
3292 GLbitfield accessFlags
;
3293 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3295 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3296 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3298 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3303 _mesa_MapBuffer(GLenum target
, GLenum access
)
3305 GET_CURRENT_CONTEXT(ctx
);
3306 struct gl_buffer_object
*bufObj
;
3307 GLbitfield accessFlags
;
3309 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3310 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
3314 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
3318 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3322 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3327 _mesa_MapNamedBuffer_no_error(GLuint buffer
, GLenum access
)
3329 GET_CURRENT_CONTEXT(ctx
);
3331 GLbitfield accessFlags
;
3332 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3334 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3336 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3337 "glMapNamedBuffer");
3341 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
3343 GET_CURRENT_CONTEXT(ctx
);
3344 struct gl_buffer_object
*bufObj
;
3345 GLbitfield accessFlags
;
3347 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3348 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
3352 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
3356 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3357 "glMapNamedBuffer"))
3360 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3361 "glMapNamedBuffer");
3366 flush_mapped_buffer_range(struct gl_context
*ctx
,
3367 struct gl_buffer_object
*bufObj
,
3368 GLintptr offset
, GLsizeiptr length
,
3371 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3372 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3373 "%s(ARB_map_buffer_range not supported)", func
);
3378 _mesa_error(ctx
, GL_INVALID_VALUE
,
3379 "%s(offset %ld < 0)", func
, (long) offset
);
3384 _mesa_error(ctx
, GL_INVALID_VALUE
,
3385 "%s(length %ld < 0)", func
, (long) length
);
3389 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3390 /* buffer is not mapped */
3391 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3392 "%s(buffer is not mapped)", func
);
3396 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
3397 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
3398 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3399 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
3403 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
3404 _mesa_error(ctx
, GL_INVALID_VALUE
,
3405 "%s(offset %ld + length %ld > mapped length %ld)", func
,
3406 (long) offset
, (long) length
,
3407 (long) bufObj
->Mappings
[MAP_USER
].Length
);
3411 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
3413 if (ctx
->Driver
.FlushMappedBufferRange
)
3414 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3419 _mesa_FlushMappedBufferRange_no_error(GLenum target
, GLintptr offset
,
3422 GET_CURRENT_CONTEXT(ctx
);
3423 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3424 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3426 if (ctx
->Driver
.FlushMappedBufferRange
)
3427 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3432 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
3435 GET_CURRENT_CONTEXT(ctx
);
3436 struct gl_buffer_object
*bufObj
;
3438 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
3439 GL_INVALID_OPERATION
);
3443 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3444 "glFlushMappedBufferRange");
3448 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3451 GET_CURRENT_CONTEXT(ctx
);
3452 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3454 if (ctx
->Driver
.FlushMappedBufferRange
)
3455 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3460 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
3463 GET_CURRENT_CONTEXT(ctx
);
3464 struct gl_buffer_object
*bufObj
;
3466 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
3467 "glFlushMappedNamedBufferRange");
3471 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3472 "glFlushMappedNamedBufferRange");
3476 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
, GLuint index
,
3477 struct gl_buffer_object
*bufObj
,
3478 GLintptr offset
, GLsizeiptr size
)
3480 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3485 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3486 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3490 * Bind a region of a buffer object to a uniform block binding point.
3491 * \param index the uniform buffer binding point index
3492 * \param bufObj the buffer object
3493 * \param offset offset to the start of buffer object region
3494 * \param size size of the buffer object region
3497 bind_buffer_range_uniform_buffer_err(struct gl_context
*ctx
, GLuint index
,
3498 struct gl_buffer_object
*bufObj
,
3499 GLintptr offset
, GLsizeiptr size
)
3501 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3502 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3506 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3507 _mesa_error(ctx
, GL_INVALID_VALUE
,
3508 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3509 ctx
->Const
.UniformBufferOffsetAlignment
);
3513 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3517 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3519 struct gl_buffer_object
*bufObj
,
3523 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3528 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3529 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3533 * Bind a region of a buffer object to a shader storage block binding point.
3534 * \param index the shader storage buffer binding point index
3535 * \param bufObj the buffer object
3536 * \param offset offset to the start of buffer object region
3537 * \param size size of the buffer object region
3540 bind_buffer_range_shader_storage_buffer_err(struct gl_context
*ctx
,
3542 struct gl_buffer_object
*bufObj
,
3543 GLintptr offset
, GLsizeiptr size
)
3545 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3546 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3550 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3551 _mesa_error(ctx
, GL_INVALID_VALUE
,
3552 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3553 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3557 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3561 * Binds a buffer object to an atomic buffer binding point.
3563 * Unlike set_atomic_buffer_binding(), this function also validates the
3564 * index and offset, flushes vertices, and updates NewDriverState.
3565 * It also checks if the binding has actually changing before
3569 bind_atomic_buffer_err(struct gl_context
*ctx
, unsigned index
,
3570 struct gl_buffer_object
*bufObj
, GLintptr offset
,
3571 GLsizeiptr size
, const char *name
)
3573 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3574 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3578 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3579 _mesa_error(ctx
, GL_INVALID_VALUE
,
3580 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3581 ATOMIC_COUNTER_SIZE
);
3585 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
3589 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3591 const GLintptr
*offsets
,
3592 const GLsizeiptr
*sizes
)
3594 if (offsets
[index
] < 0) {
3595 /* The ARB_multi_bind spec says:
3597 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3598 * value in <offsets> is less than zero (per binding)."
3600 _mesa_error(ctx
, GL_INVALID_VALUE
,
3601 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3602 index
, (int64_t) offsets
[index
]);
3606 if (sizes
[index
] <= 0) {
3607 /* The ARB_multi_bind spec says:
3609 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3610 * value in <sizes> is less than or equal to zero (per binding)."
3612 _mesa_error(ctx
, GL_INVALID_VALUE
,
3613 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3614 index
, (int64_t) sizes
[index
]);
3622 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3623 GLuint first
, GLsizei count
,
3626 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3627 _mesa_error(ctx
, GL_INVALID_ENUM
,
3628 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3632 /* The ARB_multi_bind_spec says:
3634 * "An INVALID_OPERATION error is generated if <first> + <count> is
3635 * greater than the number of target-specific indexed binding points,
3636 * as described in section 6.7.1."
3638 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3639 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3640 "%s(first=%u + count=%d > the value of "
3641 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3642 caller
, first
, count
,
3643 ctx
->Const
.MaxUniformBufferBindings
);
3651 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3652 GLuint first
, GLsizei count
,
3655 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3656 _mesa_error(ctx
, GL_INVALID_ENUM
,
3657 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3661 /* The ARB_multi_bind_spec says:
3663 * "An INVALID_OPERATION error is generated if <first> + <count> is
3664 * greater than the number of target-specific indexed binding points,
3665 * as described in section 6.7.1."
3667 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3668 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3669 "%s(first=%u + count=%d > the value of "
3670 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3671 caller
, first
, count
,
3672 ctx
->Const
.MaxShaderStorageBufferBindings
);
3680 * Unbind all uniform buffers in the range
3681 * <first> through <first>+<count>-1
3684 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3686 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3688 for (int i
= 0; i
< count
; i
++)
3689 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3690 bufObj
, -1, -1, GL_TRUE
);
3694 * Unbind all shader storage buffers in the range
3695 * <first> through <first>+<count>-1
3698 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3701 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3703 for (int i
= 0; i
< count
; i
++)
3704 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3705 bufObj
, -1, -1, GL_TRUE
);
3709 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3710 const GLuint
*buffers
,
3712 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3715 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3718 /* Assume that at least one binding will be changed */
3719 FLUSH_VERTICES(ctx
, 0);
3720 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3723 /* The ARB_multi_bind spec says:
3725 * "If <buffers> is NULL, all bindings from <first> through
3726 * <first>+<count>-1 are reset to their unbound (zero) state.
3727 * In this case, the offsets and sizes associated with the
3728 * binding points are set to default values, ignoring
3729 * <offsets> and <sizes>."
3731 unbind_uniform_buffers(ctx
, first
, count
);
3735 /* Note that the error semantics for multi-bind commands differ from
3736 * those of other GL commands.
3738 * The Issues section in the ARB_multi_bind spec says:
3740 * "(11) Typically, OpenGL specifies that if an error is generated by a
3741 * command, that command has no effect. This is somewhat
3742 * unfortunate for multi-bind commands, because it would require a
3743 * first pass to scan the entire list of bound objects for errors
3744 * and then a second pass to actually perform the bindings.
3745 * Should we have different error semantics?
3747 * RESOLVED: Yes. In this specification, when the parameters for
3748 * one of the <count> binding points are invalid, that binding point
3749 * is not updated and an error will be generated. However, other
3750 * binding points in the same command will be updated if their
3751 * parameters are valid and no other error occurs."
3754 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3756 for (int i
= 0; i
< count
; i
++) {
3757 struct gl_uniform_buffer_binding
*binding
=
3758 &ctx
->UniformBufferBindings
[first
+ i
];
3759 struct gl_buffer_object
*bufObj
;
3760 GLintptr offset
= 0;
3761 GLsizeiptr size
= 0;
3764 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3767 /* The ARB_multi_bind spec says:
3769 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3770 * pair of values in <offsets> and <sizes> does not respectively
3771 * satisfy the constraints described for those parameters for the
3772 * specified target, as described in section 6.7.1 (per binding)."
3774 * Section 6.7.1 refers to table 6.5, which says:
3776 * "┌───────────────────────────────────────────────────────────────┐
3777 * │ Uniform buffer array bindings (see sec. 7.6) │
3778 * ├─────────────────────┬─────────────────────────────────────────┤
3780 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3781 * │ │ OFFSET_ALIGNMENT │
3783 * │ size restriction │ none │
3784 * └─────────────────────┴─────────────────────────────────────────┘"
3786 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3787 _mesa_error(ctx
, GL_INVALID_VALUE
,
3788 "glBindBuffersRange(offsets[%u]=%" PRId64
3789 " is misaligned; it must be a multiple of the value of "
3790 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3791 "target=GL_UNIFORM_BUFFER)",
3792 i
, (int64_t) offsets
[i
],
3793 ctx
->Const
.UniformBufferOffsetAlignment
);
3797 offset
= offsets
[i
];
3801 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3802 bufObj
= binding
->BufferObject
;
3804 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3807 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3808 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3810 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3814 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3818 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3819 GLsizei count
, const GLuint
*buffers
,
3821 const GLintptr
*offsets
,
3822 const GLsizeiptr
*sizes
,
3825 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3828 /* Assume that at least one binding will be changed */
3829 FLUSH_VERTICES(ctx
, 0);
3830 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3833 /* The ARB_multi_bind spec says:
3835 * "If <buffers> is NULL, all bindings from <first> through
3836 * <first>+<count>-1 are reset to their unbound (zero) state.
3837 * In this case, the offsets and sizes associated with the
3838 * binding points are set to default values, ignoring
3839 * <offsets> and <sizes>."
3841 unbind_shader_storage_buffers(ctx
, first
, count
);
3845 /* Note that the error semantics for multi-bind commands differ from
3846 * those of other GL commands.
3848 * The Issues section in the ARB_multi_bind spec says:
3850 * "(11) Typically, OpenGL specifies that if an error is generated by a
3851 * command, that command has no effect. This is somewhat
3852 * unfortunate for multi-bind commands, because it would require a
3853 * first pass to scan the entire list of bound objects for errors
3854 * and then a second pass to actually perform the bindings.
3855 * Should we have different error semantics?
3857 * RESOLVED: Yes. In this specification, when the parameters for
3858 * one of the <count> binding points are invalid, that binding point
3859 * is not updated and an error will be generated. However, other
3860 * binding points in the same command will be updated if their
3861 * parameters are valid and no other error occurs."
3864 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3866 for (int i
= 0; i
< count
; i
++) {
3867 struct gl_shader_storage_buffer_binding
*binding
=
3868 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3869 struct gl_buffer_object
*bufObj
;
3870 GLintptr offset
= 0;
3871 GLsizeiptr size
= 0;
3874 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3877 /* The ARB_multi_bind spec says:
3879 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3880 * pair of values in <offsets> and <sizes> does not respectively
3881 * satisfy the constraints described for those parameters for the
3882 * specified target, as described in section 6.7.1 (per binding)."
3884 * Section 6.7.1 refers to table 6.5, which says:
3886 * "┌───────────────────────────────────────────────────────────────┐
3887 * │ Shader storage buffer array bindings (see sec. 7.8) │
3888 * ├─────────────────────┬─────────────────────────────────────────┤
3890 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3891 * │ │ BUFFER_OFFSET_ALIGNMENT │
3893 * │ size restriction │ none │
3894 * └─────────────────────┴─────────────────────────────────────────┘"
3896 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3897 _mesa_error(ctx
, GL_INVALID_VALUE
,
3898 "glBindBuffersRange(offsets[%u]=%" PRId64
3899 " is misaligned; it must be a multiple of the value of "
3900 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3901 "target=GL_SHADER_STORAGE_BUFFER)",
3902 i
, (int64_t) offsets
[i
],
3903 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3907 offset
= offsets
[i
];
3911 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3912 bufObj
= binding
->BufferObject
;
3914 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3917 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3918 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3920 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3924 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3928 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3929 struct gl_transform_feedback_object
*tfObj
,
3930 GLuint first
, GLsizei count
, const char *caller
)
3932 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3933 _mesa_error(ctx
, GL_INVALID_ENUM
,
3934 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3938 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3940 * "An INVALID_OPERATION error is generated :
3943 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3944 * FEEDBACK_BUFFER and transform feedback is currently active."
3946 * We assume that this is also meant to apply to BindBuffersRange
3947 * and BindBuffersBase.
3949 if (tfObj
->Active
) {
3950 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3951 "%s(Changing transform feedback buffers while "
3952 "transform feedback is active)", caller
);
3956 /* The ARB_multi_bind_spec says:
3958 * "An INVALID_OPERATION error is generated if <first> + <count> is
3959 * greater than the number of target-specific indexed binding points,
3960 * as described in section 6.7.1."
3962 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3963 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3964 "%s(first=%u + count=%d > the value of "
3965 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3966 caller
, first
, count
,
3967 ctx
->Const
.MaxTransformFeedbackBuffers
);
3975 * Unbind all transform feedback buffers in the range
3976 * <first> through <first>+<count>-1
3979 unbind_xfb_buffers(struct gl_context
*ctx
,
3980 struct gl_transform_feedback_object
*tfObj
,
3981 GLuint first
, GLsizei count
)
3983 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3985 for (int i
= 0; i
< count
; i
++)
3986 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3991 bind_xfb_buffers(struct gl_context
*ctx
,
3992 GLuint first
, GLsizei count
,
3993 const GLuint
*buffers
,
3995 const GLintptr
*offsets
,
3996 const GLsizeiptr
*sizes
,
3999 struct gl_transform_feedback_object
*tfObj
=
4000 ctx
->TransformFeedback
.CurrentObject
;
4002 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
4005 /* Assume that at least one binding will be changed */
4006 FLUSH_VERTICES(ctx
, 0);
4007 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
4010 /* The ARB_multi_bind spec says:
4012 * "If <buffers> is NULL, all bindings from <first> through
4013 * <first>+<count>-1 are reset to their unbound (zero) state.
4014 * In this case, the offsets and sizes associated with the
4015 * binding points are set to default values, ignoring
4016 * <offsets> and <sizes>."
4018 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
4022 /* Note that the error semantics for multi-bind commands differ from
4023 * those of other GL commands.
4025 * The Issues section in the ARB_multi_bind spec says:
4027 * "(11) Typically, OpenGL specifies that if an error is generated by a
4028 * command, that command has no effect. This is somewhat
4029 * unfortunate for multi-bind commands, because it would require a
4030 * first pass to scan the entire list of bound objects for errors
4031 * and then a second pass to actually perform the bindings.
4032 * Should we have different error semantics?
4034 * RESOLVED: Yes. In this specification, when the parameters for
4035 * one of the <count> binding points are invalid, that binding point
4036 * is not updated and an error will be generated. However, other
4037 * binding points in the same command will be updated if their
4038 * parameters are valid and no other error occurs."
4041 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4043 for (int i
= 0; i
< count
; i
++) {
4044 const GLuint index
= first
+ i
;
4045 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
4046 struct gl_buffer_object
*bufObj
;
4047 GLintptr offset
= 0;
4048 GLsizeiptr size
= 0;
4051 offset
= offsets
[i
];
4054 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4057 /* The ARB_multi_bind spec says:
4059 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4060 * pair of values in <offsets> and <sizes> does not respectively
4061 * satisfy the constraints described for those parameters for the
4062 * specified target, as described in section 6.7.1 (per binding)."
4064 * Section 6.7.1 refers to table 6.5, which says:
4066 * "┌───────────────────────────────────────────────────────────────┐
4067 * │ Transform feedback array bindings (see sec. 13.2.2) │
4068 * ├───────────────────────┬───────────────────────────────────────┤
4070 * │ offset restriction │ multiple of 4 │
4072 * │ size restriction │ multiple of 4 │
4073 * └───────────────────────┴───────────────────────────────────────┘"
4075 if (offsets
[i
] & 0x3) {
4076 _mesa_error(ctx
, GL_INVALID_VALUE
,
4077 "glBindBuffersRange(offsets[%u]=%" PRId64
4078 " is misaligned; it must be a multiple of 4 when "
4079 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4080 i
, (int64_t) offsets
[i
]);
4084 if (sizes
[i
] & 0x3) {
4085 _mesa_error(ctx
, GL_INVALID_VALUE
,
4086 "glBindBuffersRange(sizes[%u]=%" PRId64
4087 " is misaligned; it must be a multiple of 4 when "
4088 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4089 i
, (int64_t) sizes
[i
]);
4093 offset
= offsets
[i
];
4097 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
4098 bufObj
= boundBufObj
;
4100 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
4103 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
4107 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4111 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
4112 GLuint first
, GLsizei count
,
4115 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
4116 _mesa_error(ctx
, GL_INVALID_ENUM
,
4117 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
4121 /* The ARB_multi_bind_spec says:
4123 * "An INVALID_OPERATION error is generated if <first> + <count> is
4124 * greater than the number of target-specific indexed binding points,
4125 * as described in section 6.7.1."
4127 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
4128 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4129 "%s(first=%u + count=%d > the value of "
4130 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4131 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
4139 * Unbind all atomic counter buffers in the range
4140 * <first> through <first>+<count>-1
4143 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
4145 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
4147 for (int i
= 0; i
< count
; i
++)
4148 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
4153 bind_atomic_buffers(struct gl_context
*ctx
,
4156 const GLuint
*buffers
,
4158 const GLintptr
*offsets
,
4159 const GLsizeiptr
*sizes
,
4162 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
4165 /* Assume that at least one binding will be changed */
4166 FLUSH_VERTICES(ctx
, 0);
4167 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
4170 /* The ARB_multi_bind spec says:
4172 * "If <buffers> is NULL, all bindings from <first> through
4173 * <first>+<count>-1 are reset to their unbound (zero) state.
4174 * In this case, the offsets and sizes associated with the
4175 * binding points are set to default values, ignoring
4176 * <offsets> and <sizes>."
4178 unbind_atomic_buffers(ctx
, first
, count
);
4182 /* Note that the error semantics for multi-bind commands differ from
4183 * those of other GL commands.
4185 * The Issues section in the ARB_multi_bind spec says:
4187 * "(11) Typically, OpenGL specifies that if an error is generated by a
4188 * command, that command has no effect. This is somewhat
4189 * unfortunate for multi-bind commands, because it would require a
4190 * first pass to scan the entire list of bound objects for errors
4191 * and then a second pass to actually perform the bindings.
4192 * Should we have different error semantics?
4194 * RESOLVED: Yes. In this specification, when the parameters for
4195 * one of the <count> binding points are invalid, that binding point
4196 * is not updated and an error will be generated. However, other
4197 * binding points in the same command will be updated if their
4198 * parameters are valid and no other error occurs."
4201 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4203 for (int i
= 0; i
< count
; i
++) {
4204 struct gl_atomic_buffer_binding
*binding
=
4205 &ctx
->AtomicBufferBindings
[first
+ i
];
4206 struct gl_buffer_object
*bufObj
;
4207 GLintptr offset
= 0;
4208 GLsizeiptr size
= 0;
4211 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4214 /* The ARB_multi_bind spec says:
4216 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4217 * pair of values in <offsets> and <sizes> does not respectively
4218 * satisfy the constraints described for those parameters for the
4219 * specified target, as described in section 6.7.1 (per binding)."
4221 * Section 6.7.1 refers to table 6.5, which says:
4223 * "┌───────────────────────────────────────────────────────────────┐
4224 * │ Atomic counter array bindings (see sec. 7.7.2) │
4225 * ├───────────────────────┬───────────────────────────────────────┤
4227 * │ offset restriction │ multiple of 4 │
4229 * │ size restriction │ none │
4230 * └───────────────────────┴───────────────────────────────────────┘"
4232 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
4233 _mesa_error(ctx
, GL_INVALID_VALUE
,
4234 "glBindBuffersRange(offsets[%u]=%" PRId64
4235 " is misaligned; it must be a multiple of %d when "
4236 "target=GL_ATOMIC_COUNTER_BUFFER)",
4237 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
4241 offset
= offsets
[i
];
4245 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
4246 bufObj
= binding
->BufferObject
;
4248 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
4251 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
4254 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4257 static ALWAYS_INLINE
void
4258 bind_buffer_range(GLenum target
, GLuint index
, GLuint buffer
, GLintptr offset
,
4259 GLsizeiptr size
, bool no_error
)
4261 GET_CURRENT_CONTEXT(ctx
);
4262 struct gl_buffer_object
*bufObj
;
4264 if (MESA_VERBOSE
& VERBOSE_API
) {
4265 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4266 _mesa_enum_to_string(target
), index
, buffer
,
4267 (unsigned long) offset
, (unsigned long) size
);
4271 bufObj
= ctx
->Shared
->NullBufferObj
;
4273 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4274 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4275 &bufObj
, "glBindBufferRange"))
4281 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4282 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4283 index
, bufObj
, offset
, size
);
4285 case GL_UNIFORM_BUFFER
:
4286 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
4288 case GL_SHADER_STORAGE_BUFFER
:
4289 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
,
4292 case GL_ATOMIC_COUNTER_BUFFER
:
4293 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
4296 unreachable("invalid BindBufferRange target with KHR_no_error");
4300 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4301 "glBindBufferRange(invalid buffer=%u)", buffer
);
4307 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
4314 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4315 if (!_mesa_validate_buffer_range_xfb(ctx
,
4316 ctx
->TransformFeedback
.CurrentObject
,
4317 index
, bufObj
, offset
, size
,
4321 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4322 index
, bufObj
, offset
, size
);
4324 case GL_UNIFORM_BUFFER
:
4325 bind_buffer_range_uniform_buffer_err(ctx
, index
, bufObj
, offset
,
4328 case GL_SHADER_STORAGE_BUFFER
:
4329 bind_buffer_range_shader_storage_buffer_err(ctx
, index
, bufObj
,
4332 case GL_ATOMIC_COUNTER_BUFFER
:
4333 bind_atomic_buffer_err(ctx
, index
, bufObj
, offset
, size
,
4334 "glBindBufferRange");
4337 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
4344 _mesa_BindBufferRange_no_error(GLenum target
, GLuint index
, GLuint buffer
,
4345 GLintptr offset
, GLsizeiptr size
)
4347 bind_buffer_range(target
, index
, buffer
, offset
, size
, true);
4351 _mesa_BindBufferRange(GLenum target
, GLuint index
,
4352 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
4354 bind_buffer_range(target
, index
, buffer
, offset
, size
, false);
4358 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
4360 GET_CURRENT_CONTEXT(ctx
);
4361 struct gl_buffer_object
*bufObj
;
4363 if (MESA_VERBOSE
& VERBOSE_API
) {
4364 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
4365 _mesa_enum_to_string(target
), index
, buffer
);
4369 bufObj
= ctx
->Shared
->NullBufferObj
;
4371 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4372 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4373 &bufObj
, "glBindBufferBase"))
4378 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4379 "glBindBufferBase(invalid buffer=%u)", buffer
);
4383 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4384 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4386 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4387 * zero and size equal to the size of buffer."
4389 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4391 * "If the parameter (starting offset or size) was not specified when the
4392 * buffer object was bound, zero is returned."
4394 * What happens if the size of the buffer changes? Does the size of the
4395 * buffer at the moment glBindBufferBase was called still play a role, like
4396 * the first quote would imply, or is the size meaningless in the
4397 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4398 * core spec page 45 says:
4400 * "It is equivalent to calling BindBufferRange with offset zero, while
4401 * size is determined by the size of the bound buffer at the time the
4404 * My interpretation is that the GL 4.1 spec was a clarification of the
4405 * behavior, not a change. In particular, this choice will only make
4406 * rendering work in cases where it would have had undefined results.
4410 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4411 _mesa_bind_buffer_base_transform_feedback(ctx
,
4412 ctx
->TransformFeedback
.CurrentObject
,
4413 index
, bufObj
, false);
4415 case GL_UNIFORM_BUFFER
:
4416 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4418 case GL_SHADER_STORAGE_BUFFER
:
4419 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4421 case GL_ATOMIC_COUNTER_BUFFER
:
4422 bind_atomic_buffer_err(ctx
, index
, bufObj
, 0, 0,
4423 "glBindBufferBase");
4426 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4432 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4433 const GLuint
*buffers
,
4434 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4436 GET_CURRENT_CONTEXT(ctx
);
4438 if (MESA_VERBOSE
& VERBOSE_API
) {
4439 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4440 _mesa_enum_to_string(target
), first
, count
,
4441 buffers
, offsets
, sizes
);
4445 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4446 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4447 "glBindBuffersRange");
4449 case GL_UNIFORM_BUFFER
:
4450 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4451 "glBindBuffersRange");
4453 case GL_SHADER_STORAGE_BUFFER
:
4454 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4455 "glBindBuffersRange");
4457 case GL_ATOMIC_COUNTER_BUFFER
:
4458 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4459 "glBindBuffersRange");
4462 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4463 _mesa_enum_to_string(target
));
4469 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4470 const GLuint
*buffers
)
4472 GET_CURRENT_CONTEXT(ctx
);
4474 if (MESA_VERBOSE
& VERBOSE_API
) {
4475 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4476 _mesa_enum_to_string(target
), first
, count
, buffers
);
4480 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4481 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4482 "glBindBuffersBase");
4484 case GL_UNIFORM_BUFFER
:
4485 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4486 "glBindBuffersBase");
4488 case GL_SHADER_STORAGE_BUFFER
:
4489 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4490 "glBindBuffersBase");
4492 case GL_ATOMIC_COUNTER_BUFFER
:
4493 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4494 "glBindBuffersBase");
4497 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4498 _mesa_enum_to_string(target
));
4503 static ALWAYS_INLINE
void
4504 invalidate_buffer_subdata(struct gl_context
*ctx
,
4505 struct gl_buffer_object
*bufObj
, GLintptr offset
,
4508 if (ctx
->Driver
.InvalidateBufferSubData
)
4509 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4513 _mesa_InvalidateBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
4516 GET_CURRENT_CONTEXT(ctx
);
4518 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4519 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4523 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4526 GET_CURRENT_CONTEXT(ctx
);
4527 struct gl_buffer_object
*bufObj
;
4528 const GLintptr end
= offset
+ length
;
4530 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4531 * Profile) spec says:
4533 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4534 * name of an existing buffer object."
4536 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4537 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4538 _mesa_error(ctx
, GL_INVALID_VALUE
,
4539 "glInvalidateBufferSubData(name = %u) invalid object",
4544 /* The GL_ARB_invalidate_subdata spec says:
4546 * "An INVALID_VALUE error is generated if <offset> or <length> is
4547 * negative, or if <offset> + <length> is greater than the value of
4550 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
4551 _mesa_error(ctx
, GL_INVALID_VALUE
,
4552 "glInvalidateBufferSubData(invalid offset or length)");
4556 /* The OpenGL 4.4 (Core Profile) spec says:
4558 * "An INVALID_OPERATION error is generated if buffer is currently
4559 * mapped by MapBuffer or if the invalidate range intersects the range
4560 * currently mapped by MapBufferRange, unless it was mapped
4561 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4563 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4564 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4565 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4566 "glInvalidateBufferSubData(intersection with mapped "
4571 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4575 _mesa_InvalidateBufferData_no_error(GLuint buffer
)
4577 GET_CURRENT_CONTEXT(ctx
);
4579 struct gl_buffer_object
*bufObj
=_mesa_lookup_bufferobj(ctx
, buffer
);
4580 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4584 _mesa_InvalidateBufferData(GLuint buffer
)
4586 GET_CURRENT_CONTEXT(ctx
);
4587 struct gl_buffer_object
*bufObj
;
4589 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4590 * Profile) spec says:
4592 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4593 * name of an existing buffer object."
4595 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4596 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4597 _mesa_error(ctx
, GL_INVALID_VALUE
,
4598 "glInvalidateBufferData(name = %u) invalid object",
4603 /* The OpenGL 4.4 (Core Profile) spec says:
4605 * "An INVALID_OPERATION error is generated if buffer is currently
4606 * mapped by MapBuffer or if the invalidate range intersects the range
4607 * currently mapped by MapBufferRange, unless it was mapped
4608 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4610 if (_mesa_check_disallowed_mapping(bufObj
)) {
4611 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4612 "glInvalidateBufferData(intersection with mapped "
4617 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4621 buffer_page_commitment(struct gl_context
*ctx
,
4622 struct gl_buffer_object
*bufferObj
,
4623 GLintptr offset
, GLsizeiptr size
,
4624 GLboolean commit
, const char *func
)
4626 if (!(bufferObj
->StorageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)) {
4627 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(not a sparse buffer object)",
4632 if (size
< 0 || size
> bufferObj
->Size
||
4633 offset
< 0 || offset
> bufferObj
->Size
- size
) {
4634 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(out of bounds)",
4639 /* The GL_ARB_sparse_buffer extension specification says:
4641 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4642 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4643 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4644 * not extend to the end of the buffer's data store."
4646 if (offset
% ctx
->Const
.SparseBufferPageSize
!= 0) {
4647 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset not aligned to page size)",
4652 if (size
% ctx
->Const
.SparseBufferPageSize
!= 0 &&
4653 offset
+ size
!= bufferObj
->Size
) {
4654 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size not aligned to page size)",
4659 ctx
->Driver
.BufferPageCommitment(ctx
, bufferObj
, offset
, size
, commit
);
4663 _mesa_BufferPageCommitmentARB(GLenum target
, GLintptr offset
, GLsizeiptr size
,
4666 GET_CURRENT_CONTEXT(ctx
);
4667 struct gl_buffer_object
*bufferObj
;
4669 bufferObj
= get_buffer(ctx
, "glBufferPageCommitmentARB", target
,
4674 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4675 "glBufferPageCommitmentARB");
4679 _mesa_NamedBufferPageCommitmentARB(GLuint buffer
, GLintptr offset
,
4680 GLsizeiptr size
, GLboolean commit
)
4682 GET_CURRENT_CONTEXT(ctx
);
4683 struct gl_buffer_object
*bufferObj
;
4685 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4686 if (!bufferObj
|| bufferObj
== &DummyBufferObject
) {
4687 /* Note: the extension spec is not clear about the excpected error value. */
4688 _mesa_error(ctx
, GL_INVALID_VALUE
,
4689 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4694 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4695 "glNamedBufferPageCommitmentARB");