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"
44 #include "glformats.h"
46 #include "transformfeedback.h"
52 /*#define BOUNDS_CHECK*/
56 * We count the number of buffer modification calls to check for
57 * inefficient buffer use. This is the number of such calls before we
60 #define BUFFER_WARNING_CALL_COUNT 4
64 * Helper to warn of possible performance issues, such as frequently
65 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
69 buffer_usage_warning(struct gl_context
*ctx
, GLuint
*id
, const char *fmt
, ...)
74 _mesa_gl_vdebug(ctx
, id
,
75 MESA_DEBUG_SOURCE_API
,
76 MESA_DEBUG_TYPE_PERFORMANCE
,
77 MESA_DEBUG_SEVERITY_MEDIUM
,
82 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
84 static GLuint id = 0; \
85 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
90 * Used as a placeholder for buffer objects between glGenBuffers() and
91 * glBindBuffer() so that glIsBuffer() can work correctly.
93 static struct gl_buffer_object DummyBufferObject
;
97 * Return pointer to address of a buffer object target.
98 * \param ctx the GL context
99 * \param target the buffer object target to be retrieved.
100 * \return pointer to pointer to the buffer object bound to \c target in the
101 * specified context or \c NULL if \c target is invalid.
103 static inline struct gl_buffer_object
**
104 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
106 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
108 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
109 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
113 case GL_ARRAY_BUFFER_ARB
:
114 return &ctx
->Array
.ArrayBufferObj
;
115 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
116 return &ctx
->Array
.VAO
->IndexBufferObj
;
117 case GL_PIXEL_PACK_BUFFER_EXT
:
118 return &ctx
->Pack
.BufferObj
;
119 case GL_PIXEL_UNPACK_BUFFER_EXT
:
120 return &ctx
->Unpack
.BufferObj
;
121 case GL_COPY_READ_BUFFER
:
122 return &ctx
->CopyReadBuffer
;
123 case GL_COPY_WRITE_BUFFER
:
124 return &ctx
->CopyWriteBuffer
;
125 case GL_QUERY_BUFFER
:
126 if (_mesa_has_ARB_query_buffer_object(ctx
))
127 return &ctx
->QueryBuffer
;
129 case GL_DRAW_INDIRECT_BUFFER
:
130 if ((ctx
->API
== API_OPENGL_CORE
&&
131 ctx
->Extensions
.ARB_draw_indirect
) ||
132 _mesa_is_gles31(ctx
)) {
133 return &ctx
->DrawIndirectBuffer
;
136 case GL_PARAMETER_BUFFER_ARB
:
137 if (_mesa_has_ARB_indirect_parameters(ctx
)) {
138 return &ctx
->ParameterBuffer
;
141 case GL_DISPATCH_INDIRECT_BUFFER
:
142 if (_mesa_has_compute_shaders(ctx
)) {
143 return &ctx
->DispatchIndirectBuffer
;
146 case GL_TRANSFORM_FEEDBACK_BUFFER
:
147 if (ctx
->Extensions
.EXT_transform_feedback
) {
148 return &ctx
->TransformFeedback
.CurrentBuffer
;
151 case GL_TEXTURE_BUFFER
:
152 if (_mesa_has_ARB_texture_buffer_object(ctx
) ||
153 _mesa_has_OES_texture_buffer(ctx
)) {
154 return &ctx
->Texture
.BufferObject
;
157 case GL_UNIFORM_BUFFER
:
158 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
159 return &ctx
->UniformBuffer
;
162 case GL_SHADER_STORAGE_BUFFER
:
163 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
164 return &ctx
->ShaderStorageBuffer
;
167 case GL_ATOMIC_COUNTER_BUFFER
:
168 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
169 return &ctx
->AtomicBuffer
;
172 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
173 if (ctx
->Extensions
.AMD_pinned_memory
) {
174 return &ctx
->ExternalVirtualMemoryBuffer
;
185 * Get the buffer object bound to the specified target in a GL context.
186 * \param ctx the GL context
187 * \param target the buffer object target to be retrieved.
188 * \param error the GL error to record if target is illegal.
189 * \return pointer to the buffer object bound to \c target in the
190 * specified context or \c NULL if \c target is invalid.
192 static inline struct gl_buffer_object
*
193 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
196 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
199 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
203 if (!_mesa_is_bufferobj(*bufObj
)) {
204 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
213 * Convert a GLbitfield describing the mapped buffer access flags
214 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
217 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
219 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
220 if ((access
& rwFlags
) == rwFlags
)
221 return GL_READ_WRITE
;
222 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
224 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
225 return GL_WRITE_ONLY
;
227 /* Otherwise, AccessFlags is zero (the default state).
229 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
231 * Name Type Initial Value Legal Values
233 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
236 * However, table 6.8 in the GL_OES_mapbuffer extension says:
238 * Get Value Type Get Command Value Description
239 * --------- ---- ----------- ----- -----------
240 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
242 * The difference is because GL_OES_mapbuffer only supports mapping buffers
247 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
252 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
254 * The regions do not overlap if and only if the end of the given
255 * region is before the mapped region or the start of the given region
256 * is after the mapped region.
258 * \param obj Buffer object target on which to operate.
259 * \param offset Offset of the first byte of the subdata range.
260 * \param size Size, in bytes, of the subdata range.
261 * \return true if ranges overlap, false otherwise
265 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
266 GLintptr offset
, GLsizeiptr size
)
268 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
269 const GLintptr end
= offset
+ size
;
270 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
271 obj
->Mappings
[MAP_USER
].Length
;
273 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
282 * Tests the subdata range parameters and sets the GL error code for
283 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
284 * \c glClearBufferSubData.
286 * \param ctx GL context.
287 * \param bufObj The buffer object.
288 * \param offset Offset of the first byte of the subdata range.
289 * \param size Size, in bytes, of the subdata range.
290 * \param mappedRange If true, checks if an overlapping range is mapped.
291 * If false, checks if buffer is mapped.
292 * \param caller Name of calling function for recording errors.
293 * \return false if error, true otherwise
295 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
298 buffer_object_subdata_range_good(struct gl_context
*ctx
,
299 const struct gl_buffer_object
*bufObj
,
300 GLintptr offset
, GLsizeiptr size
,
301 bool mappedRange
, const char *caller
)
304 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
309 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
313 if (offset
+ size
> bufObj
->Size
) {
314 _mesa_error(ctx
, GL_INVALID_VALUE
,
315 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
316 (unsigned long) offset
,
317 (unsigned long) size
,
318 (unsigned long) bufObj
->Size
);
322 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
326 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
327 _mesa_error(ctx
, GL_INVALID_OPERATION
,
328 "%s(range is mapped without persistent bit)",
334 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
335 _mesa_error(ctx
, GL_INVALID_OPERATION
,
336 "%s(buffer is mapped without persistent bit)",
347 * Test the format and type parameters and set the GL error code for
348 * \c glClearBufferData and \c glClearBufferSubData.
350 * \param ctx GL context.
351 * \param internalformat Format to which the data is to be converted.
352 * \param format Format of the supplied data.
353 * \param type Type of the supplied data.
354 * \param caller Name of calling function for recording errors.
355 * \return If internalformat, format and type are legal the mesa_format
356 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
358 * \sa glClearBufferData and glClearBufferSubData
361 validate_clear_buffer_format(struct gl_context
*ctx
,
362 GLenum internalformat
,
363 GLenum format
, GLenum type
,
366 mesa_format mesaFormat
;
367 GLenum errorFormatType
;
369 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
370 if (mesaFormat
== MESA_FORMAT_NONE
) {
371 _mesa_error(ctx
, GL_INVALID_ENUM
,
372 "%s(invalid internalformat)", caller
);
373 return MESA_FORMAT_NONE
;
376 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
377 * EXT_texture_integer there is no conversion between integer and
378 * non-integer formats
380 if (_mesa_is_enum_format_signed_int(format
) !=
381 _mesa_is_format_integer_color(mesaFormat
)) {
382 _mesa_error(ctx
, GL_INVALID_OPERATION
,
383 "%s(integer vs non-integer)", caller
);
384 return MESA_FORMAT_NONE
;
387 if (!_mesa_is_color_format(format
)) {
388 _mesa_error(ctx
, GL_INVALID_ENUM
,
389 "%s(format is not a color format)", caller
);
390 return MESA_FORMAT_NONE
;
393 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
394 if (errorFormatType
!= GL_NO_ERROR
) {
395 _mesa_error(ctx
, GL_INVALID_ENUM
,
396 "%s(invalid format or type)", caller
);
397 return MESA_FORMAT_NONE
;
405 * Convert user-specified clear value to the specified internal format.
407 * \param ctx GL context.
408 * \param internalformat Format to which the data is converted.
409 * \param clearValue Points to the converted clear value.
410 * \param format Format of the supplied data.
411 * \param type Type of the supplied data.
412 * \param data Data which is to be converted to internalformat.
413 * \param caller Name of calling function for recording errors.
414 * \return true if data could be converted, false otherwise.
416 * \sa glClearBufferData, glClearBufferSubData
419 convert_clear_buffer_data(struct gl_context
*ctx
,
420 mesa_format internalformat
,
421 GLubyte
*clearValue
, GLenum format
, GLenum type
,
422 const GLvoid
*data
, const char *caller
)
424 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
426 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
427 0, &clearValue
, 1, 1, 1,
428 format
, type
, data
, &ctx
->Unpack
)) {
432 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
439 * Allocate and initialize a new buffer object.
441 * Default callback for the \c dd_function_table::NewBufferObject() hook.
443 static struct gl_buffer_object
*
444 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
446 struct gl_buffer_object
*obj
;
450 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 assert(ctx
->Driver
.NewBufferObject
);
993 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
995 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
998 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
1006 * Bind the specified target to buffer for the specified context.
1007 * Called by glBindBuffer() and other functions.
1010 bind_buffer_object(struct gl_context
*ctx
,
1011 struct gl_buffer_object
**bindTarget
, GLuint buffer
)
1013 struct gl_buffer_object
*oldBufObj
;
1014 struct gl_buffer_object
*newBufObj
= NULL
;
1018 /* Get pointer to old buffer object (to be unbound) */
1019 oldBufObj
= *bindTarget
;
1020 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
1021 return; /* rebinding the same buffer object- no change */
1024 * Get pointer to new buffer object (newBufObj)
1027 /* The spec says there's not a buffer object named 0, but we use
1028 * one internally because it simplifies things.
1030 newBufObj
= ctx
->Shared
->NullBufferObj
;
1033 /* non-default buffer object */
1034 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1035 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1036 &newBufObj
, "glBindBuffer"))
1040 /* record usage history */
1041 if (bindTarget
== &ctx
->Pack
.BufferObj
) {
1042 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1045 /* bind new buffer */
1046 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1051 * Update the default buffer objects in the given context to reference those
1052 * specified in the shared state and release those referencing the old
1056 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1058 /* Bind the NullBufferObj to remove references to those
1059 * in the shared context hash table.
1061 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1062 bind_buffer_object(ctx
, &ctx
->Array
.VAO
->IndexBufferObj
, 0);
1063 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1064 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1070 * Return the gl_buffer_object for the given ID.
1071 * Always return NULL for ID 0.
1073 struct gl_buffer_object
*
1074 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1079 return (struct gl_buffer_object
*)
1080 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1084 struct gl_buffer_object
*
1085 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1090 return (struct gl_buffer_object
*)
1091 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1095 * A convenience function for direct state access functions that throws
1096 * GL_INVALID_OPERATION if buffer is not the name of an existing
1099 struct gl_buffer_object
*
1100 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1103 struct gl_buffer_object
*bufObj
;
1105 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1106 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1107 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1108 "%s(non-existent buffer object %u)", caller
, buffer
);
1117 * Look up a buffer object for a multi-bind function.
1119 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1120 * of generating an error if the buffer ID is not zero or the name
1121 * of an existing buffer object.
1123 * If the buffer ID refers to an existing buffer object, a pointer
1124 * to the buffer object is returned. If the ID is zero, a pointer
1125 * to the shared NullBufferObj is returned. If the ID is not zero
1126 * and does not refer to a valid buffer object, this function
1129 * This function assumes that the caller has already locked the
1130 * hash table mutex by calling
1131 * _mesa_HashLockMutex(ctx->Shared->BufferObjects).
1133 struct gl_buffer_object
*
1134 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1135 const GLuint
*buffers
,
1136 GLuint index
, const char *caller
)
1138 struct gl_buffer_object
*bufObj
;
1140 if (buffers
[index
] != 0) {
1141 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1143 /* The multi-bind functions don't create the buffer objects
1144 when they don't exist. */
1145 if (bufObj
== &DummyBufferObject
)
1148 bufObj
= ctx
->Shared
->NullBufferObj
;
1151 /* The ARB_multi_bind spec says:
1153 * "An INVALID_OPERATION error is generated if any value
1154 * in <buffers> is not zero or the name of an existing
1155 * buffer object (per binding)."
1157 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1158 "%s(buffers[%u]=%u is not zero or the name "
1159 "of an existing buffer object)",
1160 caller
, index
, buffers
[index
]);
1168 * If *ptr points to obj, set ptr = the Null/default buffer object.
1169 * This is a helper for buffer object deletion.
1170 * The GL spec says that deleting a buffer object causes it to get
1171 * unbound from all arrays in the current context.
1174 unbind(struct gl_context
*ctx
,
1175 struct gl_vertex_array_object
*vao
, unsigned index
,
1176 struct gl_buffer_object
*obj
)
1178 if (vao
->BufferBinding
[index
].BufferObj
== obj
) {
1179 _mesa_bind_vertex_buffer(ctx
, vao
, index
, ctx
->Shared
->NullBufferObj
,
1180 vao
->BufferBinding
[index
].Offset
,
1181 vao
->BufferBinding
[index
].Stride
);
1187 * Plug default/fallback buffer object functions into the device
1191 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1193 /* GL_ARB_vertex/pixel_buffer_object */
1194 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1195 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1196 driver
->BufferData
= buffer_data_fallback
;
1197 driver
->BufferSubData
= buffer_sub_data_fallback
;
1198 driver
->GetBufferSubData
= buffer_get_subdata
;
1199 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1201 /* GL_ARB_clear_buffer_object */
1202 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1204 /* GL_ARB_map_buffer_range */
1205 driver
->MapBufferRange
= map_buffer_range_fallback
;
1206 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1208 /* GL_ARB_copy_buffer */
1209 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1214 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1215 struct gl_buffer_object
*bufObj
)
1217 for (int i
= 0; i
< MAP_COUNT
; i
++) {
1218 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1219 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1220 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1221 bufObj
->Mappings
[i
].AccessFlags
= 0;
1227 /**********************************************************************/
1229 /**********************************************************************/
1232 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1234 GET_CURRENT_CONTEXT(ctx
);
1236 if (MESA_VERBOSE
& VERBOSE_API
) {
1237 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1238 _mesa_enum_to_string(target
), buffer
);
1241 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1243 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target %s)",
1244 _mesa_enum_to_string(target
));
1248 bind_buffer_object(ctx
, bindTarget
, buffer
);
1253 * Delete a set of buffer objects.
1255 * \param n Number of buffer objects to delete.
1256 * \param ids Array of \c n buffer object IDs.
1259 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1261 GET_CURRENT_CONTEXT(ctx
);
1263 FLUSH_VERTICES(ctx
, 0);
1266 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1270 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1272 for (i
= 0; i
< n
; i
++) {
1273 struct gl_buffer_object
*bufObj
=
1274 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1276 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1279 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1281 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1283 /* unbind any vertex pointers bound to this buffer */
1284 for (j
= 0; j
< ARRAY_SIZE(vao
->BufferBinding
); j
++) {
1285 unbind(ctx
, vao
, j
, bufObj
);
1288 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1289 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1291 if (vao
->IndexBufferObj
== bufObj
) {
1292 bind_buffer_object(ctx
, &vao
->IndexBufferObj
, 0);
1295 /* unbind ARB_draw_indirect binding point */
1296 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1297 bind_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, 0);
1300 /* unbind ARB_indirect_parameters binding point */
1301 if (ctx
->ParameterBuffer
== bufObj
) {
1302 bind_buffer_object(ctx
, &ctx
->ParameterBuffer
, 0);
1305 /* unbind ARB_compute_shader binding point */
1306 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1307 bind_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, 0);
1310 /* unbind ARB_copy_buffer binding points */
1311 if (ctx
->CopyReadBuffer
== bufObj
) {
1312 bind_buffer_object(ctx
, &ctx
->CopyReadBuffer
, 0);
1314 if (ctx
->CopyWriteBuffer
== bufObj
) {
1315 bind_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, 0);
1318 /* unbind transform feedback binding points */
1319 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1320 bind_buffer_object(ctx
, &ctx
->TransformFeedback
.CurrentBuffer
, 0);
1322 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1323 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1324 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1328 /* unbind UBO binding points */
1329 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1330 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1331 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1335 if (ctx
->UniformBuffer
== bufObj
) {
1336 bind_buffer_object(ctx
, &ctx
->UniformBuffer
, 0);
1339 /* unbind SSBO binding points */
1340 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1341 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1342 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1346 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1347 bind_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, 0);
1350 /* unbind Atomci Buffer binding points */
1351 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1352 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1353 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1357 if (ctx
->AtomicBuffer
== bufObj
) {
1358 bind_buffer_object(ctx
, &ctx
->AtomicBuffer
, 0);
1361 /* unbind any pixel pack/unpack pointers bound to this buffer */
1362 if (ctx
->Pack
.BufferObj
== bufObj
) {
1363 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1365 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1366 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1369 if (ctx
->Texture
.BufferObject
== bufObj
) {
1370 bind_buffer_object(ctx
, &ctx
->Texture
.BufferObject
, 0);
1373 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1374 bind_buffer_object(ctx
, &ctx
->ExternalVirtualMemoryBuffer
, 0);
1377 /* unbind query buffer binding point */
1378 if (ctx
->QueryBuffer
== bufObj
) {
1379 bind_buffer_object(ctx
, &ctx
->QueryBuffer
, 0);
1382 /* The ID is immediately freed for re-use */
1383 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1384 /* Make sure we do not run into the classic ABA problem on bind.
1385 * We don't want to allow re-binding a buffer object that's been
1386 * "deleted" by glDeleteBuffers().
1388 * The explicit rebinding to the default object in the current context
1389 * prevents the above in the current context, but another context
1390 * sharing the same objects might suffer from this problem.
1391 * The alternative would be to do the hash lookup in any case on bind
1392 * which would introduce more runtime overhead than this.
1394 bufObj
->DeletePending
= GL_TRUE
;
1395 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1399 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1404 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1405 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1409 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1411 GET_CURRENT_CONTEXT(ctx
);
1413 struct gl_buffer_object
*buf
;
1415 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1417 if (MESA_VERBOSE
& VERBOSE_API
)
1418 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1421 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1430 * This must be atomic (generation and allocation of buffer object IDs)
1432 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1434 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1436 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1437 * DummyBufferObject. Otherwise, create a new buffer object and insert
1440 for (int i
= 0; i
< n
; i
++) {
1441 buffers
[i
] = first
+ i
;
1443 assert(ctx
->Driver
.NewBufferObject
);
1444 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1446 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1447 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1452 buf
= &DummyBufferObject
;
1454 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1457 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1461 * Generate a set of unique buffer object IDs and store them in \c buffers.
1463 * \param n Number of IDs to generate.
1464 * \param buffers Array of \c n locations to store the IDs.
1467 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1469 create_buffers(n
, buffers
, false);
1473 * Create a set of buffer objects and store their unique IDs in \c buffers.
1475 * \param n Number of IDs to generate.
1476 * \param buffers Array of \c n locations to store the IDs.
1479 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1481 create_buffers(n
, buffers
, true);
1486 * Determine if ID is the name of a buffer object.
1488 * \param id ID of the potential buffer object.
1489 * \return \c GL_TRUE if \c id is the name of a buffer object,
1490 * \c GL_FALSE otherwise.
1492 GLboolean GLAPIENTRY
1493 _mesa_IsBuffer(GLuint id
)
1495 struct gl_buffer_object
*bufObj
;
1496 GET_CURRENT_CONTEXT(ctx
);
1497 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1499 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1501 return bufObj
&& bufObj
!= &DummyBufferObject
;
1506 validate_buffer_storage(struct gl_context
*ctx
,
1507 struct gl_buffer_object
*bufObj
, GLsizeiptr size
,
1508 GLbitfield flags
, const char *func
)
1511 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1515 GLbitfield valid_flags
= GL_MAP_READ_BIT
|
1517 GL_MAP_PERSISTENT_BIT
|
1518 GL_MAP_COHERENT_BIT
|
1519 GL_DYNAMIC_STORAGE_BIT
|
1520 GL_CLIENT_STORAGE_BIT
;
1522 if (ctx
->Extensions
.ARB_sparse_buffer
)
1523 valid_flags
|= GL_SPARSE_STORAGE_BIT_ARB
;
1525 if (flags
& ~valid_flags
) {
1526 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1530 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1532 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1533 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1534 * MAP_READ_BIT or MAP_WRITE_BIT."
1536 if (flags
& GL_SPARSE_STORAGE_BIT_ARB
&&
1537 flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) {
1538 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(SPARSE_STORAGE and READ/WRITE)", func
);
1542 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1543 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1544 _mesa_error(ctx
, GL_INVALID_VALUE
,
1545 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1549 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1550 _mesa_error(ctx
, GL_INVALID_VALUE
,
1551 "%s(COHERENT and flags!=PERSISTENT)", func
);
1555 if (bufObj
->Immutable
) {
1556 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1565 buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1566 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1567 GLbitfield flags
, const char *func
)
1569 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1570 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1572 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1574 bufObj
->Written
= GL_TRUE
;
1575 bufObj
->Immutable
= GL_TRUE
;
1576 bufObj
->MinMaxCacheDirty
= true;
1578 assert(ctx
->Driver
.BufferData
);
1579 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1581 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1582 /* Even though the interaction between AMD_pinned_memory and
1583 * glBufferStorage is not described in the spec, Graham Sellers
1584 * said that it should behave the same as glBufferData.
1586 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1589 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1595 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1598 GET_CURRENT_CONTEXT(ctx
);
1599 struct gl_buffer_object
*bufObj
;
1600 const char *func
= "glBufferStorage";
1602 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
1606 if (validate_buffer_storage(ctx
, bufObj
, size
, flags
, func
))
1607 buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
, func
);
1611 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1614 GET_CURRENT_CONTEXT(ctx
);
1615 struct gl_buffer_object
*bufObj
;
1616 const char *func
= "glNamedBufferStorage";
1618 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
1623 * In direct state access, buffer objects have an unspecified target since
1624 * they are not required to be bound.
1626 if (validate_buffer_storage(ctx
, bufObj
, size
, flags
, func
))
1627 buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
, func
);
1632 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1633 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1634 GLenum usage
, const char *func
)
1638 if (MESA_VERBOSE
& VERBOSE_API
) {
1639 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1641 _mesa_enum_to_string(target
),
1642 (long int) size
, data
,
1643 _mesa_enum_to_string(usage
));
1647 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1652 case GL_STREAM_DRAW_ARB
:
1653 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1656 case GL_STATIC_DRAW_ARB
:
1657 case GL_DYNAMIC_DRAW_ARB
:
1661 case GL_STREAM_READ_ARB
:
1662 case GL_STREAM_COPY_ARB
:
1663 case GL_STATIC_READ_ARB
:
1664 case GL_STATIC_COPY_ARB
:
1665 case GL_DYNAMIC_READ_ARB
:
1666 case GL_DYNAMIC_COPY_ARB
:
1667 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1671 valid_usage
= false;
1676 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1677 _mesa_enum_to_string(usage
));
1681 if (bufObj
->Immutable
) {
1682 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1686 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1687 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1689 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1691 bufObj
->Written
= GL_TRUE
;
1692 bufObj
->MinMaxCacheDirty
= true;
1695 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1696 bufObj
->Name
, size
, data
, usage
);
1703 assert(ctx
->Driver
.BufferData
);
1704 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1707 GL_DYNAMIC_STORAGE_BIT
,
1709 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1710 /* From GL_AMD_pinned_memory:
1712 * INVALID_OPERATION is generated by BufferData if <target> is
1713 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1714 * mapped to the GPU address space.
1716 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1719 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1725 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1726 const GLvoid
*data
, GLenum usage
)
1728 GET_CURRENT_CONTEXT(ctx
);
1729 struct gl_buffer_object
*bufObj
;
1731 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1735 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1740 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1743 GET_CURRENT_CONTEXT(ctx
);
1744 struct gl_buffer_object
*bufObj
;
1746 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1750 /* In direct state access, buffer objects have an unspecified target since
1751 * they are not required to be bound.
1753 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1754 "glNamedBufferData");
1759 * Implementation for glBufferSubData and glNamedBufferSubData.
1761 * \param ctx GL context.
1762 * \param bufObj The buffer object.
1763 * \param offset Offset of the first byte of the subdata range.
1764 * \param size Size, in bytes, of the subdata range.
1765 * \param data The data store.
1766 * \param func Name of calling function for recording errors.
1770 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1771 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1774 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1776 /* error already recorded */
1780 if (bufObj
->Immutable
&&
1781 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1782 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1789 bufObj
->NumSubDataCalls
++;
1791 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
1792 bufObj
->Usage
== GL_STATIC_COPY
) &&
1793 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
) {
1794 /* If the application declared the buffer as static draw/copy or stream
1795 * draw, it should not be frequently modified with glBufferSubData.
1797 BUFFER_USAGE_WARNING(ctx
,
1798 "using %s(buffer %u, offset %u, size %u) to "
1799 "update a %s buffer",
1800 func
, bufObj
->Name
, offset
, size
,
1801 _mesa_enum_to_string(bufObj
->Usage
));
1804 bufObj
->Written
= GL_TRUE
;
1805 bufObj
->MinMaxCacheDirty
= true;
1807 assert(ctx
->Driver
.BufferSubData
);
1808 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1812 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1813 GLsizeiptr size
, const GLvoid
*data
)
1815 GET_CURRENT_CONTEXT(ctx
);
1816 struct gl_buffer_object
*bufObj
;
1818 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1822 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1826 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1827 GLsizeiptr size
, const GLvoid
*data
)
1829 GET_CURRENT_CONTEXT(ctx
);
1830 struct gl_buffer_object
*bufObj
;
1832 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1836 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1837 "glNamedBufferSubData");
1842 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1843 GLsizeiptr size
, GLvoid
*data
)
1845 GET_CURRENT_CONTEXT(ctx
);
1846 struct gl_buffer_object
*bufObj
;
1848 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1849 GL_INVALID_OPERATION
);
1853 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1854 "glGetBufferSubData")) {
1858 assert(ctx
->Driver
.GetBufferSubData
);
1859 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1863 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1864 GLsizeiptr size
, GLvoid
*data
)
1866 GET_CURRENT_CONTEXT(ctx
);
1867 struct gl_buffer_object
*bufObj
;
1869 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1870 "glGetNamedBufferSubData");
1874 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1875 "glGetNamedBufferSubData")) {
1879 assert(ctx
->Driver
.GetBufferSubData
);
1880 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1885 * \param subdata true if caller is *SubData, false if *Data
1888 clear_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1889 GLenum internalformat
, GLintptr offset
, GLsizeiptr size
,
1890 GLenum format
, GLenum type
, const GLvoid
*data
,
1891 const char *func
, bool subdata
)
1893 mesa_format mesaFormat
;
1894 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1895 GLsizeiptr clearValueSize
;
1897 /* This checks for disallowed mappings. */
1898 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1903 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1904 format
, type
, func
);
1906 if (mesaFormat
== MESA_FORMAT_NONE
) {
1910 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1911 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1912 _mesa_error(ctx
, GL_INVALID_VALUE
,
1913 "%s(offset or size is not a multiple of "
1914 "internalformat size)", func
);
1918 /* Bail early. Negative size has already been checked. */
1922 bufObj
->MinMaxCacheDirty
= true;
1925 /* clear to zeros, per the spec */
1926 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1927 NULL
, clearValueSize
, bufObj
);
1931 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1932 format
, type
, data
, func
)) {
1936 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1937 clearValue
, clearValueSize
, bufObj
);
1941 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1942 GLenum type
, const GLvoid
*data
)
1944 GET_CURRENT_CONTEXT(ctx
);
1945 struct gl_buffer_object
*bufObj
;
1947 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1951 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1952 format
, type
, data
, "glClearBufferData", false);
1956 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1957 GLenum format
, GLenum type
, const GLvoid
*data
)
1959 GET_CURRENT_CONTEXT(ctx
);
1960 struct gl_buffer_object
*bufObj
;
1962 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1966 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1967 format
, type
, data
, "glClearNamedBufferData", false);
1972 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1973 GLintptr offset
, GLsizeiptr size
,
1974 GLenum format
, GLenum type
,
1977 GET_CURRENT_CONTEXT(ctx
);
1978 struct gl_buffer_object
*bufObj
;
1980 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1984 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1985 format
, type
, data
, "glClearBufferSubData", true);
1989 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1990 GLintptr offset
, GLsizeiptr size
,
1991 GLenum format
, GLenum type
,
1994 GET_CURRENT_CONTEXT(ctx
);
1995 struct gl_buffer_object
*bufObj
;
1997 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1998 "glClearNamedBufferSubData");
2002 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2003 type
, data
, "glClearNamedBufferSubData", true);
2007 unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
2009 GLboolean status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2010 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2011 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2012 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2013 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2019 validate_and_unmap_buffer(struct gl_context
*ctx
,
2020 struct gl_buffer_object
*bufObj
,
2023 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2025 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2026 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2027 "%s(buffer is not mapped)", func
);
2032 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2033 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2035 /* check that last 100 bytes are still = magic value */
2036 for (i
= 0; i
< 100; i
++) {
2037 GLuint pos
= bufObj
->Size
- i
- 1;
2038 if (buf
[pos
] != 123) {
2039 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2040 " at position %d (value = %u)\n",
2048 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2049 GLuint i
, unchanged
= 0;
2050 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2052 /* check which bytes changed */
2053 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2054 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2061 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2062 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2067 return unmap_buffer(ctx
, bufObj
);
2070 GLboolean GLAPIENTRY
2071 _mesa_UnmapBuffer_no_error(GLenum target
)
2073 GET_CURRENT_CONTEXT(ctx
);
2074 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2075 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2077 return unmap_buffer(ctx
, bufObj
);
2080 GLboolean GLAPIENTRY
2081 _mesa_UnmapBuffer(GLenum target
)
2083 GET_CURRENT_CONTEXT(ctx
);
2084 struct gl_buffer_object
*bufObj
;
2086 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2090 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2093 GLboolean GLAPIENTRY
2094 _mesa_UnmapNamedBuffer_no_error(GLuint buffer
)
2096 GET_CURRENT_CONTEXT(ctx
);
2097 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2099 return unmap_buffer(ctx
, bufObj
);
2102 GLboolean GLAPIENTRY
2103 _mesa_UnmapNamedBuffer(GLuint buffer
)
2105 GET_CURRENT_CONTEXT(ctx
);
2106 struct gl_buffer_object
*bufObj
;
2108 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2112 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2117 get_buffer_parameter(struct gl_context
*ctx
,
2118 struct gl_buffer_object
*bufObj
, GLenum pname
,
2119 GLint64
*params
, const char *func
)
2122 case GL_BUFFER_SIZE_ARB
:
2123 *params
= bufObj
->Size
;
2125 case GL_BUFFER_USAGE_ARB
:
2126 *params
= bufObj
->Usage
;
2128 case GL_BUFFER_ACCESS_ARB
:
2129 *params
= simplified_access_mode(ctx
,
2130 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2132 case GL_BUFFER_MAPPED_ARB
:
2133 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2135 case GL_BUFFER_ACCESS_FLAGS
:
2136 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2138 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2140 case GL_BUFFER_MAP_OFFSET
:
2141 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2143 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2145 case GL_BUFFER_MAP_LENGTH
:
2146 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2148 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2150 case GL_BUFFER_IMMUTABLE_STORAGE
:
2151 if (!ctx
->Extensions
.ARB_buffer_storage
)
2153 *params
= bufObj
->Immutable
;
2155 case GL_BUFFER_STORAGE_FLAGS
:
2156 if (!ctx
->Extensions
.ARB_buffer_storage
)
2158 *params
= bufObj
->StorageFlags
;
2167 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2168 _mesa_enum_to_string(pname
));
2173 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2175 GET_CURRENT_CONTEXT(ctx
);
2176 struct gl_buffer_object
*bufObj
;
2179 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2180 GL_INVALID_OPERATION
);
2184 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2185 "glGetBufferParameteriv"))
2186 return; /* Error already recorded. */
2188 *params
= (GLint
) parameter
;
2192 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2194 GET_CURRENT_CONTEXT(ctx
);
2195 struct gl_buffer_object
*bufObj
;
2198 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2199 GL_INVALID_OPERATION
);
2203 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2204 "glGetBufferParameteri64v"))
2205 return; /* Error already recorded. */
2207 *params
= parameter
;
2211 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2213 GET_CURRENT_CONTEXT(ctx
);
2214 struct gl_buffer_object
*bufObj
;
2217 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2218 "glGetNamedBufferParameteriv");
2222 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2223 "glGetNamedBufferParameteriv"))
2224 return; /* Error already recorded. */
2226 *params
= (GLint
) parameter
;
2230 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2233 GET_CURRENT_CONTEXT(ctx
);
2234 struct gl_buffer_object
*bufObj
;
2237 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2238 "glGetNamedBufferParameteri64v");
2242 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2243 "glGetNamedBufferParameteri64v"))
2244 return; /* Error already recorded. */
2246 *params
= parameter
;
2251 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2253 GET_CURRENT_CONTEXT(ctx
);
2254 struct gl_buffer_object
*bufObj
;
2256 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2257 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2258 "GL_BUFFER_MAP_POINTER)");
2262 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2263 GL_INVALID_OPERATION
);
2267 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2271 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2273 GET_CURRENT_CONTEXT(ctx
);
2274 struct gl_buffer_object
*bufObj
;
2276 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2277 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2278 "GL_BUFFER_MAP_POINTER)");
2282 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2283 "glGetNamedBufferPointerv");
2287 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2292 copy_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*src
,
2293 struct gl_buffer_object
*dst
, GLintptr readOffset
,
2294 GLintptr writeOffset
, GLsizeiptr size
, const char *func
)
2296 if (_mesa_check_disallowed_mapping(src
)) {
2297 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2298 "%s(readBuffer is mapped)", func
);
2302 if (_mesa_check_disallowed_mapping(dst
)) {
2303 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2304 "%s(writeBuffer is mapped)", func
);
2308 if (readOffset
< 0) {
2309 _mesa_error(ctx
, GL_INVALID_VALUE
,
2310 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2314 if (writeOffset
< 0) {
2315 _mesa_error(ctx
, GL_INVALID_VALUE
,
2316 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2321 _mesa_error(ctx
, GL_INVALID_VALUE
,
2322 "%s(size %d < 0)", func
, (int) size
);
2326 if (readOffset
+ size
> src
->Size
) {
2327 _mesa_error(ctx
, GL_INVALID_VALUE
,
2328 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2329 (int) readOffset
, (int) size
, (int) src
->Size
);
2333 if (writeOffset
+ size
> dst
->Size
) {
2334 _mesa_error(ctx
, GL_INVALID_VALUE
,
2335 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2336 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2341 if (readOffset
+ size
<= writeOffset
) {
2344 else if (writeOffset
+ size
<= readOffset
) {
2348 /* overlapping src/dst is illegal */
2349 _mesa_error(ctx
, GL_INVALID_VALUE
,
2350 "%s(overlapping src/dst)", func
);
2355 dst
->MinMaxCacheDirty
= true;
2357 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2361 _mesa_CopyBufferSubData_no_error(GLenum readTarget
, GLenum writeTarget
,
2362 GLintptr readOffset
, GLintptr writeOffset
,
2365 GET_CURRENT_CONTEXT(ctx
);
2367 struct gl_buffer_object
**src_ptr
= get_buffer_target(ctx
, readTarget
);
2368 struct gl_buffer_object
*src
= *src_ptr
;
2370 struct gl_buffer_object
**dst_ptr
= get_buffer_target(ctx
, writeTarget
);
2371 struct gl_buffer_object
*dst
= *dst_ptr
;
2373 dst
->MinMaxCacheDirty
= true;
2374 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2379 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2380 GLintptr readOffset
, GLintptr writeOffset
,
2383 GET_CURRENT_CONTEXT(ctx
);
2384 struct gl_buffer_object
*src
, *dst
;
2386 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2387 GL_INVALID_OPERATION
);
2391 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2392 GL_INVALID_OPERATION
);
2396 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2397 "glCopyBufferSubData");
2401 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer
, GLuint writeBuffer
,
2402 GLintptr readOffset
,
2403 GLintptr writeOffset
, GLsizeiptr size
)
2405 GET_CURRENT_CONTEXT(ctx
);
2407 struct gl_buffer_object
*src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
2408 struct gl_buffer_object
*dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
2410 dst
->MinMaxCacheDirty
= true;
2411 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2416 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2417 GLintptr readOffset
, GLintptr writeOffset
,
2420 GET_CURRENT_CONTEXT(ctx
);
2421 struct gl_buffer_object
*src
, *dst
;
2423 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2424 "glCopyNamedBufferSubData");
2428 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2429 "glCopyNamedBufferSubData");
2433 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2434 "glCopyNamedBufferSubData");
2438 validate_map_buffer_range(struct gl_context
*ctx
,
2439 struct gl_buffer_object
*bufObj
, GLintptr offset
,
2440 GLsizeiptr length
, GLbitfield access
,
2443 GLbitfield allowed_access
;
2445 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, false);
2448 _mesa_error(ctx
, GL_INVALID_VALUE
,
2449 "%s(offset %ld < 0)", func
, (long) offset
);
2454 _mesa_error(ctx
, GL_INVALID_VALUE
,
2455 "%s(length %ld < 0)", func
, (long) length
);
2459 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2461 * "An INVALID_OPERATION error is generated for any of the following
2464 * * <length> is zero."
2466 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2467 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2471 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2475 allowed_access
= GL_MAP_READ_BIT
|
2477 GL_MAP_INVALIDATE_RANGE_BIT
|
2478 GL_MAP_INVALIDATE_BUFFER_BIT
|
2479 GL_MAP_FLUSH_EXPLICIT_BIT
|
2480 GL_MAP_UNSYNCHRONIZED_BIT
;
2482 if (ctx
->Extensions
.ARB_buffer_storage
) {
2483 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2484 GL_MAP_COHERENT_BIT
;
2487 if (access
& ~allowed_access
) {
2488 /* generate an error if any bits other than those allowed are set */
2489 _mesa_error(ctx
, GL_INVALID_VALUE
,
2490 "%s(access has undefined bits set)", func
);
2494 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2495 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2496 "%s(access indicates neither read or write)", func
);
2500 if ((access
& GL_MAP_READ_BIT
) &&
2501 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2502 GL_MAP_INVALIDATE_BUFFER_BIT
|
2503 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2505 "%s(read access with disallowed bits)", func
);
2509 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2510 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2511 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2512 "%s(access has flush explicit without write)", func
);
2516 if (access
& GL_MAP_READ_BIT
&&
2517 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2518 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2519 "%s(buffer does not allow read access)", func
);
2523 if (access
& GL_MAP_WRITE_BIT
&&
2524 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2525 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2526 "%s(buffer does not allow write access)", func
);
2530 if (access
& GL_MAP_COHERENT_BIT
&&
2531 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2532 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2533 "%s(buffer does not allow coherent access)", func
);
2537 if (access
& GL_MAP_PERSISTENT_BIT
&&
2538 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2539 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2540 "%s(buffer does not allow persistent access)", func
);
2544 if (offset
+ length
> bufObj
->Size
) {
2545 _mesa_error(ctx
, GL_INVALID_VALUE
,
2546 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
2547 (unsigned long) offset
, (unsigned long) length
,
2548 (unsigned long) bufObj
->Size
);
2552 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2553 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2554 "%s(buffer already mapped)", func
);
2558 if (access
& GL_MAP_WRITE_BIT
) {
2559 bufObj
->NumMapBufferWriteCalls
++;
2560 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2561 bufObj
->Usage
== GL_STATIC_COPY
) &&
2562 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
2563 BUFFER_USAGE_WARNING(ctx
,
2564 "using %s(buffer %u, offset %u, length %u) to "
2565 "update a %s buffer",
2566 func
, bufObj
->Name
, offset
, length
,
2567 _mesa_enum_to_string(bufObj
->Usage
));
2575 map_buffer_range(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2576 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
2579 if (!bufObj
->Size
) {
2580 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2584 assert(ctx
->Driver
.MapBufferRange
);
2585 void *map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2588 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2591 /* The driver callback should have set all these fields.
2592 * This is important because other modules (like VBO) might call
2593 * the driver function directly.
2595 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2596 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2597 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2598 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2601 if (access
& GL_MAP_WRITE_BIT
) {
2602 bufObj
->Written
= GL_TRUE
;
2603 bufObj
->MinMaxCacheDirty
= true;
2607 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2608 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2609 bufObj
->Name
, bufObj
->Size
, access
);
2610 /* Access must be write only */
2611 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2613 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2614 for (i
= 0; i
< bufObj
->Size
; i
++)
2621 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2622 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2624 /* buffer is 100 bytes larger than requested, fill with magic value */
2625 for (i
= 0; i
< 100; i
++) {
2626 buf
[bufObj
->Size
- i
- 1] = 123;
2635 _mesa_MapBufferRange_no_error(GLenum target
, GLintptr offset
,
2636 GLsizeiptr length
, GLbitfield access
)
2638 GET_CURRENT_CONTEXT(ctx
);
2640 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2641 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2643 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2644 "glMapBufferRange");
2648 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2651 GET_CURRENT_CONTEXT(ctx
);
2652 struct gl_buffer_object
*bufObj
;
2654 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2655 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2656 "glMapBufferRange(ARB_map_buffer_range not supported)");
2660 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2664 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2665 "glMapBufferRange"))
2668 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2669 "glMapBufferRange");
2673 _mesa_MapNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
2674 GLsizeiptr length
, GLbitfield access
)
2676 GET_CURRENT_CONTEXT(ctx
);
2677 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2679 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2680 "glMapNamedBufferRange");
2684 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2687 GET_CURRENT_CONTEXT(ctx
);
2688 struct gl_buffer_object
*bufObj
;
2690 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2691 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2692 "glMapNamedBufferRange("
2693 "ARB_map_buffer_range not supported)");
2697 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2701 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2702 "glMapNamedBufferRange"))
2705 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2706 "glMapNamedBufferRange");
2710 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2711 * flags for input to map_buffer_range.
2713 * \return true if the type of requested access is permissible.
2716 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2720 case GL_READ_ONLY_ARB
:
2721 *flags
= GL_MAP_READ_BIT
;
2722 return _mesa_is_desktop_gl(ctx
);
2723 case GL_WRITE_ONLY_ARB
:
2724 *flags
= GL_MAP_WRITE_BIT
;
2726 case GL_READ_WRITE_ARB
:
2727 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2728 return _mesa_is_desktop_gl(ctx
);
2735 _mesa_MapBuffer_no_error(GLenum target
, GLenum access
)
2737 GET_CURRENT_CONTEXT(ctx
);
2739 GLbitfield accessFlags
;
2740 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
2742 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2743 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2745 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2750 _mesa_MapBuffer(GLenum target
, GLenum access
)
2752 GET_CURRENT_CONTEXT(ctx
);
2753 struct gl_buffer_object
*bufObj
;
2754 GLbitfield accessFlags
;
2756 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2757 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2761 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2765 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2769 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2774 _mesa_MapNamedBuffer_no_error(GLuint buffer
, GLenum access
)
2776 GET_CURRENT_CONTEXT(ctx
);
2778 GLbitfield accessFlags
;
2779 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
2781 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2783 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2784 "glMapNamedBuffer");
2788 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2790 GET_CURRENT_CONTEXT(ctx
);
2791 struct gl_buffer_object
*bufObj
;
2792 GLbitfield accessFlags
;
2794 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2795 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2799 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2803 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2804 "glMapNamedBuffer"))
2807 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2808 "glMapNamedBuffer");
2813 flush_mapped_buffer_range(struct gl_context
*ctx
,
2814 struct gl_buffer_object
*bufObj
,
2815 GLintptr offset
, GLsizeiptr length
,
2818 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2819 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2820 "%s(ARB_map_buffer_range not supported)", func
);
2825 _mesa_error(ctx
, GL_INVALID_VALUE
,
2826 "%s(offset %ld < 0)", func
, (long) offset
);
2831 _mesa_error(ctx
, GL_INVALID_VALUE
,
2832 "%s(length %ld < 0)", func
, (long) length
);
2836 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2837 /* buffer is not mapped */
2838 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2839 "%s(buffer is not mapped)", func
);
2843 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2844 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2845 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2846 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2850 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2851 _mesa_error(ctx
, GL_INVALID_VALUE
,
2852 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2853 (long) offset
, (long) length
,
2854 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2858 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2860 if (ctx
->Driver
.FlushMappedBufferRange
)
2861 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2866 _mesa_FlushMappedBufferRange_no_error(GLenum target
, GLintptr offset
,
2869 GET_CURRENT_CONTEXT(ctx
);
2870 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2871 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2873 if (ctx
->Driver
.FlushMappedBufferRange
)
2874 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2879 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2882 GET_CURRENT_CONTEXT(ctx
);
2883 struct gl_buffer_object
*bufObj
;
2885 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2886 GL_INVALID_OPERATION
);
2890 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2891 "glFlushMappedBufferRange");
2895 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
2898 GET_CURRENT_CONTEXT(ctx
);
2899 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2901 if (ctx
->Driver
.FlushMappedBufferRange
)
2902 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2907 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2910 GET_CURRENT_CONTEXT(ctx
);
2911 struct gl_buffer_object
*bufObj
;
2913 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2914 "glFlushMappedNamedBufferRange");
2918 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2919 "glFlushMappedNamedBufferRange");
2924 * Binds a buffer object to a uniform buffer binding point.
2926 * The caller is responsible for flushing vertices and updating
2930 set_ubo_binding(struct gl_context
*ctx
,
2931 struct gl_uniform_buffer_binding
*binding
,
2932 struct gl_buffer_object
*bufObj
,
2937 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2939 binding
->Offset
= offset
;
2940 binding
->Size
= size
;
2941 binding
->AutomaticSize
= autoSize
;
2943 /* If this is a real buffer object, mark it has having been used
2944 * at some point as a UBO.
2947 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2951 * Binds a buffer object to a shader storage buffer binding point.
2953 * The caller is responsible for flushing vertices and updating
2957 set_ssbo_binding(struct gl_context
*ctx
,
2958 struct gl_shader_storage_buffer_binding
*binding
,
2959 struct gl_buffer_object
*bufObj
,
2964 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2966 binding
->Offset
= offset
;
2967 binding
->Size
= size
;
2968 binding
->AutomaticSize
= autoSize
;
2970 /* If this is a real buffer object, mark it has having been used
2971 * at some point as a SSBO.
2974 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
2978 * Binds a buffer object to a uniform buffer binding point.
2980 * Unlike set_ubo_binding(), this function also flushes vertices
2981 * and updates NewDriverState. It also checks if the binding
2982 * has actually changed before updating it.
2985 bind_uniform_buffer(struct gl_context
*ctx
,
2987 struct gl_buffer_object
*bufObj
,
2992 struct gl_uniform_buffer_binding
*binding
=
2993 &ctx
->UniformBufferBindings
[index
];
2995 if (binding
->BufferObject
== bufObj
&&
2996 binding
->Offset
== offset
&&
2997 binding
->Size
== size
&&
2998 binding
->AutomaticSize
== autoSize
) {
3002 FLUSH_VERTICES(ctx
, 0);
3003 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3005 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
3009 * Binds a buffer object to a shader storage buffer binding point.
3011 * Unlike set_ssbo_binding(), this function also flushes vertices
3012 * and updates NewDriverState. It also checks if the binding
3013 * has actually changed before updating it.
3016 bind_shader_storage_buffer(struct gl_context
*ctx
,
3018 struct gl_buffer_object
*bufObj
,
3023 struct gl_shader_storage_buffer_binding
*binding
=
3024 &ctx
->ShaderStorageBufferBindings
[index
];
3026 if (binding
->BufferObject
== bufObj
&&
3027 binding
->Offset
== offset
&&
3028 binding
->Size
== size
&&
3029 binding
->AutomaticSize
== autoSize
) {
3033 FLUSH_VERTICES(ctx
, 0);
3034 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3036 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
3040 * Bind a region of a buffer object to a uniform block binding point.
3041 * \param index the uniform buffer binding point index
3042 * \param bufObj the buffer object
3043 * \param offset offset to the start of buffer object region
3044 * \param size size of the buffer object region
3047 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
3049 struct gl_buffer_object
*bufObj
,
3053 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3054 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3058 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3059 _mesa_error(ctx
, GL_INVALID_VALUE
,
3060 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3061 ctx
->Const
.UniformBufferOffsetAlignment
);
3065 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3070 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3071 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3075 * Bind a region of a buffer object to a shader storage block binding point.
3076 * \param index the shader storage buffer binding point index
3077 * \param bufObj the buffer object
3078 * \param offset offset to the start of buffer object region
3079 * \param size size of the buffer object region
3082 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3084 struct gl_buffer_object
*bufObj
,
3088 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3089 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3093 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3094 _mesa_error(ctx
, GL_INVALID_VALUE
,
3095 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3096 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3100 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3105 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3106 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3110 * Bind a buffer object to a uniform block binding point.
3111 * As above, but offset = 0.
3114 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
3116 struct gl_buffer_object
*bufObj
)
3118 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3119 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
3123 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3125 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3126 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
3128 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
3132 * Bind a buffer object to a shader storage block binding point.
3133 * As above, but offset = 0.
3136 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
3138 struct gl_buffer_object
*bufObj
)
3140 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3141 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
3145 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3147 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3148 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
3150 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
3154 * Binds a buffer object to an atomic buffer binding point.
3156 * The caller is responsible for validating the offset,
3157 * flushing the vertices and updating NewDriverState.
3160 set_atomic_buffer_binding(struct gl_context
*ctx
,
3161 struct gl_atomic_buffer_binding
*binding
,
3162 struct gl_buffer_object
*bufObj
,
3166 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
3168 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3169 binding
->Offset
= 0;
3172 binding
->Offset
= offset
;
3173 binding
->Size
= size
;
3174 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
3179 * Binds a buffer object to an atomic buffer binding point.
3181 * Unlike set_atomic_buffer_binding(), this function also validates the
3182 * index and offset, flushes vertices, and updates NewDriverState.
3183 * It also checks if the binding has actually changing before
3187 bind_atomic_buffer(struct gl_context
*ctx
,
3189 struct gl_buffer_object
*bufObj
,
3194 struct gl_atomic_buffer_binding
*binding
;
3196 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3197 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3201 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3202 _mesa_error(ctx
, GL_INVALID_VALUE
,
3203 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3204 ATOMIC_COUNTER_SIZE
);
3208 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3210 binding
= &ctx
->AtomicBufferBindings
[index
];
3211 if (binding
->BufferObject
== bufObj
&&
3212 binding
->Offset
== offset
&&
3213 binding
->Size
== size
) {
3217 FLUSH_VERTICES(ctx
, 0);
3218 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3220 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3224 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3226 const GLintptr
*offsets
,
3227 const GLsizeiptr
*sizes
)
3229 if (offsets
[index
] < 0) {
3230 /* The ARB_multi_bind spec says:
3232 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3233 * value in <offsets> is less than zero (per binding)."
3235 _mesa_error(ctx
, GL_INVALID_VALUE
,
3236 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3237 index
, (int64_t) offsets
[index
]);
3241 if (sizes
[index
] <= 0) {
3242 /* The ARB_multi_bind spec says:
3244 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3245 * value in <sizes> is less than or equal to zero (per binding)."
3247 _mesa_error(ctx
, GL_INVALID_VALUE
,
3248 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3249 index
, (int64_t) sizes
[index
]);
3257 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3258 GLuint first
, GLsizei count
,
3261 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3262 _mesa_error(ctx
, GL_INVALID_ENUM
,
3263 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3267 /* The ARB_multi_bind_spec says:
3269 * "An INVALID_OPERATION error is generated if <first> + <count> is
3270 * greater than the number of target-specific indexed binding points,
3271 * as described in section 6.7.1."
3273 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3274 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3275 "%s(first=%u + count=%d > the value of "
3276 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3277 caller
, first
, count
,
3278 ctx
->Const
.MaxUniformBufferBindings
);
3286 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3287 GLuint first
, GLsizei count
,
3290 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3291 _mesa_error(ctx
, GL_INVALID_ENUM
,
3292 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3296 /* The ARB_multi_bind_spec says:
3298 * "An INVALID_OPERATION error is generated if <first> + <count> is
3299 * greater than the number of target-specific indexed binding points,
3300 * as described in section 6.7.1."
3302 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3303 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3304 "%s(first=%u + count=%d > the value of "
3305 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3306 caller
, first
, count
,
3307 ctx
->Const
.MaxShaderStorageBufferBindings
);
3315 * Unbind all uniform buffers in the range
3316 * <first> through <first>+<count>-1
3319 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3321 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3323 for (int i
= 0; i
< count
; i
++)
3324 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3325 bufObj
, -1, -1, GL_TRUE
);
3329 * Unbind all shader storage buffers in the range
3330 * <first> through <first>+<count>-1
3333 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3336 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3338 for (int i
= 0; i
< count
; i
++)
3339 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3340 bufObj
, -1, -1, GL_TRUE
);
3344 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3345 const GLuint
*buffers
,
3347 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3350 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3353 /* Assume that at least one binding will be changed */
3354 FLUSH_VERTICES(ctx
, 0);
3355 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3358 /* The ARB_multi_bind spec says:
3360 * "If <buffers> is NULL, all bindings from <first> through
3361 * <first>+<count>-1 are reset to their unbound (zero) state.
3362 * In this case, the offsets and sizes associated with the
3363 * binding points are set to default values, ignoring
3364 * <offsets> and <sizes>."
3366 unbind_uniform_buffers(ctx
, first
, count
);
3370 /* Note that the error semantics for multi-bind commands differ from
3371 * those of other GL commands.
3373 * The Issues section in the ARB_multi_bind spec says:
3375 * "(11) Typically, OpenGL specifies that if an error is generated by a
3376 * command, that command has no effect. This is somewhat
3377 * unfortunate for multi-bind commands, because it would require a
3378 * first pass to scan the entire list of bound objects for errors
3379 * and then a second pass to actually perform the bindings.
3380 * Should we have different error semantics?
3382 * RESOLVED: Yes. In this specification, when the parameters for
3383 * one of the <count> binding points are invalid, that binding point
3384 * is not updated and an error will be generated. However, other
3385 * binding points in the same command will be updated if their
3386 * parameters are valid and no other error occurs."
3389 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3391 for (int i
= 0; i
< count
; i
++) {
3392 struct gl_uniform_buffer_binding
*binding
=
3393 &ctx
->UniformBufferBindings
[first
+ i
];
3394 struct gl_buffer_object
*bufObj
;
3395 GLintptr offset
= 0;
3396 GLsizeiptr size
= 0;
3399 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3402 /* The ARB_multi_bind spec says:
3404 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3405 * pair of values in <offsets> and <sizes> does not respectively
3406 * satisfy the constraints described for those parameters for the
3407 * specified target, as described in section 6.7.1 (per binding)."
3409 * Section 6.7.1 refers to table 6.5, which says:
3411 * "┌───────────────────────────────────────────────────────────────┐
3412 * │ Uniform buffer array bindings (see sec. 7.6) │
3413 * ├─────────────────────┬─────────────────────────────────────────┤
3415 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3416 * │ │ OFFSET_ALIGNMENT │
3418 * │ size restriction │ none │
3419 * └─────────────────────┴─────────────────────────────────────────┘"
3421 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3422 _mesa_error(ctx
, GL_INVALID_VALUE
,
3423 "glBindBuffersRange(offsets[%u]=%" PRId64
3424 " is misaligned; it must be a multiple of the value of "
3425 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3426 "target=GL_UNIFORM_BUFFER)",
3427 i
, (int64_t) offsets
[i
],
3428 ctx
->Const
.UniformBufferOffsetAlignment
);
3432 offset
= offsets
[i
];
3436 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3437 bufObj
= binding
->BufferObject
;
3439 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3442 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3443 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3445 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3449 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3453 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3454 GLsizei count
, const GLuint
*buffers
,
3456 const GLintptr
*offsets
,
3457 const GLsizeiptr
*sizes
,
3460 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3463 /* Assume that at least one binding will be changed */
3464 FLUSH_VERTICES(ctx
, 0);
3465 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3468 /* The ARB_multi_bind spec says:
3470 * "If <buffers> is NULL, all bindings from <first> through
3471 * <first>+<count>-1 are reset to their unbound (zero) state.
3472 * In this case, the offsets and sizes associated with the
3473 * binding points are set to default values, ignoring
3474 * <offsets> and <sizes>."
3476 unbind_shader_storage_buffers(ctx
, first
, count
);
3480 /* Note that the error semantics for multi-bind commands differ from
3481 * those of other GL commands.
3483 * The Issues section in the ARB_multi_bind spec says:
3485 * "(11) Typically, OpenGL specifies that if an error is generated by a
3486 * command, that command has no effect. This is somewhat
3487 * unfortunate for multi-bind commands, because it would require a
3488 * first pass to scan the entire list of bound objects for errors
3489 * and then a second pass to actually perform the bindings.
3490 * Should we have different error semantics?
3492 * RESOLVED: Yes. In this specification, when the parameters for
3493 * one of the <count> binding points are invalid, that binding point
3494 * is not updated and an error will be generated. However, other
3495 * binding points in the same command will be updated if their
3496 * parameters are valid and no other error occurs."
3499 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3501 for (int i
= 0; i
< count
; i
++) {
3502 struct gl_shader_storage_buffer_binding
*binding
=
3503 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3504 struct gl_buffer_object
*bufObj
;
3505 GLintptr offset
= 0;
3506 GLsizeiptr size
= 0;
3509 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3512 /* The ARB_multi_bind spec says:
3514 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3515 * pair of values in <offsets> and <sizes> does not respectively
3516 * satisfy the constraints described for those parameters for the
3517 * specified target, as described in section 6.7.1 (per binding)."
3519 * Section 6.7.1 refers to table 6.5, which says:
3521 * "┌───────────────────────────────────────────────────────────────┐
3522 * │ Shader storage buffer array bindings (see sec. 7.8) │
3523 * ├─────────────────────┬─────────────────────────────────────────┤
3525 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3526 * │ │ BUFFER_OFFSET_ALIGNMENT │
3528 * │ size restriction │ none │
3529 * └─────────────────────┴─────────────────────────────────────────┘"
3531 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3532 _mesa_error(ctx
, GL_INVALID_VALUE
,
3533 "glBindBuffersRange(offsets[%u]=%" PRId64
3534 " is misaligned; it must be a multiple of the value of "
3535 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3536 "target=GL_SHADER_STORAGE_BUFFER)",
3537 i
, (int64_t) offsets
[i
],
3538 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3542 offset
= offsets
[i
];
3546 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3547 bufObj
= binding
->BufferObject
;
3549 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3552 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3553 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3555 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3559 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3563 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3564 struct gl_transform_feedback_object
*tfObj
,
3565 GLuint first
, GLsizei count
, const char *caller
)
3567 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3568 _mesa_error(ctx
, GL_INVALID_ENUM
,
3569 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3573 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3575 * "An INVALID_OPERATION error is generated :
3578 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3579 * FEEDBACK_BUFFER and transform feedback is currently active."
3581 * We assume that this is also meant to apply to BindBuffersRange
3582 * and BindBuffersBase.
3584 if (tfObj
->Active
) {
3585 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3586 "%s(Changing transform feedback buffers while "
3587 "transform feedback is active)", caller
);
3591 /* The ARB_multi_bind_spec says:
3593 * "An INVALID_OPERATION error is generated if <first> + <count> is
3594 * greater than the number of target-specific indexed binding points,
3595 * as described in section 6.7.1."
3597 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3598 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3599 "%s(first=%u + count=%d > the value of "
3600 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3601 caller
, first
, count
,
3602 ctx
->Const
.MaxTransformFeedbackBuffers
);
3610 * Unbind all transform feedback buffers in the range
3611 * <first> through <first>+<count>-1
3614 unbind_xfb_buffers(struct gl_context
*ctx
,
3615 struct gl_transform_feedback_object
*tfObj
,
3616 GLuint first
, GLsizei count
)
3618 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3620 for (int i
= 0; i
< count
; i
++)
3621 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3626 bind_xfb_buffers(struct gl_context
*ctx
,
3627 GLuint first
, GLsizei count
,
3628 const GLuint
*buffers
,
3630 const GLintptr
*offsets
,
3631 const GLsizeiptr
*sizes
,
3634 struct gl_transform_feedback_object
*tfObj
=
3635 ctx
->TransformFeedback
.CurrentObject
;
3637 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
3640 /* Assume that at least one binding will be changed */
3641 FLUSH_VERTICES(ctx
, 0);
3642 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3645 /* The ARB_multi_bind spec says:
3647 * "If <buffers> is NULL, all bindings from <first> through
3648 * <first>+<count>-1 are reset to their unbound (zero) state.
3649 * In this case, the offsets and sizes associated with the
3650 * binding points are set to default values, ignoring
3651 * <offsets> and <sizes>."
3653 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3657 /* Note that the error semantics for multi-bind commands differ from
3658 * those of other GL commands.
3660 * The Issues section in the ARB_multi_bind spec says:
3662 * "(11) Typically, OpenGL specifies that if an error is generated by a
3663 * command, that command has no effect. This is somewhat
3664 * unfortunate for multi-bind commands, because it would require a
3665 * first pass to scan the entire list of bound objects for errors
3666 * and then a second pass to actually perform the bindings.
3667 * Should we have different error semantics?
3669 * RESOLVED: Yes. In this specification, when the parameters for
3670 * one of the <count> binding points are invalid, that binding point
3671 * is not updated and an error will be generated. However, other
3672 * binding points in the same command will be updated if their
3673 * parameters are valid and no other error occurs."
3676 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3678 for (int i
= 0; i
< count
; i
++) {
3679 const GLuint index
= first
+ i
;
3680 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3681 struct gl_buffer_object
*bufObj
;
3682 GLintptr offset
= 0;
3683 GLsizeiptr size
= 0;
3686 offset
= offsets
[i
];
3689 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3692 /* The ARB_multi_bind spec says:
3694 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3695 * pair of values in <offsets> and <sizes> does not respectively
3696 * satisfy the constraints described for those parameters for the
3697 * specified target, as described in section 6.7.1 (per binding)."
3699 * Section 6.7.1 refers to table 6.5, which says:
3701 * "┌───────────────────────────────────────────────────────────────┐
3702 * │ Transform feedback array bindings (see sec. 13.2.2) │
3703 * ├───────────────────────┬───────────────────────────────────────┤
3705 * │ offset restriction │ multiple of 4 │
3707 * │ size restriction │ multiple of 4 │
3708 * └───────────────────────┴───────────────────────────────────────┘"
3710 if (offsets
[i
] & 0x3) {
3711 _mesa_error(ctx
, GL_INVALID_VALUE
,
3712 "glBindBuffersRange(offsets[%u]=%" PRId64
3713 " is misaligned; it must be a multiple of 4 when "
3714 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3715 i
, (int64_t) offsets
[i
]);
3719 if (sizes
[i
] & 0x3) {
3720 _mesa_error(ctx
, GL_INVALID_VALUE
,
3721 "glBindBuffersRange(sizes[%u]=%" PRId64
3722 " is misaligned; it must be a multiple of 4 when "
3723 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3724 i
, (int64_t) sizes
[i
]);
3728 offset
= offsets
[i
];
3732 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3733 bufObj
= boundBufObj
;
3735 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3738 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3742 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3746 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3747 GLuint first
, GLsizei count
,
3750 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3751 _mesa_error(ctx
, GL_INVALID_ENUM
,
3752 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3756 /* The ARB_multi_bind_spec says:
3758 * "An INVALID_OPERATION error is generated if <first> + <count> is
3759 * greater than the number of target-specific indexed binding points,
3760 * as described in section 6.7.1."
3762 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3763 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3764 "%s(first=%u + count=%d > the value of "
3765 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3766 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3774 * Unbind all atomic counter buffers in the range
3775 * <first> through <first>+<count>-1
3778 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3780 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3782 for (int i
= 0; i
< count
; i
++)
3783 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3788 bind_atomic_buffers(struct gl_context
*ctx
,
3791 const GLuint
*buffers
,
3793 const GLintptr
*offsets
,
3794 const GLsizeiptr
*sizes
,
3797 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
3800 /* Assume that at least one binding will be changed */
3801 FLUSH_VERTICES(ctx
, 0);
3802 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3805 /* The ARB_multi_bind spec says:
3807 * "If <buffers> is NULL, all bindings from <first> through
3808 * <first>+<count>-1 are reset to their unbound (zero) state.
3809 * In this case, the offsets and sizes associated with the
3810 * binding points are set to default values, ignoring
3811 * <offsets> and <sizes>."
3813 unbind_atomic_buffers(ctx
, first
, count
);
3817 /* Note that the error semantics for multi-bind commands differ from
3818 * those of other GL commands.
3820 * The Issues section in the ARB_multi_bind spec says:
3822 * "(11) Typically, OpenGL specifies that if an error is generated by a
3823 * command, that command has no effect. This is somewhat
3824 * unfortunate for multi-bind commands, because it would require a
3825 * first pass to scan the entire list of bound objects for errors
3826 * and then a second pass to actually perform the bindings.
3827 * Should we have different error semantics?
3829 * RESOLVED: Yes. In this specification, when the parameters for
3830 * one of the <count> binding points are invalid, that binding point
3831 * is not updated and an error will be generated. However, other
3832 * binding points in the same command will be updated if their
3833 * parameters are valid and no other error occurs."
3836 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3838 for (int i
= 0; i
< count
; i
++) {
3839 struct gl_atomic_buffer_binding
*binding
=
3840 &ctx
->AtomicBufferBindings
[first
+ i
];
3841 struct gl_buffer_object
*bufObj
;
3842 GLintptr offset
= 0;
3843 GLsizeiptr size
= 0;
3846 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3849 /* The ARB_multi_bind spec says:
3851 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3852 * pair of values in <offsets> and <sizes> does not respectively
3853 * satisfy the constraints described for those parameters for the
3854 * specified target, as described in section 6.7.1 (per binding)."
3856 * Section 6.7.1 refers to table 6.5, which says:
3858 * "┌───────────────────────────────────────────────────────────────┐
3859 * │ Atomic counter array bindings (see sec. 7.7.2) │
3860 * ├───────────────────────┬───────────────────────────────────────┤
3862 * │ offset restriction │ multiple of 4 │
3864 * │ size restriction │ none │
3865 * └───────────────────────┴───────────────────────────────────────┘"
3867 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3868 _mesa_error(ctx
, GL_INVALID_VALUE
,
3869 "glBindBuffersRange(offsets[%u]=%" PRId64
3870 " is misaligned; it must be a multiple of %d when "
3871 "target=GL_ATOMIC_COUNTER_BUFFER)",
3872 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3876 offset
= offsets
[i
];
3880 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3881 bufObj
= binding
->BufferObject
;
3883 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3886 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3889 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3893 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3894 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3896 GET_CURRENT_CONTEXT(ctx
);
3897 struct gl_buffer_object
*bufObj
;
3899 if (MESA_VERBOSE
& VERBOSE_API
) {
3900 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
3901 _mesa_enum_to_string(target
), index
, buffer
,
3902 (unsigned long) offset
, (unsigned long) size
);
3906 bufObj
= ctx
->Shared
->NullBufferObj
;
3908 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3910 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3911 &bufObj
, "glBindBufferRange"))
3915 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3916 "glBindBufferRange(invalid buffer=%u)", buffer
);
3922 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3929 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3930 _mesa_bind_buffer_range_transform_feedback(ctx
,
3931 ctx
->TransformFeedback
.CurrentObject
,
3932 index
, bufObj
, offset
, size
,
3935 case GL_UNIFORM_BUFFER
:
3936 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3938 case GL_SHADER_STORAGE_BUFFER
:
3939 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3941 case GL_ATOMIC_COUNTER_BUFFER
:
3942 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3943 "glBindBufferRange");
3946 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3952 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3954 GET_CURRENT_CONTEXT(ctx
);
3955 struct gl_buffer_object
*bufObj
;
3957 if (MESA_VERBOSE
& VERBOSE_API
) {
3958 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
3959 _mesa_enum_to_string(target
), index
, buffer
);
3963 bufObj
= ctx
->Shared
->NullBufferObj
;
3965 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3967 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3968 &bufObj
, "glBindBufferBase"))
3972 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3973 "glBindBufferBase(invalid buffer=%u)", buffer
);
3977 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3978 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3980 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3981 * zero and size equal to the size of buffer."
3983 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3985 * "If the parameter (starting offset or size) was not specified when the
3986 * buffer object was bound, zero is returned."
3988 * What happens if the size of the buffer changes? Does the size of the
3989 * buffer at the moment glBindBufferBase was called still play a role, like
3990 * the first quote would imply, or is the size meaningless in the
3991 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3992 * core spec page 45 says:
3994 * "It is equivalent to calling BindBufferRange with offset zero, while
3995 * size is determined by the size of the bound buffer at the time the
3998 * My interpretation is that the GL 4.1 spec was a clarification of the
3999 * behavior, not a change. In particular, this choice will only make
4000 * rendering work in cases where it would have had undefined results.
4004 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4005 _mesa_bind_buffer_base_transform_feedback(ctx
,
4006 ctx
->TransformFeedback
.CurrentObject
,
4007 index
, bufObj
, false);
4009 case GL_UNIFORM_BUFFER
:
4010 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4012 case GL_SHADER_STORAGE_BUFFER
:
4013 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4015 case GL_ATOMIC_COUNTER_BUFFER
:
4016 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
4017 "glBindBufferBase");
4020 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4026 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4027 const GLuint
*buffers
,
4028 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4030 GET_CURRENT_CONTEXT(ctx
);
4032 if (MESA_VERBOSE
& VERBOSE_API
) {
4033 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4034 _mesa_enum_to_string(target
), first
, count
,
4035 buffers
, offsets
, sizes
);
4039 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4040 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4041 "glBindBuffersRange");
4043 case GL_UNIFORM_BUFFER
:
4044 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4045 "glBindBuffersRange");
4047 case GL_SHADER_STORAGE_BUFFER
:
4048 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4049 "glBindBuffersRange");
4051 case GL_ATOMIC_COUNTER_BUFFER
:
4052 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4053 "glBindBuffersRange");
4056 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4057 _mesa_enum_to_string(target
));
4063 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4064 const GLuint
*buffers
)
4066 GET_CURRENT_CONTEXT(ctx
);
4068 if (MESA_VERBOSE
& VERBOSE_API
) {
4069 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4070 _mesa_enum_to_string(target
), first
, count
, buffers
);
4074 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4075 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4076 "glBindBuffersBase");
4078 case GL_UNIFORM_BUFFER
:
4079 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4080 "glBindBuffersBase");
4082 case GL_SHADER_STORAGE_BUFFER
:
4083 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4084 "glBindBuffersBase");
4086 case GL_ATOMIC_COUNTER_BUFFER
:
4087 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4088 "glBindBuffersBase");
4091 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4092 _mesa_enum_to_string(target
));
4098 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4101 GET_CURRENT_CONTEXT(ctx
);
4102 struct gl_buffer_object
*bufObj
;
4103 const GLintptr end
= offset
+ length
;
4105 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4106 * Profile) spec says:
4108 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4109 * name of an existing buffer object."
4111 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4112 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4113 _mesa_error(ctx
, GL_INVALID_VALUE
,
4114 "glInvalidateBufferSubData(name = %u) invalid object",
4119 /* The GL_ARB_invalidate_subdata spec says:
4121 * "An INVALID_VALUE error is generated if <offset> or <length> is
4122 * negative, or if <offset> + <length> is greater than the value of
4125 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
4126 _mesa_error(ctx
, GL_INVALID_VALUE
,
4127 "glInvalidateBufferSubData(invalid offset or length)");
4131 /* The OpenGL 4.4 (Core Profile) spec says:
4133 * "An INVALID_OPERATION error is generated if buffer is currently
4134 * mapped by MapBuffer or if the invalidate range intersects the range
4135 * currently mapped by MapBufferRange, unless it was mapped
4136 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4138 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4139 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4140 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4141 "glInvalidateBufferSubData(intersection with mapped "
4146 if (ctx
->Driver
.InvalidateBufferSubData
)
4147 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4151 _mesa_InvalidateBufferData(GLuint buffer
)
4153 GET_CURRENT_CONTEXT(ctx
);
4154 struct gl_buffer_object
*bufObj
;
4156 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4157 * Profile) spec says:
4159 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4160 * name of an existing buffer object."
4162 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4163 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4164 _mesa_error(ctx
, GL_INVALID_VALUE
,
4165 "glInvalidateBufferData(name = %u) invalid object",
4170 /* The OpenGL 4.4 (Core Profile) spec says:
4172 * "An INVALID_OPERATION error is generated if buffer is currently
4173 * mapped by MapBuffer or if the invalidate range intersects the range
4174 * currently mapped by MapBufferRange, unless it was mapped
4175 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4177 if (_mesa_check_disallowed_mapping(bufObj
)) {
4178 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4179 "glInvalidateBufferData(intersection with mapped "
4184 if (ctx
->Driver
.InvalidateBufferSubData
)
4185 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, 0, bufObj
->Size
);
4189 buffer_page_commitment(struct gl_context
*ctx
,
4190 struct gl_buffer_object
*bufferObj
,
4191 GLintptr offset
, GLsizeiptr size
,
4192 GLboolean commit
, const char *func
)
4194 if (!(bufferObj
->StorageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)) {
4195 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(not a sparse buffer object)",
4200 if (size
< 0 || size
> bufferObj
->Size
||
4201 offset
< 0 || offset
> bufferObj
->Size
- size
) {
4202 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(out of bounds)",
4207 /* The GL_ARB_sparse_buffer extension specification says:
4209 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4210 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4211 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4212 * not extend to the end of the buffer's data store."
4214 if (offset
% ctx
->Const
.SparseBufferPageSize
!= 0) {
4215 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset not aligned to page size)",
4220 if (size
% ctx
->Const
.SparseBufferPageSize
!= 0 &&
4221 offset
+ size
!= bufferObj
->Size
) {
4222 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size not aligned to page size)",
4227 ctx
->Driver
.BufferPageCommitment(ctx
, bufferObj
, offset
, size
, commit
);
4231 _mesa_BufferPageCommitmentARB(GLenum target
, GLintptr offset
, GLsizeiptr size
,
4234 GET_CURRENT_CONTEXT(ctx
);
4235 struct gl_buffer_object
*bufferObj
;
4237 bufferObj
= get_buffer(ctx
, "glBufferPageCommitmentARB", target
,
4242 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4243 "glBufferPageCommitmentARB");
4247 _mesa_NamedBufferPageCommitmentARB(GLuint buffer
, GLintptr offset
,
4248 GLsizeiptr size
, GLboolean commit
)
4250 GET_CURRENT_CONTEXT(ctx
);
4251 struct gl_buffer_object
*bufferObj
;
4253 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4254 if (!bufferObj
|| bufferObj
== &DummyBufferObject
) {
4255 /* Note: the extension spec is not clear about the excpected error value. */
4256 _mesa_error(ctx
, GL_INVALID_VALUE
,
4257 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4262 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4263 "glNamedBufferPageCommitmentARB");