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 _mesa_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
= _mesa_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
)
1219 for (i
= 0; i
< MAP_COUNT
; i
++) {
1220 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1221 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1222 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1223 bufObj
->Mappings
[i
].AccessFlags
= 0;
1229 /**********************************************************************/
1231 /**********************************************************************/
1234 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1236 GET_CURRENT_CONTEXT(ctx
);
1238 if (MESA_VERBOSE
& VERBOSE_API
) {
1239 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1240 _mesa_enum_to_string(target
), buffer
);
1243 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1245 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target %s)",
1246 _mesa_enum_to_string(target
));
1250 bind_buffer_object(ctx
, bindTarget
, buffer
);
1255 * Delete a set of buffer objects.
1257 * \param n Number of buffer objects to delete.
1258 * \param ids Array of \c n buffer object IDs.
1261 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1263 GET_CURRENT_CONTEXT(ctx
);
1265 FLUSH_VERTICES(ctx
, 0);
1268 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1272 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1274 for (i
= 0; i
< n
; i
++) {
1275 struct gl_buffer_object
*bufObj
=
1276 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1278 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1281 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1283 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1285 /* unbind any vertex pointers bound to this buffer */
1286 for (j
= 0; j
< ARRAY_SIZE(vao
->BufferBinding
); j
++) {
1287 unbind(ctx
, vao
, j
, bufObj
);
1290 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1291 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1293 if (vao
->IndexBufferObj
== bufObj
) {
1294 bind_buffer_object(ctx
, &vao
->IndexBufferObj
, 0);
1297 /* unbind ARB_draw_indirect binding point */
1298 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1299 bind_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, 0);
1302 /* unbind ARB_indirect_parameters binding point */
1303 if (ctx
->ParameterBuffer
== bufObj
) {
1304 bind_buffer_object(ctx
, &ctx
->ParameterBuffer
, 0);
1307 /* unbind ARB_compute_shader binding point */
1308 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1309 bind_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, 0);
1312 /* unbind ARB_copy_buffer binding points */
1313 if (ctx
->CopyReadBuffer
== bufObj
) {
1314 bind_buffer_object(ctx
, &ctx
->CopyReadBuffer
, 0);
1316 if (ctx
->CopyWriteBuffer
== bufObj
) {
1317 bind_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, 0);
1320 /* unbind transform feedback binding points */
1321 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1322 bind_buffer_object(ctx
, &ctx
->TransformFeedback
.CurrentBuffer
, 0);
1324 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1325 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1326 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1330 /* unbind UBO binding points */
1331 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1332 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1333 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1337 if (ctx
->UniformBuffer
== bufObj
) {
1338 bind_buffer_object(ctx
, &ctx
->UniformBuffer
, 0);
1341 /* unbind SSBO binding points */
1342 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1343 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1344 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1348 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1349 bind_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, 0);
1352 /* unbind Atomci Buffer binding points */
1353 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1354 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1355 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1359 if (ctx
->AtomicBuffer
== bufObj
) {
1360 bind_buffer_object(ctx
, &ctx
->AtomicBuffer
, 0);
1363 /* unbind any pixel pack/unpack pointers bound to this buffer */
1364 if (ctx
->Pack
.BufferObj
== bufObj
) {
1365 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1367 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1368 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1371 if (ctx
->Texture
.BufferObject
== bufObj
) {
1372 bind_buffer_object(ctx
, &ctx
->Texture
.BufferObject
, 0);
1375 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1376 bind_buffer_object(ctx
, &ctx
->ExternalVirtualMemoryBuffer
, 0);
1379 /* unbind query buffer binding point */
1380 if (ctx
->QueryBuffer
== bufObj
) {
1381 bind_buffer_object(ctx
, &ctx
->QueryBuffer
, 0);
1384 /* The ID is immediately freed for re-use */
1385 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1386 /* Make sure we do not run into the classic ABA problem on bind.
1387 * We don't want to allow re-binding a buffer object that's been
1388 * "deleted" by glDeleteBuffers().
1390 * The explicit rebinding to the default object in the current context
1391 * prevents the above in the current context, but another context
1392 * sharing the same objects might suffer from this problem.
1393 * The alternative would be to do the hash lookup in any case on bind
1394 * which would introduce more runtime overhead than this.
1396 bufObj
->DeletePending
= GL_TRUE
;
1397 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1401 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1406 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1407 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1411 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1413 GET_CURRENT_CONTEXT(ctx
);
1416 struct gl_buffer_object
*buf
;
1418 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1420 if (MESA_VERBOSE
& VERBOSE_API
)
1421 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1424 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1433 * This must be atomic (generation and allocation of buffer object IDs)
1435 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1437 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1439 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1440 * DummyBufferObject. Otherwise, create a new buffer object and insert
1443 for (i
= 0; i
< n
; i
++) {
1444 buffers
[i
] = first
+ i
;
1446 assert(ctx
->Driver
.NewBufferObject
);
1447 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1449 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1450 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1455 buf
= &DummyBufferObject
;
1457 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1460 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1464 * Generate a set of unique buffer object IDs and store them in \c buffers.
1466 * \param n Number of IDs to generate.
1467 * \param buffers Array of \c n locations to store the IDs.
1470 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1472 create_buffers(n
, buffers
, false);
1476 * Create a set of buffer objects and store their unique IDs in \c buffers.
1478 * \param n Number of IDs to generate.
1479 * \param buffers Array of \c n locations to store the IDs.
1482 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1484 create_buffers(n
, buffers
, true);
1489 * Determine if ID is the name of a buffer object.
1491 * \param id ID of the potential buffer object.
1492 * \return \c GL_TRUE if \c id is the name of a buffer object,
1493 * \c GL_FALSE otherwise.
1495 GLboolean GLAPIENTRY
1496 _mesa_IsBuffer(GLuint id
)
1498 struct gl_buffer_object
*bufObj
;
1499 GET_CURRENT_CONTEXT(ctx
);
1500 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1502 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1504 return bufObj
&& bufObj
!= &DummyBufferObject
;
1509 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1510 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1511 GLbitfield flags
, const char *func
)
1514 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1518 GLbitfield valid_flags
= GL_MAP_READ_BIT
|
1520 GL_MAP_PERSISTENT_BIT
|
1521 GL_MAP_COHERENT_BIT
|
1522 GL_DYNAMIC_STORAGE_BIT
|
1523 GL_CLIENT_STORAGE_BIT
;
1525 if (ctx
->Extensions
.ARB_sparse_buffer
)
1526 valid_flags
|= GL_SPARSE_STORAGE_BIT_ARB
;
1528 if (flags
& ~valid_flags
) {
1529 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1533 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1535 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1536 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1537 * MAP_READ_BIT or MAP_WRITE_BIT."
1539 if (flags
& GL_SPARSE_STORAGE_BIT_ARB
&&
1540 flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) {
1541 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(SPARSE_STORAGE and READ/WRITE)", func
);
1545 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1546 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1547 _mesa_error(ctx
, GL_INVALID_VALUE
,
1548 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1552 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1553 _mesa_error(ctx
, GL_INVALID_VALUE
,
1554 "%s(COHERENT and flags!=PERSISTENT)", func
);
1558 if (bufObj
->Immutable
) {
1559 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1563 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1564 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1566 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1568 bufObj
->Written
= GL_TRUE
;
1569 bufObj
->Immutable
= GL_TRUE
;
1570 bufObj
->MinMaxCacheDirty
= true;
1572 assert(ctx
->Driver
.BufferData
);
1573 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1575 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1576 /* Even though the interaction between AMD_pinned_memory and
1577 * glBufferStorage is not described in the spec, Graham Sellers
1578 * said that it should behave the same as glBufferData.
1580 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1583 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1589 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1592 GET_CURRENT_CONTEXT(ctx
);
1593 struct gl_buffer_object
*bufObj
;
1595 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1599 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1604 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1607 GET_CURRENT_CONTEXT(ctx
);
1608 struct gl_buffer_object
*bufObj
;
1610 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1615 * In direct state access, buffer objects have an unspecified target since
1616 * they are not required to be bound.
1618 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1619 "glNamedBufferStorage");
1624 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1625 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1626 GLenum usage
, const char *func
)
1630 if (MESA_VERBOSE
& VERBOSE_API
) {
1631 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1633 _mesa_enum_to_string(target
),
1634 (long int) size
, data
,
1635 _mesa_enum_to_string(usage
));
1639 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1644 case GL_STREAM_DRAW_ARB
:
1645 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1648 case GL_STATIC_DRAW_ARB
:
1649 case GL_DYNAMIC_DRAW_ARB
:
1653 case GL_STREAM_READ_ARB
:
1654 case GL_STREAM_COPY_ARB
:
1655 case GL_STATIC_READ_ARB
:
1656 case GL_STATIC_COPY_ARB
:
1657 case GL_DYNAMIC_READ_ARB
:
1658 case GL_DYNAMIC_COPY_ARB
:
1659 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1663 valid_usage
= false;
1668 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1669 _mesa_enum_to_string(usage
));
1673 if (bufObj
->Immutable
) {
1674 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1678 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1679 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1681 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1683 bufObj
->Written
= GL_TRUE
;
1684 bufObj
->MinMaxCacheDirty
= true;
1687 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1688 bufObj
->Name
, size
, data
, usage
);
1695 assert(ctx
->Driver
.BufferData
);
1696 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1699 GL_DYNAMIC_STORAGE_BIT
,
1701 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1702 /* From GL_AMD_pinned_memory:
1704 * INVALID_OPERATION is generated by BufferData if <target> is
1705 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1706 * mapped to the GPU address space.
1708 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1711 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1717 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1718 const GLvoid
*data
, GLenum usage
)
1720 GET_CURRENT_CONTEXT(ctx
);
1721 struct gl_buffer_object
*bufObj
;
1723 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1727 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1732 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1735 GET_CURRENT_CONTEXT(ctx
);
1736 struct gl_buffer_object
*bufObj
;
1738 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1742 /* In direct state access, buffer objects have an unspecified target since
1743 * they are not required to be bound.
1745 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1746 "glNamedBufferData");
1751 * Implementation for glBufferSubData and glNamedBufferSubData.
1753 * \param ctx GL context.
1754 * \param bufObj The buffer object.
1755 * \param offset Offset of the first byte of the subdata range.
1756 * \param size Size, in bytes, of the subdata range.
1757 * \param data The data store.
1758 * \param func Name of calling function for recording errors.
1762 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1763 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1766 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1768 /* error already recorded */
1772 if (bufObj
->Immutable
&&
1773 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1774 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1781 bufObj
->NumSubDataCalls
++;
1783 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
1784 bufObj
->Usage
== GL_STATIC_COPY
) &&
1785 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
) {
1786 /* If the application declared the buffer as static draw/copy or stream
1787 * draw, it should not be frequently modified with glBufferSubData.
1789 BUFFER_USAGE_WARNING(ctx
,
1790 "using %s(buffer %u, offset %u, size %u) to "
1791 "update a %s buffer",
1792 func
, bufObj
->Name
, offset
, size
,
1793 _mesa_enum_to_string(bufObj
->Usage
));
1796 bufObj
->Written
= GL_TRUE
;
1797 bufObj
->MinMaxCacheDirty
= true;
1799 assert(ctx
->Driver
.BufferSubData
);
1800 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1804 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1805 GLsizeiptr size
, const GLvoid
*data
)
1807 GET_CURRENT_CONTEXT(ctx
);
1808 struct gl_buffer_object
*bufObj
;
1810 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1814 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1818 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1819 GLsizeiptr size
, const GLvoid
*data
)
1821 GET_CURRENT_CONTEXT(ctx
);
1822 struct gl_buffer_object
*bufObj
;
1824 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1828 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1829 "glNamedBufferSubData");
1834 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1835 GLsizeiptr size
, GLvoid
*data
)
1837 GET_CURRENT_CONTEXT(ctx
);
1838 struct gl_buffer_object
*bufObj
;
1840 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1841 GL_INVALID_OPERATION
);
1845 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1846 "glGetBufferSubData")) {
1850 assert(ctx
->Driver
.GetBufferSubData
);
1851 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1855 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1856 GLsizeiptr size
, GLvoid
*data
)
1858 GET_CURRENT_CONTEXT(ctx
);
1859 struct gl_buffer_object
*bufObj
;
1861 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1862 "glGetNamedBufferSubData");
1866 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1867 "glGetNamedBufferSubData")) {
1871 assert(ctx
->Driver
.GetBufferSubData
);
1872 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1877 * \param subdata true if caller is *SubData, false if *Data
1880 clear_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1881 GLenum internalformat
, GLintptr offset
, GLsizeiptr size
,
1882 GLenum format
, GLenum type
, const GLvoid
*data
,
1883 const char *func
, bool subdata
)
1885 mesa_format mesaFormat
;
1886 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1887 GLsizeiptr clearValueSize
;
1889 /* This checks for disallowed mappings. */
1890 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1895 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1896 format
, type
, func
);
1898 if (mesaFormat
== MESA_FORMAT_NONE
) {
1902 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1903 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1904 _mesa_error(ctx
, GL_INVALID_VALUE
,
1905 "%s(offset or size is not a multiple of "
1906 "internalformat size)", func
);
1910 /* Bail early. Negative size has already been checked. */
1914 bufObj
->MinMaxCacheDirty
= true;
1917 /* clear to zeros, per the spec */
1918 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1919 NULL
, clearValueSize
, bufObj
);
1923 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1924 format
, type
, data
, func
)) {
1928 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1929 clearValue
, clearValueSize
, bufObj
);
1933 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1934 GLenum type
, const GLvoid
*data
)
1936 GET_CURRENT_CONTEXT(ctx
);
1937 struct gl_buffer_object
*bufObj
;
1939 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1943 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1944 format
, type
, data
, "glClearBufferData", false);
1948 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1949 GLenum format
, GLenum type
, const GLvoid
*data
)
1951 GET_CURRENT_CONTEXT(ctx
);
1952 struct gl_buffer_object
*bufObj
;
1954 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1958 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1959 format
, type
, data
, "glClearNamedBufferData", false);
1964 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1965 GLintptr offset
, GLsizeiptr size
,
1966 GLenum format
, GLenum type
,
1969 GET_CURRENT_CONTEXT(ctx
);
1970 struct gl_buffer_object
*bufObj
;
1972 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1976 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1977 format
, type
, data
, "glClearBufferSubData", true);
1981 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1982 GLintptr offset
, GLsizeiptr size
,
1983 GLenum format
, GLenum type
,
1986 GET_CURRENT_CONTEXT(ctx
);
1987 struct gl_buffer_object
*bufObj
;
1989 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1990 "glClearNamedBufferSubData");
1994 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
1995 type
, data
, "glClearNamedBufferSubData", true);
1999 unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
2001 GLboolean status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2002 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2003 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2004 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2005 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2011 validate_and_unmap_buffer(struct gl_context
*ctx
,
2012 struct gl_buffer_object
*bufObj
,
2015 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2017 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2018 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2019 "%s(buffer is not mapped)", func
);
2024 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2025 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2027 /* check that last 100 bytes are still = magic value */
2028 for (i
= 0; i
< 100; i
++) {
2029 GLuint pos
= bufObj
->Size
- i
- 1;
2030 if (buf
[pos
] != 123) {
2031 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2032 " at position %d (value = %u)\n",
2040 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2041 GLuint i
, unchanged
= 0;
2042 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2044 /* check which bytes changed */
2045 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2046 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2053 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2054 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2059 return unmap_buffer(ctx
, bufObj
);
2062 GLboolean GLAPIENTRY
2063 _mesa_UnmapBuffer_no_error(GLenum target
)
2065 GET_CURRENT_CONTEXT(ctx
);
2066 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2067 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2069 return unmap_buffer(ctx
, bufObj
);
2072 GLboolean GLAPIENTRY
2073 _mesa_UnmapBuffer(GLenum target
)
2075 GET_CURRENT_CONTEXT(ctx
);
2076 struct gl_buffer_object
*bufObj
;
2078 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2082 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2085 GLboolean GLAPIENTRY
2086 _mesa_UnmapNamedBuffer_no_error(GLuint buffer
)
2088 GET_CURRENT_CONTEXT(ctx
);
2089 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2091 return unmap_buffer(ctx
, bufObj
);
2094 GLboolean GLAPIENTRY
2095 _mesa_UnmapNamedBuffer(GLuint buffer
)
2097 GET_CURRENT_CONTEXT(ctx
);
2098 struct gl_buffer_object
*bufObj
;
2100 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2104 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2109 get_buffer_parameter(struct gl_context
*ctx
,
2110 struct gl_buffer_object
*bufObj
, GLenum pname
,
2111 GLint64
*params
, const char *func
)
2114 case GL_BUFFER_SIZE_ARB
:
2115 *params
= bufObj
->Size
;
2117 case GL_BUFFER_USAGE_ARB
:
2118 *params
= bufObj
->Usage
;
2120 case GL_BUFFER_ACCESS_ARB
:
2121 *params
= simplified_access_mode(ctx
,
2122 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2124 case GL_BUFFER_MAPPED_ARB
:
2125 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2127 case GL_BUFFER_ACCESS_FLAGS
:
2128 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2130 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2132 case GL_BUFFER_MAP_OFFSET
:
2133 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2135 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2137 case GL_BUFFER_MAP_LENGTH
:
2138 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2140 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2142 case GL_BUFFER_IMMUTABLE_STORAGE
:
2143 if (!ctx
->Extensions
.ARB_buffer_storage
)
2145 *params
= bufObj
->Immutable
;
2147 case GL_BUFFER_STORAGE_FLAGS
:
2148 if (!ctx
->Extensions
.ARB_buffer_storage
)
2150 *params
= bufObj
->StorageFlags
;
2159 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2160 _mesa_enum_to_string(pname
));
2165 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2167 GET_CURRENT_CONTEXT(ctx
);
2168 struct gl_buffer_object
*bufObj
;
2171 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2172 GL_INVALID_OPERATION
);
2176 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2177 "glGetBufferParameteriv"))
2178 return; /* Error already recorded. */
2180 *params
= (GLint
) parameter
;
2184 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2186 GET_CURRENT_CONTEXT(ctx
);
2187 struct gl_buffer_object
*bufObj
;
2190 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2191 GL_INVALID_OPERATION
);
2195 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2196 "glGetBufferParameteri64v"))
2197 return; /* Error already recorded. */
2199 *params
= parameter
;
2203 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2205 GET_CURRENT_CONTEXT(ctx
);
2206 struct gl_buffer_object
*bufObj
;
2209 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2210 "glGetNamedBufferParameteriv");
2214 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2215 "glGetNamedBufferParameteriv"))
2216 return; /* Error already recorded. */
2218 *params
= (GLint
) parameter
;
2222 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2225 GET_CURRENT_CONTEXT(ctx
);
2226 struct gl_buffer_object
*bufObj
;
2229 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2230 "glGetNamedBufferParameteri64v");
2234 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2235 "glGetNamedBufferParameteri64v"))
2236 return; /* Error already recorded. */
2238 *params
= parameter
;
2243 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2245 GET_CURRENT_CONTEXT(ctx
);
2246 struct gl_buffer_object
*bufObj
;
2248 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2249 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2250 "GL_BUFFER_MAP_POINTER)");
2254 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2255 GL_INVALID_OPERATION
);
2259 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2263 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2265 GET_CURRENT_CONTEXT(ctx
);
2266 struct gl_buffer_object
*bufObj
;
2268 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2269 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2270 "GL_BUFFER_MAP_POINTER)");
2274 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2275 "glGetNamedBufferPointerv");
2279 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2284 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2285 struct gl_buffer_object
*src
,
2286 struct gl_buffer_object
*dst
,
2287 GLintptr readOffset
, GLintptr writeOffset
,
2288 GLsizeiptr size
, const char *func
)
2290 if (_mesa_check_disallowed_mapping(src
)) {
2291 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2292 "%s(readBuffer is mapped)", func
);
2296 if (_mesa_check_disallowed_mapping(dst
)) {
2297 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2298 "%s(writeBuffer is mapped)", func
);
2302 if (readOffset
< 0) {
2303 _mesa_error(ctx
, GL_INVALID_VALUE
,
2304 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2308 if (writeOffset
< 0) {
2309 _mesa_error(ctx
, GL_INVALID_VALUE
,
2310 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2315 _mesa_error(ctx
, GL_INVALID_VALUE
,
2316 "%s(size %d < 0)", func
, (int) size
);
2320 if (readOffset
+ size
> src
->Size
) {
2321 _mesa_error(ctx
, GL_INVALID_VALUE
,
2322 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2323 (int) readOffset
, (int) size
, (int) src
->Size
);
2327 if (writeOffset
+ size
> dst
->Size
) {
2328 _mesa_error(ctx
, GL_INVALID_VALUE
,
2329 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2330 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2335 if (readOffset
+ size
<= writeOffset
) {
2338 else if (writeOffset
+ size
<= readOffset
) {
2342 /* overlapping src/dst is illegal */
2343 _mesa_error(ctx
, GL_INVALID_VALUE
,
2344 "%s(overlapping src/dst)", func
);
2349 dst
->MinMaxCacheDirty
= true;
2351 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2355 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2356 GLintptr readOffset
, GLintptr writeOffset
,
2359 GET_CURRENT_CONTEXT(ctx
);
2360 struct gl_buffer_object
*src
, *dst
;
2362 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2363 GL_INVALID_OPERATION
);
2367 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2368 GL_INVALID_OPERATION
);
2372 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2373 "glCopyBufferSubData");
2377 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2378 GLintptr readOffset
, GLintptr writeOffset
,
2381 GET_CURRENT_CONTEXT(ctx
);
2382 struct gl_buffer_object
*src
, *dst
;
2384 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2385 "glCopyNamedBufferSubData");
2389 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2390 "glCopyNamedBufferSubData");
2394 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2395 "glCopyNamedBufferSubData");
2399 validate_map_buffer_range(struct gl_context
*ctx
,
2400 struct gl_buffer_object
*bufObj
, GLintptr offset
,
2401 GLsizeiptr length
, GLbitfield access
,
2404 GLbitfield allowed_access
;
2406 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, false);
2409 _mesa_error(ctx
, GL_INVALID_VALUE
,
2410 "%s(offset %ld < 0)", func
, (long) offset
);
2415 _mesa_error(ctx
, GL_INVALID_VALUE
,
2416 "%s(length %ld < 0)", func
, (long) length
);
2420 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2422 * "An INVALID_OPERATION error is generated for any of the following
2425 * * <length> is zero."
2427 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2428 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2432 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2436 allowed_access
= GL_MAP_READ_BIT
|
2438 GL_MAP_INVALIDATE_RANGE_BIT
|
2439 GL_MAP_INVALIDATE_BUFFER_BIT
|
2440 GL_MAP_FLUSH_EXPLICIT_BIT
|
2441 GL_MAP_UNSYNCHRONIZED_BIT
;
2443 if (ctx
->Extensions
.ARB_buffer_storage
) {
2444 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2445 GL_MAP_COHERENT_BIT
;
2448 if (access
& ~allowed_access
) {
2449 /* generate an error if any bits other than those allowed are set */
2450 _mesa_error(ctx
, GL_INVALID_VALUE
,
2451 "%s(access has undefined bits set)", func
);
2455 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2456 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2457 "%s(access indicates neither read or write)", func
);
2461 if ((access
& GL_MAP_READ_BIT
) &&
2462 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2463 GL_MAP_INVALIDATE_BUFFER_BIT
|
2464 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2465 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2466 "%s(read access with disallowed bits)", func
);
2470 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2471 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2472 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2473 "%s(access has flush explicit without write)", func
);
2477 if (access
& GL_MAP_READ_BIT
&&
2478 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2479 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2480 "%s(buffer does not allow read access)", func
);
2484 if (access
& GL_MAP_WRITE_BIT
&&
2485 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2486 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2487 "%s(buffer does not allow write access)", func
);
2491 if (access
& GL_MAP_COHERENT_BIT
&&
2492 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2493 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2494 "%s(buffer does not allow coherent access)", func
);
2498 if (access
& GL_MAP_PERSISTENT_BIT
&&
2499 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2500 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2501 "%s(buffer does not allow persistent access)", func
);
2505 if (offset
+ length
> bufObj
->Size
) {
2506 _mesa_error(ctx
, GL_INVALID_VALUE
,
2507 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
2508 (unsigned long) offset
, (unsigned long) length
,
2509 (unsigned long) bufObj
->Size
);
2513 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2514 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2515 "%s(buffer already mapped)", func
);
2519 if (access
& GL_MAP_WRITE_BIT
) {
2520 bufObj
->NumMapBufferWriteCalls
++;
2521 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2522 bufObj
->Usage
== GL_STATIC_COPY
) &&
2523 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
2524 BUFFER_USAGE_WARNING(ctx
,
2525 "using %s(buffer %u, offset %u, length %u) to "
2526 "update a %s buffer",
2527 func
, bufObj
->Name
, offset
, length
,
2528 _mesa_enum_to_string(bufObj
->Usage
));
2536 map_buffer_range(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2537 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
2540 if (!bufObj
->Size
) {
2541 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2545 assert(ctx
->Driver
.MapBufferRange
);
2546 void *map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2549 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2552 /* The driver callback should have set all these fields.
2553 * This is important because other modules (like VBO) might call
2554 * the driver function directly.
2556 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2557 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2558 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2559 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2562 if (access
& GL_MAP_WRITE_BIT
) {
2563 bufObj
->Written
= GL_TRUE
;
2564 bufObj
->MinMaxCacheDirty
= true;
2568 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2569 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2570 bufObj
->Name
, bufObj
->Size
, access
);
2571 /* Access must be write only */
2572 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2574 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2575 for (i
= 0; i
< bufObj
->Size
; i
++)
2582 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2583 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2585 /* buffer is 100 bytes larger than requested, fill with magic value */
2586 for (i
= 0; i
< 100; i
++) {
2587 buf
[bufObj
->Size
- i
- 1] = 123;
2596 _mesa_MapBufferRange_no_error(GLenum target
, GLintptr offset
,
2597 GLsizeiptr length
, GLbitfield access
)
2599 GET_CURRENT_CONTEXT(ctx
);
2601 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2602 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2604 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2605 "glMapBufferRange");
2609 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2612 GET_CURRENT_CONTEXT(ctx
);
2613 struct gl_buffer_object
*bufObj
;
2615 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2616 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2617 "glMapBufferRange(ARB_map_buffer_range not supported)");
2621 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2625 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2626 "glMapBufferRange"))
2629 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2630 "glMapBufferRange");
2634 _mesa_MapNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
2635 GLsizeiptr length
, GLbitfield access
)
2637 GET_CURRENT_CONTEXT(ctx
);
2638 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2640 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2641 "glMapNamedBufferRange");
2645 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2648 GET_CURRENT_CONTEXT(ctx
);
2649 struct gl_buffer_object
*bufObj
;
2651 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2652 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2653 "glMapNamedBufferRange("
2654 "ARB_map_buffer_range not supported)");
2658 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2662 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2663 "glMapNamedBufferRange"))
2666 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2667 "glMapNamedBufferRange");
2671 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2672 * flags for input to map_buffer_range.
2674 * \return true if the type of requested access is permissible.
2677 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2681 case GL_READ_ONLY_ARB
:
2682 *flags
= GL_MAP_READ_BIT
;
2683 return _mesa_is_desktop_gl(ctx
);
2684 case GL_WRITE_ONLY_ARB
:
2685 *flags
= GL_MAP_WRITE_BIT
;
2687 case GL_READ_WRITE_ARB
:
2688 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2689 return _mesa_is_desktop_gl(ctx
);
2696 _mesa_MapBuffer_no_error(GLenum target
, GLenum access
)
2698 GET_CURRENT_CONTEXT(ctx
);
2700 GLbitfield accessFlags
;
2701 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
2703 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2704 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2706 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2711 _mesa_MapBuffer(GLenum target
, GLenum access
)
2713 GET_CURRENT_CONTEXT(ctx
);
2714 struct gl_buffer_object
*bufObj
;
2715 GLbitfield accessFlags
;
2717 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2718 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2722 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2726 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2730 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2735 _mesa_MapNamedBuffer_no_error(GLuint buffer
, GLenum access
)
2737 GET_CURRENT_CONTEXT(ctx
);
2739 GLbitfield accessFlags
;
2740 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
2742 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2744 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2745 "glMapNamedBuffer");
2749 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2751 GET_CURRENT_CONTEXT(ctx
);
2752 struct gl_buffer_object
*bufObj
;
2753 GLbitfield accessFlags
;
2755 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2756 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2760 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2764 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2765 "glMapNamedBuffer"))
2768 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2769 "glMapNamedBuffer");
2774 flush_mapped_buffer_range(struct gl_context
*ctx
,
2775 struct gl_buffer_object
*bufObj
,
2776 GLintptr offset
, GLsizeiptr length
,
2779 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2780 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2781 "%s(ARB_map_buffer_range not supported)", func
);
2786 _mesa_error(ctx
, GL_INVALID_VALUE
,
2787 "%s(offset %ld < 0)", func
, (long) offset
);
2792 _mesa_error(ctx
, GL_INVALID_VALUE
,
2793 "%s(length %ld < 0)", func
, (long) length
);
2797 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2798 /* buffer is not mapped */
2799 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2800 "%s(buffer is not mapped)", func
);
2804 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2805 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2806 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2807 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2811 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2812 _mesa_error(ctx
, GL_INVALID_VALUE
,
2813 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2814 (long) offset
, (long) length
,
2815 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2819 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2821 if (ctx
->Driver
.FlushMappedBufferRange
)
2822 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2827 _mesa_FlushMappedBufferRange_no_error(GLenum target
, GLintptr offset
,
2830 GET_CURRENT_CONTEXT(ctx
);
2831 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2832 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2834 if (ctx
->Driver
.FlushMappedBufferRange
)
2835 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2840 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2843 GET_CURRENT_CONTEXT(ctx
);
2844 struct gl_buffer_object
*bufObj
;
2846 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2847 GL_INVALID_OPERATION
);
2851 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2852 "glFlushMappedBufferRange");
2856 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
2859 GET_CURRENT_CONTEXT(ctx
);
2860 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2862 if (ctx
->Driver
.FlushMappedBufferRange
)
2863 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2868 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2871 GET_CURRENT_CONTEXT(ctx
);
2872 struct gl_buffer_object
*bufObj
;
2874 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2875 "glFlushMappedNamedBufferRange");
2879 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2880 "glFlushMappedNamedBufferRange");
2885 * Binds a buffer object to a uniform buffer binding point.
2887 * The caller is responsible for flushing vertices and updating
2891 set_ubo_binding(struct gl_context
*ctx
,
2892 struct gl_uniform_buffer_binding
*binding
,
2893 struct gl_buffer_object
*bufObj
,
2898 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2900 binding
->Offset
= offset
;
2901 binding
->Size
= size
;
2902 binding
->AutomaticSize
= autoSize
;
2904 /* If this is a real buffer object, mark it has having been used
2905 * at some point as a UBO.
2908 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2912 * Binds a buffer object to a shader storage buffer binding point.
2914 * The caller is responsible for flushing vertices and updating
2918 set_ssbo_binding(struct gl_context
*ctx
,
2919 struct gl_shader_storage_buffer_binding
*binding
,
2920 struct gl_buffer_object
*bufObj
,
2925 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2927 binding
->Offset
= offset
;
2928 binding
->Size
= size
;
2929 binding
->AutomaticSize
= autoSize
;
2931 /* If this is a real buffer object, mark it has having been used
2932 * at some point as a SSBO.
2935 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
2939 * Binds a buffer object to a uniform buffer binding point.
2941 * Unlike set_ubo_binding(), this function also flushes vertices
2942 * and updates NewDriverState. It also checks if the binding
2943 * has actually changed before updating it.
2946 bind_uniform_buffer(struct gl_context
*ctx
,
2948 struct gl_buffer_object
*bufObj
,
2953 struct gl_uniform_buffer_binding
*binding
=
2954 &ctx
->UniformBufferBindings
[index
];
2956 if (binding
->BufferObject
== bufObj
&&
2957 binding
->Offset
== offset
&&
2958 binding
->Size
== size
&&
2959 binding
->AutomaticSize
== autoSize
) {
2963 FLUSH_VERTICES(ctx
, 0);
2964 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2966 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2970 * Binds a buffer object to a shader storage buffer binding point.
2972 * Unlike set_ssbo_binding(), this function also flushes vertices
2973 * and updates NewDriverState. It also checks if the binding
2974 * has actually changed before updating it.
2977 bind_shader_storage_buffer(struct gl_context
*ctx
,
2979 struct gl_buffer_object
*bufObj
,
2984 struct gl_shader_storage_buffer_binding
*binding
=
2985 &ctx
->ShaderStorageBufferBindings
[index
];
2987 if (binding
->BufferObject
== bufObj
&&
2988 binding
->Offset
== offset
&&
2989 binding
->Size
== size
&&
2990 binding
->AutomaticSize
== autoSize
) {
2994 FLUSH_VERTICES(ctx
, 0);
2995 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
2997 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
3001 * Bind a region of a buffer object to a uniform block binding point.
3002 * \param index the uniform buffer binding point index
3003 * \param bufObj the buffer object
3004 * \param offset offset to the start of buffer object region
3005 * \param size size of the buffer object region
3008 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
3010 struct gl_buffer_object
*bufObj
,
3014 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3015 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3019 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3020 _mesa_error(ctx
, GL_INVALID_VALUE
,
3021 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3022 ctx
->Const
.UniformBufferOffsetAlignment
);
3026 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3031 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3032 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3036 * Bind a region of a buffer object to a shader storage block binding point.
3037 * \param index the shader storage buffer binding point index
3038 * \param bufObj the buffer object
3039 * \param offset offset to the start of buffer object region
3040 * \param size size of the buffer object region
3043 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3045 struct gl_buffer_object
*bufObj
,
3049 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3050 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3054 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3055 _mesa_error(ctx
, GL_INVALID_VALUE
,
3056 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3057 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3061 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3066 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3067 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3071 * Bind a buffer object to a uniform block binding point.
3072 * As above, but offset = 0.
3075 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
3077 struct gl_buffer_object
*bufObj
)
3079 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3080 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
3084 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3086 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3087 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
3089 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
3093 * Bind a buffer object to a shader storage block binding point.
3094 * As above, but offset = 0.
3097 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
3099 struct gl_buffer_object
*bufObj
)
3101 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3102 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
3106 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3108 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3109 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
3111 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
3115 * Binds a buffer object to an atomic buffer binding point.
3117 * The caller is responsible for validating the offset,
3118 * flushing the vertices and updating NewDriverState.
3121 set_atomic_buffer_binding(struct gl_context
*ctx
,
3122 struct gl_atomic_buffer_binding
*binding
,
3123 struct gl_buffer_object
*bufObj
,
3127 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
3129 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3130 binding
->Offset
= 0;
3133 binding
->Offset
= offset
;
3134 binding
->Size
= size
;
3135 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
3140 * Binds a buffer object to an atomic buffer binding point.
3142 * Unlike set_atomic_buffer_binding(), this function also validates the
3143 * index and offset, flushes vertices, and updates NewDriverState.
3144 * It also checks if the binding has actually changing before
3148 bind_atomic_buffer(struct gl_context
*ctx
,
3150 struct gl_buffer_object
*bufObj
,
3155 struct gl_atomic_buffer_binding
*binding
;
3157 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3158 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3162 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3163 _mesa_error(ctx
, GL_INVALID_VALUE
,
3164 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3165 ATOMIC_COUNTER_SIZE
);
3169 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3171 binding
= &ctx
->AtomicBufferBindings
[index
];
3172 if (binding
->BufferObject
== bufObj
&&
3173 binding
->Offset
== offset
&&
3174 binding
->Size
== size
) {
3178 FLUSH_VERTICES(ctx
, 0);
3179 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3181 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3185 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3187 const GLintptr
*offsets
,
3188 const GLsizeiptr
*sizes
)
3190 if (offsets
[index
] < 0) {
3191 /* The ARB_multi_bind spec says:
3193 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3194 * value in <offsets> is less than zero (per binding)."
3196 _mesa_error(ctx
, GL_INVALID_VALUE
,
3197 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3198 index
, (int64_t) offsets
[index
]);
3202 if (sizes
[index
] <= 0) {
3203 /* The ARB_multi_bind spec says:
3205 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3206 * value in <sizes> is less than or equal to zero (per binding)."
3208 _mesa_error(ctx
, GL_INVALID_VALUE
,
3209 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3210 index
, (int64_t) sizes
[index
]);
3218 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3219 GLuint first
, GLsizei count
,
3222 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3223 _mesa_error(ctx
, GL_INVALID_ENUM
,
3224 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3228 /* The ARB_multi_bind_spec says:
3230 * "An INVALID_OPERATION error is generated if <first> + <count> is
3231 * greater than the number of target-specific indexed binding points,
3232 * as described in section 6.7.1."
3234 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3235 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3236 "%s(first=%u + count=%d > the value of "
3237 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3238 caller
, first
, count
,
3239 ctx
->Const
.MaxUniformBufferBindings
);
3247 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3248 GLuint first
, GLsizei count
,
3251 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3252 _mesa_error(ctx
, GL_INVALID_ENUM
,
3253 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3257 /* The ARB_multi_bind_spec says:
3259 * "An INVALID_OPERATION error is generated if <first> + <count> is
3260 * greater than the number of target-specific indexed binding points,
3261 * as described in section 6.7.1."
3263 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3264 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3265 "%s(first=%u + count=%d > the value of "
3266 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3267 caller
, first
, count
,
3268 ctx
->Const
.MaxShaderStorageBufferBindings
);
3276 * Unbind all uniform buffers in the range
3277 * <first> through <first>+<count>-1
3280 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3282 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3285 for (i
= 0; i
< count
; i
++)
3286 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3287 bufObj
, -1, -1, GL_TRUE
);
3291 * Unbind all shader storage buffers in the range
3292 * <first> through <first>+<count>-1
3295 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3298 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3301 for (i
= 0; i
< count
; i
++)
3302 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3303 bufObj
, -1, -1, GL_TRUE
);
3307 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3308 const GLuint
*buffers
,
3310 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3315 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3318 /* Assume that at least one binding will be changed */
3319 FLUSH_VERTICES(ctx
, 0);
3320 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3323 /* The ARB_multi_bind spec says:
3325 * "If <buffers> is NULL, all bindings from <first> through
3326 * <first>+<count>-1 are reset to their unbound (zero) state.
3327 * In this case, the offsets and sizes associated with the
3328 * binding points are set to default values, ignoring
3329 * <offsets> and <sizes>."
3331 unbind_uniform_buffers(ctx
, first
, count
);
3335 /* Note that the error semantics for multi-bind commands differ from
3336 * those of other GL commands.
3338 * The Issues section in the ARB_multi_bind spec says:
3340 * "(11) Typically, OpenGL specifies that if an error is generated by a
3341 * command, that command has no effect. This is somewhat
3342 * unfortunate for multi-bind commands, because it would require a
3343 * first pass to scan the entire list of bound objects for errors
3344 * and then a second pass to actually perform the bindings.
3345 * Should we have different error semantics?
3347 * RESOLVED: Yes. In this specification, when the parameters for
3348 * one of the <count> binding points are invalid, that binding point
3349 * is not updated and an error will be generated. However, other
3350 * binding points in the same command will be updated if their
3351 * parameters are valid and no other error occurs."
3354 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3356 for (i
= 0; i
< count
; i
++) {
3357 struct gl_uniform_buffer_binding
*binding
=
3358 &ctx
->UniformBufferBindings
[first
+ i
];
3359 struct gl_buffer_object
*bufObj
;
3360 GLintptr offset
= 0;
3361 GLsizeiptr size
= 0;
3364 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3367 /* The ARB_multi_bind spec says:
3369 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3370 * pair of values in <offsets> and <sizes> does not respectively
3371 * satisfy the constraints described for those parameters for the
3372 * specified target, as described in section 6.7.1 (per binding)."
3374 * Section 6.7.1 refers to table 6.5, which says:
3376 * "┌───────────────────────────────────────────────────────────────┐
3377 * │ Uniform buffer array bindings (see sec. 7.6) │
3378 * ├─────────────────────┬─────────────────────────────────────────┤
3380 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3381 * │ │ OFFSET_ALIGNMENT │
3383 * │ size restriction │ none │
3384 * └─────────────────────┴─────────────────────────────────────────┘"
3386 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3387 _mesa_error(ctx
, GL_INVALID_VALUE
,
3388 "glBindBuffersRange(offsets[%u]=%" PRId64
3389 " is misaligned; it must be a multiple of the value of "
3390 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3391 "target=GL_UNIFORM_BUFFER)",
3392 i
, (int64_t) offsets
[i
],
3393 ctx
->Const
.UniformBufferOffsetAlignment
);
3397 offset
= offsets
[i
];
3401 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3402 bufObj
= binding
->BufferObject
;
3404 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3407 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3408 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3410 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3414 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3418 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3419 GLsizei count
, const GLuint
*buffers
,
3421 const GLintptr
*offsets
,
3422 const GLsizeiptr
*sizes
,
3427 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3430 /* Assume that at least one binding will be changed */
3431 FLUSH_VERTICES(ctx
, 0);
3432 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3435 /* The ARB_multi_bind spec says:
3437 * "If <buffers> is NULL, all bindings from <first> through
3438 * <first>+<count>-1 are reset to their unbound (zero) state.
3439 * In this case, the offsets and sizes associated with the
3440 * binding points are set to default values, ignoring
3441 * <offsets> and <sizes>."
3443 unbind_shader_storage_buffers(ctx
, first
, count
);
3447 /* Note that the error semantics for multi-bind commands differ from
3448 * those of other GL commands.
3450 * The Issues section in the ARB_multi_bind spec says:
3452 * "(11) Typically, OpenGL specifies that if an error is generated by a
3453 * command, that command has no effect. This is somewhat
3454 * unfortunate for multi-bind commands, because it would require a
3455 * first pass to scan the entire list of bound objects for errors
3456 * and then a second pass to actually perform the bindings.
3457 * Should we have different error semantics?
3459 * RESOLVED: Yes. In this specification, when the parameters for
3460 * one of the <count> binding points are invalid, that binding point
3461 * is not updated and an error will be generated. However, other
3462 * binding points in the same command will be updated if their
3463 * parameters are valid and no other error occurs."
3466 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3468 for (i
= 0; i
< count
; i
++) {
3469 struct gl_shader_storage_buffer_binding
*binding
=
3470 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3471 struct gl_buffer_object
*bufObj
;
3472 GLintptr offset
= 0;
3473 GLsizeiptr size
= 0;
3476 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3479 /* The ARB_multi_bind spec says:
3481 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3482 * pair of values in <offsets> and <sizes> does not respectively
3483 * satisfy the constraints described for those parameters for the
3484 * specified target, as described in section 6.7.1 (per binding)."
3486 * Section 6.7.1 refers to table 6.5, which says:
3488 * "┌───────────────────────────────────────────────────────────────┐
3489 * │ Shader storage buffer array bindings (see sec. 7.8) │
3490 * ├─────────────────────┬─────────────────────────────────────────┤
3492 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3493 * │ │ BUFFER_OFFSET_ALIGNMENT │
3495 * │ size restriction │ none │
3496 * └─────────────────────┴─────────────────────────────────────────┘"
3498 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3499 _mesa_error(ctx
, GL_INVALID_VALUE
,
3500 "glBindBuffersRange(offsets[%u]=%" PRId64
3501 " is misaligned; it must be a multiple of the value of "
3502 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3503 "target=GL_SHADER_STORAGE_BUFFER)",
3504 i
, (int64_t) offsets
[i
],
3505 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3509 offset
= offsets
[i
];
3513 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3514 bufObj
= binding
->BufferObject
;
3516 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3519 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3520 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3522 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3526 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3530 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3531 struct gl_transform_feedback_object
*tfObj
,
3532 GLuint first
, GLsizei count
, const char *caller
)
3534 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3535 _mesa_error(ctx
, GL_INVALID_ENUM
,
3536 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3540 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3542 * "An INVALID_OPERATION error is generated :
3545 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3546 * FEEDBACK_BUFFER and transform feedback is currently active."
3548 * We assume that this is also meant to apply to BindBuffersRange
3549 * and BindBuffersBase.
3551 if (tfObj
->Active
) {
3552 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3553 "%s(Changing transform feedback buffers while "
3554 "transform feedback is active)", caller
);
3558 /* The ARB_multi_bind_spec says:
3560 * "An INVALID_OPERATION error is generated if <first> + <count> is
3561 * greater than the number of target-specific indexed binding points,
3562 * as described in section 6.7.1."
3564 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3565 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3566 "%s(first=%u + count=%d > the value of "
3567 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3568 caller
, first
, count
,
3569 ctx
->Const
.MaxTransformFeedbackBuffers
);
3577 * Unbind all transform feedback buffers in the range
3578 * <first> through <first>+<count>-1
3581 unbind_xfb_buffers(struct gl_context
*ctx
,
3582 struct gl_transform_feedback_object
*tfObj
,
3583 GLuint first
, GLsizei count
)
3585 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3588 for (i
= 0; i
< count
; i
++)
3589 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3594 bind_xfb_buffers(struct gl_context
*ctx
,
3595 GLuint first
, GLsizei count
,
3596 const GLuint
*buffers
,
3598 const GLintptr
*offsets
,
3599 const GLsizeiptr
*sizes
,
3602 struct gl_transform_feedback_object
*tfObj
=
3603 ctx
->TransformFeedback
.CurrentObject
;
3606 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
3609 /* Assume that at least one binding will be changed */
3610 FLUSH_VERTICES(ctx
, 0);
3611 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3614 /* The ARB_multi_bind spec says:
3616 * "If <buffers> is NULL, all bindings from <first> through
3617 * <first>+<count>-1 are reset to their unbound (zero) state.
3618 * In this case, the offsets and sizes associated with the
3619 * binding points are set to default values, ignoring
3620 * <offsets> and <sizes>."
3622 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3626 /* Note that the error semantics for multi-bind commands differ from
3627 * those of other GL commands.
3629 * The Issues section in the ARB_multi_bind spec says:
3631 * "(11) Typically, OpenGL specifies that if an error is generated by a
3632 * command, that command has no effect. This is somewhat
3633 * unfortunate for multi-bind commands, because it would require a
3634 * first pass to scan the entire list of bound objects for errors
3635 * and then a second pass to actually perform the bindings.
3636 * Should we have different error semantics?
3638 * RESOLVED: Yes. In this specification, when the parameters for
3639 * one of the <count> binding points are invalid, that binding point
3640 * is not updated and an error will be generated. However, other
3641 * binding points in the same command will be updated if their
3642 * parameters are valid and no other error occurs."
3645 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3647 for (i
= 0; i
< count
; i
++) {
3648 const GLuint index
= first
+ i
;
3649 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3650 struct gl_buffer_object
*bufObj
;
3651 GLintptr offset
= 0;
3652 GLsizeiptr size
= 0;
3655 offset
= offsets
[i
];
3658 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3661 /* The ARB_multi_bind spec says:
3663 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3664 * pair of values in <offsets> and <sizes> does not respectively
3665 * satisfy the constraints described for those parameters for the
3666 * specified target, as described in section 6.7.1 (per binding)."
3668 * Section 6.7.1 refers to table 6.5, which says:
3670 * "┌───────────────────────────────────────────────────────────────┐
3671 * │ Transform feedback array bindings (see sec. 13.2.2) │
3672 * ├───────────────────────┬───────────────────────────────────────┤
3674 * │ offset restriction │ multiple of 4 │
3676 * │ size restriction │ multiple of 4 │
3677 * └───────────────────────┴───────────────────────────────────────┘"
3679 if (offsets
[i
] & 0x3) {
3680 _mesa_error(ctx
, GL_INVALID_VALUE
,
3681 "glBindBuffersRange(offsets[%u]=%" PRId64
3682 " is misaligned; it must be a multiple of 4 when "
3683 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3684 i
, (int64_t) offsets
[i
]);
3688 if (sizes
[i
] & 0x3) {
3689 _mesa_error(ctx
, GL_INVALID_VALUE
,
3690 "glBindBuffersRange(sizes[%u]=%" PRId64
3691 " is misaligned; it must be a multiple of 4 when "
3692 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3693 i
, (int64_t) sizes
[i
]);
3697 offset
= offsets
[i
];
3701 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3702 bufObj
= boundBufObj
;
3704 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3707 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3711 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3715 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3716 GLuint first
, GLsizei count
,
3719 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3720 _mesa_error(ctx
, GL_INVALID_ENUM
,
3721 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3725 /* The ARB_multi_bind_spec says:
3727 * "An INVALID_OPERATION error is generated if <first> + <count> is
3728 * greater than the number of target-specific indexed binding points,
3729 * as described in section 6.7.1."
3731 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3732 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3733 "%s(first=%u + count=%d > the value of "
3734 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3735 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3743 * Unbind all atomic counter buffers in the range
3744 * <first> through <first>+<count>-1
3747 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3749 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3752 for (i
= 0; i
< count
; i
++)
3753 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3758 bind_atomic_buffers(struct gl_context
*ctx
,
3761 const GLuint
*buffers
,
3763 const GLintptr
*offsets
,
3764 const GLsizeiptr
*sizes
,
3769 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
3772 /* Assume that at least one binding will be changed */
3773 FLUSH_VERTICES(ctx
, 0);
3774 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3777 /* The ARB_multi_bind spec says:
3779 * "If <buffers> is NULL, all bindings from <first> through
3780 * <first>+<count>-1 are reset to their unbound (zero) state.
3781 * In this case, the offsets and sizes associated with the
3782 * binding points are set to default values, ignoring
3783 * <offsets> and <sizes>."
3785 unbind_atomic_buffers(ctx
, first
, count
);
3789 /* Note that the error semantics for multi-bind commands differ from
3790 * those of other GL commands.
3792 * The Issues section in the ARB_multi_bind spec says:
3794 * "(11) Typically, OpenGL specifies that if an error is generated by a
3795 * command, that command has no effect. This is somewhat
3796 * unfortunate for multi-bind commands, because it would require a
3797 * first pass to scan the entire list of bound objects for errors
3798 * and then a second pass to actually perform the bindings.
3799 * Should we have different error semantics?
3801 * RESOLVED: Yes. In this specification, when the parameters for
3802 * one of the <count> binding points are invalid, that binding point
3803 * is not updated and an error will be generated. However, other
3804 * binding points in the same command will be updated if their
3805 * parameters are valid and no other error occurs."
3808 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3810 for (i
= 0; i
< count
; i
++) {
3811 struct gl_atomic_buffer_binding
*binding
=
3812 &ctx
->AtomicBufferBindings
[first
+ i
];
3813 struct gl_buffer_object
*bufObj
;
3814 GLintptr offset
= 0;
3815 GLsizeiptr size
= 0;
3818 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3821 /* The ARB_multi_bind spec says:
3823 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3824 * pair of values in <offsets> and <sizes> does not respectively
3825 * satisfy the constraints described for those parameters for the
3826 * specified target, as described in section 6.7.1 (per binding)."
3828 * Section 6.7.1 refers to table 6.5, which says:
3830 * "┌───────────────────────────────────────────────────────────────┐
3831 * │ Atomic counter array bindings (see sec. 7.7.2) │
3832 * ├───────────────────────┬───────────────────────────────────────┤
3834 * │ offset restriction │ multiple of 4 │
3836 * │ size restriction │ none │
3837 * └───────────────────────┴───────────────────────────────────────┘"
3839 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3840 _mesa_error(ctx
, GL_INVALID_VALUE
,
3841 "glBindBuffersRange(offsets[%u]=%" PRId64
3842 " is misaligned; it must be a multiple of %d when "
3843 "target=GL_ATOMIC_COUNTER_BUFFER)",
3844 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3848 offset
= offsets
[i
];
3852 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3853 bufObj
= binding
->BufferObject
;
3855 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3858 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3861 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3865 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3866 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3868 GET_CURRENT_CONTEXT(ctx
);
3869 struct gl_buffer_object
*bufObj
;
3871 if (MESA_VERBOSE
& VERBOSE_API
) {
3872 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
3873 _mesa_enum_to_string(target
), index
, buffer
,
3874 (unsigned long) offset
, (unsigned long) size
);
3878 bufObj
= ctx
->Shared
->NullBufferObj
;
3880 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3882 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3883 &bufObj
, "glBindBufferRange"))
3887 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3888 "glBindBufferRange(invalid buffer=%u)", buffer
);
3894 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3901 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3902 _mesa_bind_buffer_range_transform_feedback(ctx
,
3903 ctx
->TransformFeedback
.CurrentObject
,
3904 index
, bufObj
, offset
, size
,
3907 case GL_UNIFORM_BUFFER
:
3908 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3910 case GL_SHADER_STORAGE_BUFFER
:
3911 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3913 case GL_ATOMIC_COUNTER_BUFFER
:
3914 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3915 "glBindBufferRange");
3918 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3924 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3926 GET_CURRENT_CONTEXT(ctx
);
3927 struct gl_buffer_object
*bufObj
;
3929 if (MESA_VERBOSE
& VERBOSE_API
) {
3930 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
3931 _mesa_enum_to_string(target
), index
, buffer
);
3935 bufObj
= ctx
->Shared
->NullBufferObj
;
3937 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3939 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3940 &bufObj
, "glBindBufferBase"))
3944 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3945 "glBindBufferBase(invalid buffer=%u)", buffer
);
3949 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3950 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3952 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3953 * zero and size equal to the size of buffer."
3955 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3957 * "If the parameter (starting offset or size) was not specified when the
3958 * buffer object was bound, zero is returned."
3960 * What happens if the size of the buffer changes? Does the size of the
3961 * buffer at the moment glBindBufferBase was called still play a role, like
3962 * the first quote would imply, or is the size meaningless in the
3963 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3964 * core spec page 45 says:
3966 * "It is equivalent to calling BindBufferRange with offset zero, while
3967 * size is determined by the size of the bound buffer at the time the
3970 * My interpretation is that the GL 4.1 spec was a clarification of the
3971 * behavior, not a change. In particular, this choice will only make
3972 * rendering work in cases where it would have had undefined results.
3976 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3977 _mesa_bind_buffer_base_transform_feedback(ctx
,
3978 ctx
->TransformFeedback
.CurrentObject
,
3979 index
, bufObj
, false);
3981 case GL_UNIFORM_BUFFER
:
3982 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
3984 case GL_SHADER_STORAGE_BUFFER
:
3985 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
3987 case GL_ATOMIC_COUNTER_BUFFER
:
3988 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
3989 "glBindBufferBase");
3992 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
3998 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
3999 const GLuint
*buffers
,
4000 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4002 GET_CURRENT_CONTEXT(ctx
);
4004 if (MESA_VERBOSE
& VERBOSE_API
) {
4005 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4006 _mesa_enum_to_string(target
), first
, count
,
4007 buffers
, offsets
, sizes
);
4011 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4012 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4013 "glBindBuffersRange");
4015 case GL_UNIFORM_BUFFER
:
4016 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4017 "glBindBuffersRange");
4019 case GL_SHADER_STORAGE_BUFFER
:
4020 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4021 "glBindBuffersRange");
4023 case GL_ATOMIC_COUNTER_BUFFER
:
4024 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4025 "glBindBuffersRange");
4028 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4029 _mesa_enum_to_string(target
));
4035 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4036 const GLuint
*buffers
)
4038 GET_CURRENT_CONTEXT(ctx
);
4040 if (MESA_VERBOSE
& VERBOSE_API
) {
4041 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4042 _mesa_enum_to_string(target
), first
, count
, buffers
);
4046 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4047 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4048 "glBindBuffersBase");
4050 case GL_UNIFORM_BUFFER
:
4051 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4052 "glBindBuffersBase");
4054 case GL_SHADER_STORAGE_BUFFER
:
4055 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4056 "glBindBuffersBase");
4058 case GL_ATOMIC_COUNTER_BUFFER
:
4059 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4060 "glBindBuffersBase");
4063 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4064 _mesa_enum_to_string(target
));
4070 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4073 GET_CURRENT_CONTEXT(ctx
);
4074 struct gl_buffer_object
*bufObj
;
4075 const GLintptr end
= offset
+ length
;
4077 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4078 * Profile) spec says:
4080 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4081 * name of an existing buffer object."
4083 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4084 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4085 _mesa_error(ctx
, GL_INVALID_VALUE
,
4086 "glInvalidateBufferSubData(name = %u) invalid object",
4091 /* The GL_ARB_invalidate_subdata spec says:
4093 * "An INVALID_VALUE error is generated if <offset> or <length> is
4094 * negative, or if <offset> + <length> is greater than the value of
4097 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
4098 _mesa_error(ctx
, GL_INVALID_VALUE
,
4099 "glInvalidateBufferSubData(invalid offset or length)");
4103 /* The OpenGL 4.4 (Core Profile) spec says:
4105 * "An INVALID_OPERATION error is generated if buffer is currently
4106 * mapped by MapBuffer or if the invalidate range intersects the range
4107 * currently mapped by MapBufferRange, unless it was mapped
4108 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4110 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4111 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4112 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4113 "glInvalidateBufferSubData(intersection with mapped "
4118 if (ctx
->Driver
.InvalidateBufferSubData
)
4119 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4123 _mesa_InvalidateBufferData(GLuint buffer
)
4125 GET_CURRENT_CONTEXT(ctx
);
4126 struct gl_buffer_object
*bufObj
;
4128 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4129 * Profile) spec says:
4131 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4132 * name of an existing buffer object."
4134 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4135 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4136 _mesa_error(ctx
, GL_INVALID_VALUE
,
4137 "glInvalidateBufferData(name = %u) invalid object",
4142 /* The OpenGL 4.4 (Core Profile) spec says:
4144 * "An INVALID_OPERATION error is generated if buffer is currently
4145 * mapped by MapBuffer or if the invalidate range intersects the range
4146 * currently mapped by MapBufferRange, unless it was mapped
4147 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4149 if (_mesa_check_disallowed_mapping(bufObj
)) {
4150 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4151 "glInvalidateBufferData(intersection with mapped "
4156 if (ctx
->Driver
.InvalidateBufferSubData
)
4157 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, 0, bufObj
->Size
);
4161 buffer_page_commitment(struct gl_context
*ctx
,
4162 struct gl_buffer_object
*bufferObj
,
4163 GLintptr offset
, GLsizeiptr size
,
4164 GLboolean commit
, const char *func
)
4166 if (!(bufferObj
->StorageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)) {
4167 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(not a sparse buffer object)",
4172 if (size
< 0 || size
> bufferObj
->Size
||
4173 offset
< 0 || offset
> bufferObj
->Size
- size
) {
4174 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(out of bounds)",
4179 /* The GL_ARB_sparse_buffer extension specification says:
4181 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4182 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4183 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4184 * not extend to the end of the buffer's data store."
4186 if (offset
% ctx
->Const
.SparseBufferPageSize
!= 0) {
4187 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset not aligned to page size)",
4192 if (size
% ctx
->Const
.SparseBufferPageSize
!= 0 &&
4193 offset
+ size
!= bufferObj
->Size
) {
4194 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size not aligned to page size)",
4199 ctx
->Driver
.BufferPageCommitment(ctx
, bufferObj
, offset
, size
, commit
);
4203 _mesa_BufferPageCommitmentARB(GLenum target
, GLintptr offset
, GLsizeiptr size
,
4206 GET_CURRENT_CONTEXT(ctx
);
4207 struct gl_buffer_object
*bufferObj
;
4209 bufferObj
= get_buffer(ctx
, "glBufferPageCommitmentARB", target
,
4214 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4215 "glBufferPageCommitmentARB");
4219 _mesa_NamedBufferPageCommitmentARB(GLuint buffer
, GLintptr offset
,
4220 GLsizeiptr size
, GLboolean commit
)
4222 GET_CURRENT_CONTEXT(ctx
);
4223 struct gl_buffer_object
*bufferObj
;
4225 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4226 if (!bufferObj
|| bufferObj
== &DummyBufferObject
) {
4227 /* Note: the extension spec is not clear about the excpected error value. */
4228 _mesa_error(ctx
, GL_INVALID_VALUE
,
4229 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4234 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4235 "glNamedBufferPageCommitmentARB");