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
= MALLOC_STRUCT(gl_buffer_object
);
450 _mesa_initialize_buffer_object(ctx
, obj
, name
);
456 * Delete a buffer object.
458 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
461 _mesa_delete_buffer_object(struct gl_context
*ctx
,
462 struct gl_buffer_object
*bufObj
)
466 vbo_delete_minmax_cache(bufObj
);
467 _mesa_align_free(bufObj
->Data
);
469 /* assign strange values here to help w/ debugging */
470 bufObj
->RefCount
= -1000;
473 mtx_destroy(&bufObj
->Mutex
);
481 * Set ptr to bufObj w/ reference counting.
482 * This is normally only called from the _mesa_reference_buffer_object() macro
483 * when there's a real pointer change.
486 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
487 struct gl_buffer_object
**ptr
,
488 struct gl_buffer_object
*bufObj
)
491 /* Unreference the old buffer */
492 GLboolean deleteFlag
= GL_FALSE
;
493 struct gl_buffer_object
*oldObj
= *ptr
;
495 mtx_lock(&oldObj
->Mutex
);
496 assert(oldObj
->RefCount
> 0);
498 deleteFlag
= (oldObj
->RefCount
== 0);
499 mtx_unlock(&oldObj
->Mutex
);
502 assert(ctx
->Driver
.DeleteBuffer
);
503 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
511 /* reference new buffer */
512 mtx_lock(&bufObj
->Mutex
);
513 assert(bufObj
->RefCount
> 0);
517 mtx_unlock(&bufObj
->Mutex
);
523 * Get the value of MESA_NO_MINMAX_CACHE.
526 get_no_minmax_cache()
528 static bool read
= false;
529 static bool disable
= false;
532 disable
= env_var_as_boolean("MESA_NO_MINMAX_CACHE", false);
541 * Initialize a buffer object to default values.
544 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
545 struct gl_buffer_object
*obj
,
548 memset(obj
, 0, sizeof(struct gl_buffer_object
));
549 mtx_init(&obj
->Mutex
, mtx_plain
);
552 obj
->Usage
= GL_STATIC_DRAW_ARB
;
554 if (get_no_minmax_cache())
555 obj
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
561 * Callback called from _mesa_HashWalk()
564 count_buffer_size(GLuint key
, void *data
, void *userData
)
566 const struct gl_buffer_object
*bufObj
=
567 (const struct gl_buffer_object
*) data
;
568 GLuint
*total
= (GLuint
*) userData
;
571 *total
= *total
+ bufObj
->Size
;
576 * Compute total size (in bytes) of all buffer objects for the given context.
577 * For debugging purposes.
580 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
584 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
591 * Allocate space for and store data in a buffer object. Any data that was
592 * previously stored in the buffer object is lost. If \c data is \c NULL,
593 * memory will be allocated, but no copy will occur.
595 * This is the default callback for \c dd_function_table::BufferData()
596 * Note that all GL error checking will have been done already.
598 * \param ctx GL context.
599 * \param target Buffer object target on which to operate.
600 * \param size Size, in bytes, of the new data store.
601 * \param data Pointer to the data to store in the buffer object. This
602 * pointer may be \c NULL.
603 * \param usage Hints about how the data will be used.
604 * \param bufObj Object to be used.
606 * \return GL_TRUE for success, GL_FALSE for failure
607 * \sa glBufferDataARB, dd_function_table::BufferData.
610 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptr size
,
611 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
612 struct gl_buffer_object
*bufObj
)
618 _mesa_align_free( bufObj
->Data
);
620 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
622 bufObj
->Data
= (GLubyte
*) new_data
;
624 bufObj
->Usage
= usage
;
625 bufObj
->StorageFlags
= storageFlags
;
628 memcpy( bufObj
->Data
, data
, size
);
640 * Replace data in a subrange of buffer object. If the data range
641 * specified by \c size + \c offset extends beyond the end of the buffer or
642 * if \c data is \c NULL, no copy is performed.
644 * This is the default callback for \c dd_function_table::BufferSubData()
645 * Note that all GL error checking will have been done already.
647 * \param ctx GL context.
648 * \param offset Offset of the first byte to be modified.
649 * \param size Size, in bytes, of the data range.
650 * \param data Pointer to the data to store in the buffer object.
651 * \param bufObj Object to be used.
653 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
656 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptr offset
,
657 GLsizeiptr size
, const GLvoid
*data
,
658 struct gl_buffer_object
*bufObj
)
662 /* this should have been caught in _mesa_BufferSubData() */
663 assert(size
+ offset
<= bufObj
->Size
);
666 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
672 * Retrieve data from a subrange of buffer object. If the data range
673 * specified by \c size + \c offset extends beyond the end of the buffer or
674 * if \c data is \c NULL, no copy is performed.
676 * This is the default callback for \c dd_function_table::GetBufferSubData()
677 * Note that all GL error checking will have been done already.
679 * \param ctx GL context.
680 * \param target Buffer object target on which to operate.
681 * \param offset Offset of the first byte to be fetched.
682 * \param size Size, in bytes, of the data range.
683 * \param data Destination for data
684 * \param bufObj Object to be used.
686 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
689 buffer_get_subdata(struct gl_context
*ctx
, GLintptrARB offset
,
690 GLsizeiptrARB size
, GLvoid
*data
,
691 struct gl_buffer_object
*bufObj
)
695 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
696 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
702 * Clear a subrange of the buffer object with copies of the supplied data.
703 * If data is NULL the buffer is filled with zeros.
705 * This is the default callback for \c dd_function_table::ClearBufferSubData()
706 * Note that all GL error checking will have been done already.
708 * \param ctx GL context.
709 * \param offset Offset of the first byte to be cleared.
710 * \param size Size, in bytes, of the to be cleared range.
711 * \param clearValue Source of the data.
712 * \param clearValueSize Size, in bytes, of the supplied data.
713 * \param bufObj Object to be cleared.
715 * \sa glClearBufferSubData, glClearBufferData and
716 * dd_function_table::ClearBufferSubData.
719 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
720 GLintptr offset
, GLsizeiptr size
,
721 const GLvoid
*clearValue
,
722 GLsizeiptr clearValueSize
,
723 struct gl_buffer_object
*bufObj
)
728 assert(ctx
->Driver
.MapBufferRange
);
729 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
731 GL_MAP_INVALIDATE_RANGE_BIT
,
732 bufObj
, MAP_INTERNAL
);
735 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
739 if (clearValue
== NULL
) {
740 /* Clear with zeros, per the spec */
741 memset(dest
, 0, size
);
742 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
746 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
747 memcpy(dest
, clearValue
, clearValueSize
);
748 dest
+= clearValueSize
;
751 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
756 * Default fallback for \c dd_function_table::MapBufferRange().
757 * Called via glMapBufferRange().
760 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
761 GLsizeiptr length
, GLbitfield access
,
762 struct gl_buffer_object
*bufObj
,
763 gl_map_buffer_index index
)
766 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
767 /* Just return a direct pointer to the data */
768 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
769 bufObj
->Mappings
[index
].Length
= length
;
770 bufObj
->Mappings
[index
].Offset
= offset
;
771 bufObj
->Mappings
[index
].AccessFlags
= access
;
772 return bufObj
->Mappings
[index
].Pointer
;
777 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
778 * Called via glFlushMappedBufferRange().
781 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
782 GLintptr offset
, GLsizeiptr length
,
783 struct gl_buffer_object
*obj
,
784 gl_map_buffer_index index
)
796 * Default callback for \c dd_function_table::UnmapBuffer().
798 * The input parameters will have been already tested for errors.
800 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
803 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
804 gl_map_buffer_index index
)
807 /* XXX we might assert here that bufObj->Pointer is non-null */
808 bufObj
->Mappings
[index
].Pointer
= NULL
;
809 bufObj
->Mappings
[index
].Length
= 0;
810 bufObj
->Mappings
[index
].Offset
= 0;
811 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
817 * Default fallback for \c dd_function_table::CopyBufferSubData().
818 * Called via glCopyBufferSubData().
821 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
822 struct gl_buffer_object
*src
,
823 struct gl_buffer_object
*dst
,
824 GLintptr readOffset
, GLintptr writeOffset
,
827 GLubyte
*srcPtr
, *dstPtr
;
830 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
832 GL_MAP_WRITE_BIT
, src
,
838 srcPtr
+= readOffset
;
839 dstPtr
+= writeOffset
;
841 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
842 GL_MAP_READ_BIT
, src
,
844 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
846 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
850 /* Note: the src and dst regions will never overlap. Trying to do so
851 * would generate GL_INVALID_VALUE earlier.
853 if (srcPtr
&& dstPtr
)
854 memcpy(dstPtr
, srcPtr
, size
);
856 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
858 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
864 * Initialize the state associated with buffer objects
867 _mesa_init_buffer_objects( struct gl_context
*ctx
)
871 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
872 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
873 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
875 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
876 ctx
->Shared
->NullBufferObj
);
878 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
879 ctx
->Shared
->NullBufferObj
);
880 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
881 ctx
->Shared
->NullBufferObj
);
883 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
884 ctx
->Shared
->NullBufferObj
);
886 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
887 ctx
->Shared
->NullBufferObj
);
889 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
890 ctx
->Shared
->NullBufferObj
);
892 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
893 ctx
->Shared
->NullBufferObj
);
895 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
,
896 ctx
->Shared
->NullBufferObj
);
898 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
,
899 ctx
->Shared
->NullBufferObj
);
901 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
,
902 ctx
->Shared
->NullBufferObj
);
904 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
905 _mesa_reference_buffer_object(ctx
,
906 &ctx
->UniformBufferBindings
[i
].BufferObject
,
907 ctx
->Shared
->NullBufferObj
);
908 ctx
->UniformBufferBindings
[i
].Offset
= -1;
909 ctx
->UniformBufferBindings
[i
].Size
= -1;
912 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
913 _mesa_reference_buffer_object(ctx
,
914 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
915 ctx
->Shared
->NullBufferObj
);
916 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
917 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
920 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
921 _mesa_reference_buffer_object(ctx
,
922 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
923 ctx
->Shared
->NullBufferObj
);
924 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
925 ctx
->AtomicBufferBindings
[i
].Size
= 0;
931 _mesa_free_buffer_objects( struct gl_context
*ctx
)
935 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
937 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
938 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
940 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
942 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
944 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
946 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
948 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
, NULL
);
950 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
952 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
, NULL
);
954 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
955 _mesa_reference_buffer_object(ctx
,
956 &ctx
->UniformBufferBindings
[i
].BufferObject
,
960 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
961 _mesa_reference_buffer_object(ctx
,
962 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
966 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
967 _mesa_reference_buffer_object(ctx
,
968 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
975 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
977 struct gl_buffer_object
**buf_handle
,
980 struct gl_buffer_object
*buf
= *buf_handle
;
982 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
)) {
983 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
987 if (!buf
|| buf
== &DummyBufferObject
) {
988 /* If this is a new buffer object id, or one which was generated but
989 * never used before, allocate a buffer object now.
991 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
993 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
996 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
1004 * Bind the specified target to buffer for the specified context.
1005 * Called by glBindBuffer() and other functions.
1008 bind_buffer_object(struct gl_context
*ctx
,
1009 struct gl_buffer_object
**bindTarget
, GLuint buffer
)
1011 struct gl_buffer_object
*oldBufObj
;
1012 struct gl_buffer_object
*newBufObj
= NULL
;
1016 /* Get pointer to old buffer object (to be unbound) */
1017 oldBufObj
= *bindTarget
;
1018 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
1019 return; /* rebinding the same buffer object- no change */
1022 * Get pointer to new buffer object (newBufObj)
1025 /* The spec says there's not a buffer object named 0, but we use
1026 * one internally because it simplifies things.
1028 newBufObj
= ctx
->Shared
->NullBufferObj
;
1031 /* non-default buffer object */
1032 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1033 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1034 &newBufObj
, "glBindBuffer"))
1038 /* record usage history */
1039 if (bindTarget
== &ctx
->Pack
.BufferObj
) {
1040 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1043 /* bind new buffer */
1044 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1049 * Update the default buffer objects in the given context to reference those
1050 * specified in the shared state and release those referencing the old
1054 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1056 /* Bind the NullBufferObj to remove references to those
1057 * in the shared context hash table.
1059 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1060 bind_buffer_object(ctx
, &ctx
->Array
.VAO
->IndexBufferObj
, 0);
1061 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1062 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1068 * Return the gl_buffer_object for the given ID.
1069 * Always return NULL for ID 0.
1071 struct gl_buffer_object
*
1072 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1077 return (struct gl_buffer_object
*)
1078 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1082 struct gl_buffer_object
*
1083 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1088 return (struct gl_buffer_object
*)
1089 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1093 * A convenience function for direct state access functions that throws
1094 * GL_INVALID_OPERATION if buffer is not the name of an existing
1097 struct gl_buffer_object
*
1098 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1101 struct gl_buffer_object
*bufObj
;
1103 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1104 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1105 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1106 "%s(non-existent buffer object %u)", caller
, buffer
);
1115 * Look up a buffer object for a multi-bind function.
1117 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1118 * of generating an error if the buffer ID is not zero or the name
1119 * of an existing buffer object.
1121 * If the buffer ID refers to an existing buffer object, a pointer
1122 * to the buffer object is returned. If the ID is zero, a pointer
1123 * to the shared NullBufferObj is returned. If the ID is not zero
1124 * and does not refer to a valid buffer object, this function
1127 * This function assumes that the caller has already locked the
1128 * hash table mutex by calling
1129 * _mesa_HashLockMutex(ctx->Shared->BufferObjects).
1131 struct gl_buffer_object
*
1132 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1133 const GLuint
*buffers
,
1134 GLuint index
, const char *caller
)
1136 struct gl_buffer_object
*bufObj
;
1138 if (buffers
[index
] != 0) {
1139 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1141 /* The multi-bind functions don't create the buffer objects
1142 when they don't exist. */
1143 if (bufObj
== &DummyBufferObject
)
1146 bufObj
= ctx
->Shared
->NullBufferObj
;
1149 /* The ARB_multi_bind spec says:
1151 * "An INVALID_OPERATION error is generated if any value
1152 * in <buffers> is not zero or the name of an existing
1153 * buffer object (per binding)."
1155 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1156 "%s(buffers[%u]=%u is not zero or the name "
1157 "of an existing buffer object)",
1158 caller
, index
, buffers
[index
]);
1166 * If *ptr points to obj, set ptr = the Null/default buffer object.
1167 * This is a helper for buffer object deletion.
1168 * The GL spec says that deleting a buffer object causes it to get
1169 * unbound from all arrays in the current context.
1172 unbind(struct gl_context
*ctx
,
1173 struct gl_vertex_array_object
*vao
, unsigned index
,
1174 struct gl_buffer_object
*obj
)
1176 if (vao
->BufferBinding
[index
].BufferObj
== obj
) {
1177 _mesa_bind_vertex_buffer(ctx
, vao
, index
, ctx
->Shared
->NullBufferObj
,
1178 vao
->BufferBinding
[index
].Offset
,
1179 vao
->BufferBinding
[index
].Stride
);
1185 * Plug default/fallback buffer object functions into the device
1189 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1191 /* GL_ARB_vertex/pixel_buffer_object */
1192 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1193 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1194 driver
->BufferData
= buffer_data_fallback
;
1195 driver
->BufferSubData
= buffer_sub_data_fallback
;
1196 driver
->GetBufferSubData
= buffer_get_subdata
;
1197 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1199 /* GL_ARB_clear_buffer_object */
1200 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1202 /* GL_ARB_map_buffer_range */
1203 driver
->MapBufferRange
= map_buffer_range_fallback
;
1204 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1206 /* GL_ARB_copy_buffer */
1207 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1212 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1213 struct gl_buffer_object
*bufObj
)
1215 for (int i
= 0; i
< MAP_COUNT
; i
++) {
1216 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1217 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1218 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1219 bufObj
->Mappings
[i
].AccessFlags
= 0;
1225 /**********************************************************************/
1227 /**********************************************************************/
1230 _mesa_BindBuffer_no_error(GLenum target
, GLuint buffer
)
1232 GET_CURRENT_CONTEXT(ctx
);
1234 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1235 bind_buffer_object(ctx
, bindTarget
, buffer
);
1240 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1242 GET_CURRENT_CONTEXT(ctx
);
1244 if (MESA_VERBOSE
& VERBOSE_API
) {
1245 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1246 _mesa_enum_to_string(target
), buffer
);
1249 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1251 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target %s)",
1252 _mesa_enum_to_string(target
));
1256 bind_buffer_object(ctx
, bindTarget
, buffer
);
1260 * Binds a buffer object to an atomic buffer binding point.
1262 * The caller is responsible for validating the offset,
1263 * flushing the vertices and updating NewDriverState.
1266 set_atomic_buffer_binding(struct gl_context
*ctx
,
1267 struct gl_atomic_buffer_binding
*binding
,
1268 struct gl_buffer_object
*bufObj
,
1272 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1274 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
1275 binding
->Offset
= 0;
1278 binding
->Offset
= offset
;
1279 binding
->Size
= size
;
1280 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
1285 * Binds a buffer object to a uniform buffer binding point.
1287 * The caller is responsible for flushing vertices and updating
1291 set_ubo_binding(struct gl_context
*ctx
,
1292 struct gl_uniform_buffer_binding
*binding
,
1293 struct gl_buffer_object
*bufObj
,
1298 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1300 binding
->Offset
= offset
;
1301 binding
->Size
= size
;
1302 binding
->AutomaticSize
= autoSize
;
1304 /* If this is a real buffer object, mark it has having been used
1305 * at some point as a UBO.
1308 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
1312 * Binds a buffer object to a shader storage buffer binding point.
1314 * The caller is responsible for flushing vertices and updating
1318 set_ssbo_binding(struct gl_context
*ctx
,
1319 struct gl_shader_storage_buffer_binding
*binding
,
1320 struct gl_buffer_object
*bufObj
,
1325 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1327 binding
->Offset
= offset
;
1328 binding
->Size
= size
;
1329 binding
->AutomaticSize
= autoSize
;
1331 /* If this is a real buffer object, mark it has having been used
1332 * at some point as a SSBO.
1335 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
1339 * Binds a buffer object to a uniform buffer binding point.
1341 * Unlike set_ubo_binding(), this function also flushes vertices
1342 * and updates NewDriverState. It also checks if the binding
1343 * has actually changed before updating it.
1346 bind_uniform_buffer(struct gl_context
*ctx
,
1348 struct gl_buffer_object
*bufObj
,
1353 struct gl_uniform_buffer_binding
*binding
=
1354 &ctx
->UniformBufferBindings
[index
];
1356 if (binding
->BufferObject
== bufObj
&&
1357 binding
->Offset
== offset
&&
1358 binding
->Size
== size
&&
1359 binding
->AutomaticSize
== autoSize
) {
1363 FLUSH_VERTICES(ctx
, 0);
1364 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
1366 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
1370 * Binds a buffer object to a shader storage buffer binding point.
1372 * Unlike set_ssbo_binding(), this function also flushes vertices
1373 * and updates NewDriverState. It also checks if the binding
1374 * has actually changed before updating it.
1377 bind_shader_storage_buffer(struct gl_context
*ctx
,
1379 struct gl_buffer_object
*bufObj
,
1384 struct gl_shader_storage_buffer_binding
*binding
=
1385 &ctx
->ShaderStorageBufferBindings
[index
];
1387 if (binding
->BufferObject
== bufObj
&&
1388 binding
->Offset
== offset
&&
1389 binding
->Size
== size
&&
1390 binding
->AutomaticSize
== autoSize
) {
1394 FLUSH_VERTICES(ctx
, 0);
1395 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
1397 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
1401 * Bind a buffer object to a uniform block binding point.
1402 * As above, but offset = 0.
1405 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
1407 struct gl_buffer_object
*bufObj
)
1409 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
1410 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1414 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
1416 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1417 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1419 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1423 * Bind a buffer object to a shader storage block binding point.
1424 * As above, but offset = 0.
1427 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
1429 struct gl_buffer_object
*bufObj
)
1431 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
1432 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1436 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
1438 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1439 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1441 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1445 bind_atomic_buffer(struct gl_context
*ctx
, unsigned index
,
1446 struct gl_buffer_object
*bufObj
, GLintptr offset
,
1449 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
1451 struct gl_atomic_buffer_binding
*binding
=
1452 &ctx
->AtomicBufferBindings
[index
];
1453 if (binding
->BufferObject
== bufObj
&&
1454 binding
->Offset
== offset
&&
1455 binding
->Size
== size
) {
1459 FLUSH_VERTICES(ctx
, 0);
1460 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
1462 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
1466 * Delete a set of buffer objects.
1468 * \param n Number of buffer objects to delete.
1469 * \param ids Array of \c n buffer object IDs.
1472 delete_buffers(struct gl_context
*ctx
, GLsizei n
, const GLuint
*ids
)
1474 FLUSH_VERTICES(ctx
, 0);
1476 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1478 for (GLsizei i
= 0; i
< n
; i
++) {
1479 struct gl_buffer_object
*bufObj
=
1480 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1482 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1485 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1487 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1489 /* unbind any vertex pointers bound to this buffer */
1490 for (j
= 0; j
< ARRAY_SIZE(vao
->BufferBinding
); j
++) {
1491 unbind(ctx
, vao
, j
, bufObj
);
1494 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1495 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1497 if (vao
->IndexBufferObj
== bufObj
) {
1498 bind_buffer_object(ctx
, &vao
->IndexBufferObj
, 0);
1501 /* unbind ARB_draw_indirect binding point */
1502 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1503 bind_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, 0);
1506 /* unbind ARB_indirect_parameters binding point */
1507 if (ctx
->ParameterBuffer
== bufObj
) {
1508 bind_buffer_object(ctx
, &ctx
->ParameterBuffer
, 0);
1511 /* unbind ARB_compute_shader binding point */
1512 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1513 bind_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, 0);
1516 /* unbind ARB_copy_buffer binding points */
1517 if (ctx
->CopyReadBuffer
== bufObj
) {
1518 bind_buffer_object(ctx
, &ctx
->CopyReadBuffer
, 0);
1520 if (ctx
->CopyWriteBuffer
== bufObj
) {
1521 bind_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, 0);
1524 /* unbind transform feedback binding points */
1525 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1526 bind_buffer_object(ctx
, &ctx
->TransformFeedback
.CurrentBuffer
, 0);
1528 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1529 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1530 _mesa_bind_buffer_base_transform_feedback(ctx
,
1531 ctx
->TransformFeedback
.CurrentObject
,
1532 j
, ctx
->Shared
->NullBufferObj
,
1537 /* unbind UBO binding points */
1538 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1539 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1540 bind_buffer_base_uniform_buffer(ctx
, j
,
1541 ctx
->Shared
->NullBufferObj
);
1545 if (ctx
->UniformBuffer
== bufObj
) {
1546 bind_buffer_object(ctx
, &ctx
->UniformBuffer
, 0);
1549 /* unbind SSBO binding points */
1550 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1551 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1552 bind_buffer_base_shader_storage_buffer(ctx
, j
,
1553 ctx
->Shared
->NullBufferObj
);
1557 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1558 bind_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, 0);
1561 /* unbind Atomci Buffer binding points */
1562 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1563 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1564 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1565 bind_atomic_buffer(ctx
, j
, ctx
->Shared
->NullBufferObj
, 0, 0);
1569 if (ctx
->AtomicBuffer
== bufObj
) {
1570 bind_buffer_object(ctx
, &ctx
->AtomicBuffer
, 0);
1573 /* unbind any pixel pack/unpack pointers bound to this buffer */
1574 if (ctx
->Pack
.BufferObj
== bufObj
) {
1575 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1577 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1578 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1581 if (ctx
->Texture
.BufferObject
== bufObj
) {
1582 bind_buffer_object(ctx
, &ctx
->Texture
.BufferObject
, 0);
1585 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1586 bind_buffer_object(ctx
, &ctx
->ExternalVirtualMemoryBuffer
, 0);
1589 /* unbind query buffer binding point */
1590 if (ctx
->QueryBuffer
== bufObj
) {
1591 bind_buffer_object(ctx
, &ctx
->QueryBuffer
, 0);
1594 /* The ID is immediately freed for re-use */
1595 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1596 /* Make sure we do not run into the classic ABA problem on bind.
1597 * We don't want to allow re-binding a buffer object that's been
1598 * "deleted" by glDeleteBuffers().
1600 * The explicit rebinding to the default object in the current context
1601 * prevents the above in the current context, but another context
1602 * sharing the same objects might suffer from this problem.
1603 * The alternative would be to do the hash lookup in any case on bind
1604 * which would introduce more runtime overhead than this.
1606 bufObj
->DeletePending
= GL_TRUE
;
1607 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1611 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1616 _mesa_DeleteBuffers_no_error(GLsizei n
, const GLuint
*ids
)
1618 GET_CURRENT_CONTEXT(ctx
);
1619 delete_buffers(ctx
, n
, ids
);
1624 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1626 GET_CURRENT_CONTEXT(ctx
);
1629 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1633 delete_buffers(ctx
, n
, ids
);
1638 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1639 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1643 create_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1646 struct gl_buffer_object
*buf
;
1652 * This must be atomic (generation and allocation of buffer object IDs)
1654 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1656 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1658 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1659 * DummyBufferObject. Otherwise, create a new buffer object and insert
1662 for (int i
= 0; i
< n
; i
++) {
1663 buffers
[i
] = first
+ i
;
1665 assert(ctx
->Driver
.NewBufferObject
);
1666 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1668 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCreateBuffers");
1669 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1674 buf
= &DummyBufferObject
;
1676 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1679 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1684 create_buffers_err(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1686 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1688 if (MESA_VERBOSE
& VERBOSE_API
)
1689 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1692 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1696 create_buffers(ctx
, n
, buffers
, dsa
);
1700 * Generate a set of unique buffer object IDs and store them in \c buffers.
1702 * \param n Number of IDs to generate.
1703 * \param buffers Array of \c n locations to store the IDs.
1706 _mesa_GenBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1708 GET_CURRENT_CONTEXT(ctx
);
1709 create_buffers(ctx
, n
, buffers
, false);
1714 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1716 GET_CURRENT_CONTEXT(ctx
);
1717 create_buffers_err(ctx
, n
, buffers
, false);
1721 * Create a set of buffer objects and store their unique IDs in \c buffers.
1723 * \param n Number of IDs to generate.
1724 * \param buffers Array of \c n locations to store the IDs.
1727 _mesa_CreateBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1729 GET_CURRENT_CONTEXT(ctx
);
1730 create_buffers(ctx
, n
, buffers
, true);
1735 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1737 GET_CURRENT_CONTEXT(ctx
);
1738 create_buffers_err(ctx
, n
, buffers
, true);
1743 * Determine if ID is the name of a buffer object.
1745 * \param id ID of the potential buffer object.
1746 * \return \c GL_TRUE if \c id is the name of a buffer object,
1747 * \c GL_FALSE otherwise.
1749 GLboolean GLAPIENTRY
1750 _mesa_IsBuffer(GLuint id
)
1752 struct gl_buffer_object
*bufObj
;
1753 GET_CURRENT_CONTEXT(ctx
);
1754 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1756 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1758 return bufObj
&& bufObj
!= &DummyBufferObject
;
1763 validate_buffer_storage(struct gl_context
*ctx
,
1764 struct gl_buffer_object
*bufObj
, GLsizeiptr size
,
1765 GLbitfield flags
, const char *func
)
1768 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1772 GLbitfield valid_flags
= GL_MAP_READ_BIT
|
1774 GL_MAP_PERSISTENT_BIT
|
1775 GL_MAP_COHERENT_BIT
|
1776 GL_DYNAMIC_STORAGE_BIT
|
1777 GL_CLIENT_STORAGE_BIT
;
1779 if (ctx
->Extensions
.ARB_sparse_buffer
)
1780 valid_flags
|= GL_SPARSE_STORAGE_BIT_ARB
;
1782 if (flags
& ~valid_flags
) {
1783 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1787 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1789 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1790 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1791 * MAP_READ_BIT or MAP_WRITE_BIT."
1793 if (flags
& GL_SPARSE_STORAGE_BIT_ARB
&&
1794 flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) {
1795 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(SPARSE_STORAGE and READ/WRITE)", func
);
1799 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1800 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1801 _mesa_error(ctx
, GL_INVALID_VALUE
,
1802 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1806 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1807 _mesa_error(ctx
, GL_INVALID_VALUE
,
1808 "%s(COHERENT and flags!=PERSISTENT)", func
);
1812 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
1813 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1822 buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1823 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1824 GLbitfield flags
, const char *func
)
1826 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1827 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1829 FLUSH_VERTICES(ctx
, 0);
1831 bufObj
->Written
= GL_TRUE
;
1832 bufObj
->Immutable
= GL_TRUE
;
1833 bufObj
->MinMaxCacheDirty
= true;
1835 assert(ctx
->Driver
.BufferData
);
1836 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1838 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1839 /* Even though the interaction between AMD_pinned_memory and
1840 * glBufferStorage is not described in the spec, Graham Sellers
1841 * said that it should behave the same as glBufferData.
1843 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1846 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1852 static ALWAYS_INLINE
void
1853 inlined_buffer_storage(GLenum target
, GLuint buffer
, GLsizeiptr size
,
1854 const GLvoid
*data
, GLbitfield flags
, bool dsa
,
1855 bool no_error
, const char *func
)
1857 GET_CURRENT_CONTEXT(ctx
);
1858 struct gl_buffer_object
*bufObj
;
1862 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1864 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
1870 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
1871 bufObj
= *bufObjPtr
;
1873 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
1879 if (no_error
|| validate_buffer_storage(ctx
, bufObj
, size
, flags
, func
))
1880 buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
, func
);
1885 _mesa_BufferStorage_no_error(GLenum target
, GLsizeiptr size
,
1886 const GLvoid
*data
, GLbitfield flags
)
1888 inlined_buffer_storage(target
, 0, size
, data
, flags
, false, true,
1894 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1897 inlined_buffer_storage(target
, 0, size
, data
, flags
, false, false,
1903 _mesa_BufferStorageMemEXT(GLenum target
, GLsizeiptr size
,
1904 GLuint memory
, GLuint64 offset
)
1911 _mesa_BufferStorageMemEXT_no_error(GLenum target
, GLsizeiptr size
,
1912 GLuint memory
, GLuint64 offset
)
1919 _mesa_NamedBufferStorage_no_error(GLuint buffer
, GLsizeiptr size
,
1920 const GLvoid
*data
, GLbitfield flags
)
1922 /* In direct state access, buffer objects have an unspecified target
1923 * since they are not required to be bound.
1925 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, true, true,
1926 "glNamedBufferStorage");
1931 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1934 /* In direct state access, buffer objects have an unspecified target
1935 * since they are not required to be bound.
1937 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, true, false,
1938 "glNamedBufferStorage");
1942 _mesa_NamedBufferStorageMemEXT(GLuint buffer
, GLsizeiptr size
,
1943 GLuint memory
, GLuint64 offset
)
1950 _mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer
, GLsizeiptr size
,
1951 GLuint memory
, GLuint64 offset
)
1957 static ALWAYS_INLINE
void
1958 buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1959 GLenum target
, GLsizeiptr size
, const GLvoid
*data
, GLenum usage
,
1960 const char *func
, bool no_error
)
1964 if (MESA_VERBOSE
& VERBOSE_API
) {
1965 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1967 _mesa_enum_to_string(target
),
1968 (long int) size
, data
,
1969 _mesa_enum_to_string(usage
));
1974 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1979 case GL_STREAM_DRAW_ARB
:
1980 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1982 case GL_STATIC_DRAW_ARB
:
1983 case GL_DYNAMIC_DRAW_ARB
:
1986 case GL_STREAM_READ_ARB
:
1987 case GL_STREAM_COPY_ARB
:
1988 case GL_STATIC_READ_ARB
:
1989 case GL_STATIC_COPY_ARB
:
1990 case GL_DYNAMIC_READ_ARB
:
1991 case GL_DYNAMIC_COPY_ARB
:
1992 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1995 valid_usage
= false;
2000 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
2001 _mesa_enum_to_string(usage
));
2005 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
2006 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
2011 /* Unmap the existing buffer. We'll replace it now. Not an error. */
2012 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
2014 FLUSH_VERTICES(ctx
, 0);
2016 bufObj
->Written
= GL_TRUE
;
2017 bufObj
->MinMaxCacheDirty
= true;
2020 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
2021 bufObj
->Name
, size
, data
, usage
);
2028 assert(ctx
->Driver
.BufferData
);
2029 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
2032 GL_DYNAMIC_STORAGE_BIT
,
2034 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
2036 /* From GL_AMD_pinned_memory:
2038 * INVALID_OPERATION is generated by BufferData if <target> is
2039 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
2040 * mapped to the GPU address space.
2042 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2045 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2051 buffer_data_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2052 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2053 GLenum usage
, const char *func
)
2055 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, false);
2059 buffer_data_no_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2060 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2061 GLenum usage
, const char *func
)
2063 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, true);
2067 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2068 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2069 GLenum usage
, const char *func
)
2071 buffer_data_error(ctx
, bufObj
, target
, size
, data
, usage
, func
);
2075 _mesa_BufferData_no_error(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2078 GET_CURRENT_CONTEXT(ctx
);
2080 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2081 buffer_data_no_error(ctx
, *bufObj
, target
, size
, data
, usage
,
2086 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
2087 const GLvoid
*data
, GLenum usage
)
2089 GET_CURRENT_CONTEXT(ctx
);
2090 struct gl_buffer_object
*bufObj
;
2092 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
2096 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
2101 _mesa_NamedBufferData_no_error(GLuint buffer
, GLsizeiptr size
,
2102 const GLvoid
*data
, GLenum usage
)
2104 GET_CURRENT_CONTEXT(ctx
);
2106 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2107 buffer_data_no_error(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2108 "glNamedBufferData");
2112 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2115 GET_CURRENT_CONTEXT(ctx
);
2116 struct gl_buffer_object
*bufObj
;
2118 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
2122 /* In direct state access, buffer objects have an unspecified target since
2123 * they are not required to be bound.
2125 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2126 "glNamedBufferData");
2131 validate_buffer_sub_data(struct gl_context
*ctx
,
2132 struct gl_buffer_object
*bufObj
,
2133 GLintptr offset
, GLsizeiptr size
,
2136 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2138 /* error already recorded */
2142 if (bufObj
->Immutable
&&
2143 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
2144 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2148 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2149 bufObj
->Usage
== GL_STATIC_COPY
) &&
2150 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
- 1) {
2151 /* If the application declared the buffer as static draw/copy or stream
2152 * draw, it should not be frequently modified with glBufferSubData.
2154 BUFFER_USAGE_WARNING(ctx
,
2155 "using %s(buffer %u, offset %u, size %u) to "
2156 "update a %s buffer",
2157 func
, bufObj
->Name
, offset
, size
,
2158 _mesa_enum_to_string(bufObj
->Usage
));
2166 * Implementation for glBufferSubData and glNamedBufferSubData.
2168 * \param ctx GL context.
2169 * \param bufObj The buffer object.
2170 * \param offset Offset of the first byte of the subdata range.
2171 * \param size Size, in bytes, of the subdata range.
2172 * \param data The data store.
2173 * \param func Name of calling function for recording errors.
2177 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2178 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
)
2183 bufObj
->NumSubDataCalls
++;
2184 bufObj
->Written
= GL_TRUE
;
2185 bufObj
->MinMaxCacheDirty
= true;
2187 assert(ctx
->Driver
.BufferSubData
);
2188 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
2192 static ALWAYS_INLINE
void
2193 buffer_sub_data(GLenum target
, GLuint buffer
, GLintptr offset
,
2194 GLsizeiptr size
, const GLvoid
*data
,
2195 bool dsa
, bool no_error
, const char *func
)
2197 GET_CURRENT_CONTEXT(ctx
);
2198 struct gl_buffer_object
*bufObj
;
2202 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2204 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
2210 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2211 bufObj
= *bufObjPtr
;
2213 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
2219 if (no_error
|| validate_buffer_sub_data(ctx
, bufObj
, offset
, size
, func
))
2220 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2225 _mesa_BufferSubData_no_error(GLenum target
, GLintptr offset
,
2226 GLsizeiptr size
, const GLvoid
*data
)
2228 buffer_sub_data(target
, 0, offset
, size
, data
, false, true,
2234 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
2235 GLsizeiptr size
, const GLvoid
*data
)
2237 buffer_sub_data(target
, 0, offset
, size
, data
, false, false,
2242 _mesa_NamedBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
2243 GLsizeiptr size
, const GLvoid
*data
)
2245 buffer_sub_data(0, buffer
, offset
, size
, data
, true, true,
2246 "glNamedBufferSubData");
2250 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
2251 GLsizeiptr size
, const GLvoid
*data
)
2253 buffer_sub_data(0, buffer
, offset
, size
, data
, true, false,
2254 "glNamedBufferSubData");
2259 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
2260 GLsizeiptr size
, GLvoid
*data
)
2262 GET_CURRENT_CONTEXT(ctx
);
2263 struct gl_buffer_object
*bufObj
;
2265 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
2266 GL_INVALID_OPERATION
);
2270 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2271 "glGetBufferSubData")) {
2275 assert(ctx
->Driver
.GetBufferSubData
);
2276 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2280 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
2281 GLsizeiptr size
, GLvoid
*data
)
2283 GET_CURRENT_CONTEXT(ctx
);
2284 struct gl_buffer_object
*bufObj
;
2286 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2287 "glGetNamedBufferSubData");
2291 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2292 "glGetNamedBufferSubData")) {
2296 assert(ctx
->Driver
.GetBufferSubData
);
2297 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2302 * \param subdata true if caller is *SubData, false if *Data
2304 static ALWAYS_INLINE
void
2305 clear_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2306 GLenum internalformat
, GLintptr offset
, GLsizeiptr size
,
2307 GLenum format
, GLenum type
, const GLvoid
*data
,
2308 const char *func
, bool subdata
, bool no_error
)
2310 mesa_format mesaFormat
;
2311 GLubyte clearValue
[MAX_PIXEL_BYTES
];
2312 GLsizeiptr clearValueSize
;
2314 /* This checks for disallowed mappings. */
2315 if (!no_error
&& !buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2321 mesaFormat
= _mesa_get_texbuffer_format(ctx
, internalformat
);
2323 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
2324 format
, type
, func
);
2327 if (mesaFormat
== MESA_FORMAT_NONE
)
2330 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
2332 (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0)) {
2333 _mesa_error(ctx
, GL_INVALID_VALUE
,
2334 "%s(offset or size is not a multiple of "
2335 "internalformat size)", func
);
2339 /* Bail early. Negative size has already been checked. */
2343 bufObj
->MinMaxCacheDirty
= true;
2346 /* clear to zeros, per the spec */
2347 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2348 NULL
, clearValueSize
, bufObj
);
2352 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
2353 format
, type
, data
, func
)) {
2357 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2358 clearValue
, clearValueSize
, bufObj
);
2362 clear_buffer_sub_data_error(struct gl_context
*ctx
,
2363 struct gl_buffer_object
*bufObj
,
2364 GLenum internalformat
, GLintptr offset
,
2365 GLsizeiptr size
, GLenum format
, GLenum type
,
2366 const GLvoid
*data
, const char *func
, bool subdata
)
2368 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2369 type
, data
, func
, subdata
, false);
2374 clear_buffer_sub_data_no_error(struct gl_context
*ctx
,
2375 struct gl_buffer_object
*bufObj
,
2376 GLenum internalformat
, GLintptr offset
,
2377 GLsizeiptr size
, GLenum format
, GLenum type
,
2378 const GLvoid
*data
, const char *func
,
2381 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2382 type
, data
, func
, subdata
, true);
2387 _mesa_ClearBufferData_no_error(GLenum target
, GLenum internalformat
,
2388 GLenum format
, GLenum type
, const GLvoid
*data
)
2390 GET_CURRENT_CONTEXT(ctx
);
2392 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2393 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, 0,
2394 (*bufObj
)->Size
, format
, type
, data
,
2395 "glClearBufferData", false);
2400 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
2401 GLenum type
, const GLvoid
*data
)
2403 GET_CURRENT_CONTEXT(ctx
);
2404 struct gl_buffer_object
*bufObj
;
2406 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
2410 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2411 format
, type
, data
, "glClearBufferData", false);
2416 _mesa_ClearNamedBufferData_no_error(GLuint buffer
, GLenum internalformat
,
2417 GLenum format
, GLenum type
,
2420 GET_CURRENT_CONTEXT(ctx
);
2422 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2423 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2424 format
, type
, data
, "glClearNamedBufferData",
2430 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
2431 GLenum format
, GLenum type
, const GLvoid
*data
)
2433 GET_CURRENT_CONTEXT(ctx
);
2434 struct gl_buffer_object
*bufObj
;
2436 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
2440 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2441 format
, type
, data
, "glClearNamedBufferData",
2447 _mesa_ClearBufferSubData_no_error(GLenum target
, GLenum internalformat
,
2448 GLintptr offset
, GLsizeiptr size
,
2449 GLenum format
, GLenum type
,
2452 GET_CURRENT_CONTEXT(ctx
);
2454 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2455 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, offset
, size
,
2456 format
, type
, data
, "glClearBufferSubData",
2462 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
2463 GLintptr offset
, GLsizeiptr size
,
2464 GLenum format
, GLenum type
,
2467 GET_CURRENT_CONTEXT(ctx
);
2468 struct gl_buffer_object
*bufObj
;
2470 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
2474 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2475 format
, type
, data
, "glClearBufferSubData",
2481 _mesa_ClearNamedBufferSubData_no_error(GLuint buffer
, GLenum internalformat
,
2482 GLintptr offset
, GLsizeiptr size
,
2483 GLenum format
, GLenum type
,
2486 GET_CURRENT_CONTEXT(ctx
);
2488 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2489 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, offset
, size
,
2491 "glClearNamedBufferSubData", true);
2496 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
2497 GLintptr offset
, GLsizeiptr size
,
2498 GLenum format
, GLenum type
,
2501 GET_CURRENT_CONTEXT(ctx
);
2502 struct gl_buffer_object
*bufObj
;
2504 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2505 "glClearNamedBufferSubData");
2509 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2510 format
, type
, data
, "glClearNamedBufferSubData",
2515 unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
2517 GLboolean status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2518 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2519 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2520 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2521 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2527 validate_and_unmap_buffer(struct gl_context
*ctx
,
2528 struct gl_buffer_object
*bufObj
,
2531 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2533 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2534 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2535 "%s(buffer is not mapped)", func
);
2540 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2541 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2543 /* check that last 100 bytes are still = magic value */
2544 for (i
= 0; i
< 100; i
++) {
2545 GLuint pos
= bufObj
->Size
- i
- 1;
2546 if (buf
[pos
] != 123) {
2547 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2548 " at position %d (value = %u)\n",
2556 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2557 GLuint i
, unchanged
= 0;
2558 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2560 /* check which bytes changed */
2561 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2562 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2569 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2570 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2575 return unmap_buffer(ctx
, bufObj
);
2578 GLboolean GLAPIENTRY
2579 _mesa_UnmapBuffer_no_error(GLenum target
)
2581 GET_CURRENT_CONTEXT(ctx
);
2582 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2583 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2585 return unmap_buffer(ctx
, bufObj
);
2588 GLboolean GLAPIENTRY
2589 _mesa_UnmapBuffer(GLenum target
)
2591 GET_CURRENT_CONTEXT(ctx
);
2592 struct gl_buffer_object
*bufObj
;
2594 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2598 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2601 GLboolean GLAPIENTRY
2602 _mesa_UnmapNamedBuffer_no_error(GLuint buffer
)
2604 GET_CURRENT_CONTEXT(ctx
);
2605 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2607 return unmap_buffer(ctx
, bufObj
);
2610 GLboolean GLAPIENTRY
2611 _mesa_UnmapNamedBuffer(GLuint buffer
)
2613 GET_CURRENT_CONTEXT(ctx
);
2614 struct gl_buffer_object
*bufObj
;
2616 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2620 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2625 get_buffer_parameter(struct gl_context
*ctx
,
2626 struct gl_buffer_object
*bufObj
, GLenum pname
,
2627 GLint64
*params
, const char *func
)
2630 case GL_BUFFER_SIZE_ARB
:
2631 *params
= bufObj
->Size
;
2633 case GL_BUFFER_USAGE_ARB
:
2634 *params
= bufObj
->Usage
;
2636 case GL_BUFFER_ACCESS_ARB
:
2637 *params
= simplified_access_mode(ctx
,
2638 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2640 case GL_BUFFER_MAPPED_ARB
:
2641 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2643 case GL_BUFFER_ACCESS_FLAGS
:
2644 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2646 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2648 case GL_BUFFER_MAP_OFFSET
:
2649 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2651 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2653 case GL_BUFFER_MAP_LENGTH
:
2654 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2656 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2658 case GL_BUFFER_IMMUTABLE_STORAGE
:
2659 if (!ctx
->Extensions
.ARB_buffer_storage
)
2661 *params
= bufObj
->Immutable
;
2663 case GL_BUFFER_STORAGE_FLAGS
:
2664 if (!ctx
->Extensions
.ARB_buffer_storage
)
2666 *params
= bufObj
->StorageFlags
;
2675 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2676 _mesa_enum_to_string(pname
));
2681 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2683 GET_CURRENT_CONTEXT(ctx
);
2684 struct gl_buffer_object
*bufObj
;
2687 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2688 GL_INVALID_OPERATION
);
2692 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2693 "glGetBufferParameteriv"))
2694 return; /* Error already recorded. */
2696 *params
= (GLint
) parameter
;
2700 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2702 GET_CURRENT_CONTEXT(ctx
);
2703 struct gl_buffer_object
*bufObj
;
2706 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2707 GL_INVALID_OPERATION
);
2711 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2712 "glGetBufferParameteri64v"))
2713 return; /* Error already recorded. */
2715 *params
= parameter
;
2719 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2721 GET_CURRENT_CONTEXT(ctx
);
2722 struct gl_buffer_object
*bufObj
;
2725 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2726 "glGetNamedBufferParameteriv");
2730 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2731 "glGetNamedBufferParameteriv"))
2732 return; /* Error already recorded. */
2734 *params
= (GLint
) parameter
;
2738 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2741 GET_CURRENT_CONTEXT(ctx
);
2742 struct gl_buffer_object
*bufObj
;
2745 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2746 "glGetNamedBufferParameteri64v");
2750 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2751 "glGetNamedBufferParameteri64v"))
2752 return; /* Error already recorded. */
2754 *params
= parameter
;
2759 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2761 GET_CURRENT_CONTEXT(ctx
);
2762 struct gl_buffer_object
*bufObj
;
2764 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2765 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2766 "GL_BUFFER_MAP_POINTER)");
2770 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2771 GL_INVALID_OPERATION
);
2775 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2779 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2781 GET_CURRENT_CONTEXT(ctx
);
2782 struct gl_buffer_object
*bufObj
;
2784 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2785 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2786 "GL_BUFFER_MAP_POINTER)");
2790 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2791 "glGetNamedBufferPointerv");
2795 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2800 copy_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*src
,
2801 struct gl_buffer_object
*dst
, GLintptr readOffset
,
2802 GLintptr writeOffset
, GLsizeiptr size
, const char *func
)
2804 if (_mesa_check_disallowed_mapping(src
)) {
2805 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2806 "%s(readBuffer is mapped)", func
);
2810 if (_mesa_check_disallowed_mapping(dst
)) {
2811 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2812 "%s(writeBuffer is mapped)", func
);
2816 if (readOffset
< 0) {
2817 _mesa_error(ctx
, GL_INVALID_VALUE
,
2818 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2822 if (writeOffset
< 0) {
2823 _mesa_error(ctx
, GL_INVALID_VALUE
,
2824 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2829 _mesa_error(ctx
, GL_INVALID_VALUE
,
2830 "%s(size %d < 0)", func
, (int) size
);
2834 if (readOffset
+ size
> src
->Size
) {
2835 _mesa_error(ctx
, GL_INVALID_VALUE
,
2836 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2837 (int) readOffset
, (int) size
, (int) src
->Size
);
2841 if (writeOffset
+ size
> dst
->Size
) {
2842 _mesa_error(ctx
, GL_INVALID_VALUE
,
2843 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2844 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2849 if (readOffset
+ size
<= writeOffset
) {
2852 else if (writeOffset
+ size
<= readOffset
) {
2856 /* overlapping src/dst is illegal */
2857 _mesa_error(ctx
, GL_INVALID_VALUE
,
2858 "%s(overlapping src/dst)", func
);
2863 dst
->MinMaxCacheDirty
= true;
2865 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2869 _mesa_CopyBufferSubData_no_error(GLenum readTarget
, GLenum writeTarget
,
2870 GLintptr readOffset
, GLintptr writeOffset
,
2873 GET_CURRENT_CONTEXT(ctx
);
2875 struct gl_buffer_object
**src_ptr
= get_buffer_target(ctx
, readTarget
);
2876 struct gl_buffer_object
*src
= *src_ptr
;
2878 struct gl_buffer_object
**dst_ptr
= get_buffer_target(ctx
, writeTarget
);
2879 struct gl_buffer_object
*dst
= *dst_ptr
;
2881 dst
->MinMaxCacheDirty
= true;
2882 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2887 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2888 GLintptr readOffset
, GLintptr writeOffset
,
2891 GET_CURRENT_CONTEXT(ctx
);
2892 struct gl_buffer_object
*src
, *dst
;
2894 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2895 GL_INVALID_OPERATION
);
2899 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2900 GL_INVALID_OPERATION
);
2904 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2905 "glCopyBufferSubData");
2909 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer
, GLuint writeBuffer
,
2910 GLintptr readOffset
,
2911 GLintptr writeOffset
, GLsizeiptr size
)
2913 GET_CURRENT_CONTEXT(ctx
);
2915 struct gl_buffer_object
*src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
2916 struct gl_buffer_object
*dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
2918 dst
->MinMaxCacheDirty
= true;
2919 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2924 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2925 GLintptr readOffset
, GLintptr writeOffset
,
2928 GET_CURRENT_CONTEXT(ctx
);
2929 struct gl_buffer_object
*src
, *dst
;
2931 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2932 "glCopyNamedBufferSubData");
2936 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2937 "glCopyNamedBufferSubData");
2941 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2942 "glCopyNamedBufferSubData");
2946 validate_map_buffer_range(struct gl_context
*ctx
,
2947 struct gl_buffer_object
*bufObj
, GLintptr offset
,
2948 GLsizeiptr length
, GLbitfield access
,
2951 GLbitfield allowed_access
;
2953 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, false);
2956 _mesa_error(ctx
, GL_INVALID_VALUE
,
2957 "%s(offset %ld < 0)", func
, (long) offset
);
2962 _mesa_error(ctx
, GL_INVALID_VALUE
,
2963 "%s(length %ld < 0)", func
, (long) length
);
2967 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2969 * "An INVALID_OPERATION error is generated for any of the following
2972 * * <length> is zero."
2974 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2975 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2979 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2983 allowed_access
= GL_MAP_READ_BIT
|
2985 GL_MAP_INVALIDATE_RANGE_BIT
|
2986 GL_MAP_INVALIDATE_BUFFER_BIT
|
2987 GL_MAP_FLUSH_EXPLICIT_BIT
|
2988 GL_MAP_UNSYNCHRONIZED_BIT
;
2990 if (ctx
->Extensions
.ARB_buffer_storage
) {
2991 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2992 GL_MAP_COHERENT_BIT
;
2995 if (access
& ~allowed_access
) {
2996 /* generate an error if any bits other than those allowed are set */
2997 _mesa_error(ctx
, GL_INVALID_VALUE
,
2998 "%s(access has undefined bits set)", func
);
3002 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
3003 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3004 "%s(access indicates neither read or write)", func
);
3008 if ((access
& GL_MAP_READ_BIT
) &&
3009 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
3010 GL_MAP_INVALIDATE_BUFFER_BIT
|
3011 GL_MAP_UNSYNCHRONIZED_BIT
))) {
3012 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3013 "%s(read access with disallowed bits)", func
);
3017 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
3018 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
3019 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3020 "%s(access has flush explicit without write)", func
);
3024 if (access
& GL_MAP_READ_BIT
&&
3025 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
3026 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3027 "%s(buffer does not allow read access)", func
);
3031 if (access
& GL_MAP_WRITE_BIT
&&
3032 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
3033 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3034 "%s(buffer does not allow write access)", func
);
3038 if (access
& GL_MAP_COHERENT_BIT
&&
3039 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
3040 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3041 "%s(buffer does not allow coherent access)", func
);
3045 if (access
& GL_MAP_PERSISTENT_BIT
&&
3046 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
3047 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3048 "%s(buffer does not allow persistent access)", func
);
3052 if (offset
+ length
> bufObj
->Size
) {
3053 _mesa_error(ctx
, GL_INVALID_VALUE
,
3054 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
3055 (unsigned long) offset
, (unsigned long) length
,
3056 (unsigned long) bufObj
->Size
);
3060 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3061 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3062 "%s(buffer already mapped)", func
);
3066 if (access
& GL_MAP_WRITE_BIT
) {
3067 bufObj
->NumMapBufferWriteCalls
++;
3068 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
3069 bufObj
->Usage
== GL_STATIC_COPY
) &&
3070 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
3071 BUFFER_USAGE_WARNING(ctx
,
3072 "using %s(buffer %u, offset %u, length %u) to "
3073 "update a %s buffer",
3074 func
, bufObj
->Name
, offset
, length
,
3075 _mesa_enum_to_string(bufObj
->Usage
));
3083 map_buffer_range(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
3084 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
3087 if (!bufObj
->Size
) {
3088 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
3092 assert(ctx
->Driver
.MapBufferRange
);
3093 void *map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
3096 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
3099 /* The driver callback should have set all these fields.
3100 * This is important because other modules (like VBO) might call
3101 * the driver function directly.
3103 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
3104 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
3105 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
3106 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
3109 if (access
& GL_MAP_WRITE_BIT
) {
3110 bufObj
->Written
= GL_TRUE
;
3111 bufObj
->MinMaxCacheDirty
= true;
3115 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3116 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
3117 bufObj
->Name
, bufObj
->Size
, access
);
3118 /* Access must be write only */
3119 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
3121 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
3122 for (i
= 0; i
< bufObj
->Size
; i
++)
3129 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3130 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
3132 /* buffer is 100 bytes larger than requested, fill with magic value */
3133 for (i
= 0; i
< 100; i
++) {
3134 buf
[bufObj
->Size
- i
- 1] = 123;
3143 _mesa_MapBufferRange_no_error(GLenum target
, GLintptr offset
,
3144 GLsizeiptr length
, GLbitfield access
)
3146 GET_CURRENT_CONTEXT(ctx
);
3148 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3149 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3151 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3152 "glMapBufferRange");
3156 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
3159 GET_CURRENT_CONTEXT(ctx
);
3160 struct gl_buffer_object
*bufObj
;
3162 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3163 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3164 "glMapBufferRange(ARB_map_buffer_range not supported)");
3168 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
3172 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3173 "glMapBufferRange"))
3176 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3177 "glMapBufferRange");
3181 _mesa_MapNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3182 GLsizeiptr length
, GLbitfield access
)
3184 GET_CURRENT_CONTEXT(ctx
);
3185 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3187 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3188 "glMapNamedBufferRange");
3192 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3195 GET_CURRENT_CONTEXT(ctx
);
3196 struct gl_buffer_object
*bufObj
;
3198 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3199 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3200 "glMapNamedBufferRange("
3201 "ARB_map_buffer_range not supported)");
3205 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
3209 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3210 "glMapNamedBufferRange"))
3213 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3214 "glMapNamedBufferRange");
3218 * Converts GLenum access from MapBuffer and MapNamedBuffer into
3219 * flags for input to map_buffer_range.
3221 * \return true if the type of requested access is permissible.
3224 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
3228 case GL_READ_ONLY_ARB
:
3229 *flags
= GL_MAP_READ_BIT
;
3230 return _mesa_is_desktop_gl(ctx
);
3231 case GL_WRITE_ONLY_ARB
:
3232 *flags
= GL_MAP_WRITE_BIT
;
3234 case GL_READ_WRITE_ARB
:
3235 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
3236 return _mesa_is_desktop_gl(ctx
);
3243 _mesa_MapBuffer_no_error(GLenum target
, GLenum access
)
3245 GET_CURRENT_CONTEXT(ctx
);
3247 GLbitfield accessFlags
;
3248 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3250 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3251 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3253 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3258 _mesa_MapBuffer(GLenum target
, GLenum access
)
3260 GET_CURRENT_CONTEXT(ctx
);
3261 struct gl_buffer_object
*bufObj
;
3262 GLbitfield accessFlags
;
3264 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3265 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
3269 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
3273 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3277 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3282 _mesa_MapNamedBuffer_no_error(GLuint buffer
, GLenum access
)
3284 GET_CURRENT_CONTEXT(ctx
);
3286 GLbitfield accessFlags
;
3287 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3289 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3291 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3292 "glMapNamedBuffer");
3296 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
3298 GET_CURRENT_CONTEXT(ctx
);
3299 struct gl_buffer_object
*bufObj
;
3300 GLbitfield accessFlags
;
3302 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3303 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
3307 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
3311 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3312 "glMapNamedBuffer"))
3315 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3316 "glMapNamedBuffer");
3321 flush_mapped_buffer_range(struct gl_context
*ctx
,
3322 struct gl_buffer_object
*bufObj
,
3323 GLintptr offset
, GLsizeiptr length
,
3326 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3327 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3328 "%s(ARB_map_buffer_range not supported)", func
);
3333 _mesa_error(ctx
, GL_INVALID_VALUE
,
3334 "%s(offset %ld < 0)", func
, (long) offset
);
3339 _mesa_error(ctx
, GL_INVALID_VALUE
,
3340 "%s(length %ld < 0)", func
, (long) length
);
3344 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3345 /* buffer is not mapped */
3346 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3347 "%s(buffer is not mapped)", func
);
3351 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
3352 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
3353 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3354 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
3358 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
3359 _mesa_error(ctx
, GL_INVALID_VALUE
,
3360 "%s(offset %ld + length %ld > mapped length %ld)", func
,
3361 (long) offset
, (long) length
,
3362 (long) bufObj
->Mappings
[MAP_USER
].Length
);
3366 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
3368 if (ctx
->Driver
.FlushMappedBufferRange
)
3369 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3374 _mesa_FlushMappedBufferRange_no_error(GLenum target
, GLintptr offset
,
3377 GET_CURRENT_CONTEXT(ctx
);
3378 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3379 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3381 if (ctx
->Driver
.FlushMappedBufferRange
)
3382 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3387 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
3390 GET_CURRENT_CONTEXT(ctx
);
3391 struct gl_buffer_object
*bufObj
;
3393 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
3394 GL_INVALID_OPERATION
);
3398 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3399 "glFlushMappedBufferRange");
3403 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3406 GET_CURRENT_CONTEXT(ctx
);
3407 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3409 if (ctx
->Driver
.FlushMappedBufferRange
)
3410 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3415 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
3418 GET_CURRENT_CONTEXT(ctx
);
3419 struct gl_buffer_object
*bufObj
;
3421 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
3422 "glFlushMappedNamedBufferRange");
3426 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3427 "glFlushMappedNamedBufferRange");
3431 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
, GLuint index
,
3432 struct gl_buffer_object
*bufObj
,
3433 GLintptr offset
, GLsizeiptr size
)
3435 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3440 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3441 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3445 * Bind a region of a buffer object to a uniform block binding point.
3446 * \param index the uniform buffer binding point index
3447 * \param bufObj the buffer object
3448 * \param offset offset to the start of buffer object region
3449 * \param size size of the buffer object region
3452 bind_buffer_range_uniform_buffer_err(struct gl_context
*ctx
, GLuint index
,
3453 struct gl_buffer_object
*bufObj
,
3454 GLintptr offset
, GLsizeiptr size
)
3456 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3457 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3461 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3462 _mesa_error(ctx
, GL_INVALID_VALUE
,
3463 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3464 ctx
->Const
.UniformBufferOffsetAlignment
);
3468 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3472 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3474 struct gl_buffer_object
*bufObj
,
3478 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3483 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3484 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3488 * Bind a region of a buffer object to a shader storage block binding point.
3489 * \param index the shader storage buffer binding point index
3490 * \param bufObj the buffer object
3491 * \param offset offset to the start of buffer object region
3492 * \param size size of the buffer object region
3495 bind_buffer_range_shader_storage_buffer_err(struct gl_context
*ctx
,
3497 struct gl_buffer_object
*bufObj
,
3498 GLintptr offset
, GLsizeiptr size
)
3500 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3501 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3505 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3506 _mesa_error(ctx
, GL_INVALID_VALUE
,
3507 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3508 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3512 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3516 * Binds a buffer object to an atomic buffer binding point.
3518 * Unlike set_atomic_buffer_binding(), this function also validates the
3519 * index and offset, flushes vertices, and updates NewDriverState.
3520 * It also checks if the binding has actually changing before
3524 bind_atomic_buffer_err(struct gl_context
*ctx
, unsigned index
,
3525 struct gl_buffer_object
*bufObj
, GLintptr offset
,
3526 GLsizeiptr size
, const char *name
)
3528 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3529 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3533 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3534 _mesa_error(ctx
, GL_INVALID_VALUE
,
3535 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3536 ATOMIC_COUNTER_SIZE
);
3540 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
3544 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3546 const GLintptr
*offsets
,
3547 const GLsizeiptr
*sizes
)
3549 if (offsets
[index
] < 0) {
3550 /* The ARB_multi_bind spec says:
3552 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3553 * value in <offsets> is less than zero (per binding)."
3555 _mesa_error(ctx
, GL_INVALID_VALUE
,
3556 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3557 index
, (int64_t) offsets
[index
]);
3561 if (sizes
[index
] <= 0) {
3562 /* The ARB_multi_bind spec says:
3564 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3565 * value in <sizes> is less than or equal to zero (per binding)."
3567 _mesa_error(ctx
, GL_INVALID_VALUE
,
3568 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3569 index
, (int64_t) sizes
[index
]);
3577 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3578 GLuint first
, GLsizei count
,
3581 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3582 _mesa_error(ctx
, GL_INVALID_ENUM
,
3583 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3587 /* The ARB_multi_bind_spec says:
3589 * "An INVALID_OPERATION error is generated if <first> + <count> is
3590 * greater than the number of target-specific indexed binding points,
3591 * as described in section 6.7.1."
3593 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3594 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3595 "%s(first=%u + count=%d > the value of "
3596 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3597 caller
, first
, count
,
3598 ctx
->Const
.MaxUniformBufferBindings
);
3606 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3607 GLuint first
, GLsizei count
,
3610 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3611 _mesa_error(ctx
, GL_INVALID_ENUM
,
3612 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3616 /* The ARB_multi_bind_spec says:
3618 * "An INVALID_OPERATION error is generated if <first> + <count> is
3619 * greater than the number of target-specific indexed binding points,
3620 * as described in section 6.7.1."
3622 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3623 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3624 "%s(first=%u + count=%d > the value of "
3625 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3626 caller
, first
, count
,
3627 ctx
->Const
.MaxShaderStorageBufferBindings
);
3635 * Unbind all uniform buffers in the range
3636 * <first> through <first>+<count>-1
3639 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3641 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3643 for (int i
= 0; i
< count
; i
++)
3644 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3645 bufObj
, -1, -1, GL_TRUE
);
3649 * Unbind all shader storage buffers in the range
3650 * <first> through <first>+<count>-1
3653 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3656 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3658 for (int i
= 0; i
< count
; i
++)
3659 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3660 bufObj
, -1, -1, GL_TRUE
);
3664 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3665 const GLuint
*buffers
,
3667 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3670 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3673 /* Assume that at least one binding will be changed */
3674 FLUSH_VERTICES(ctx
, 0);
3675 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3678 /* The ARB_multi_bind spec says:
3680 * "If <buffers> is NULL, all bindings from <first> through
3681 * <first>+<count>-1 are reset to their unbound (zero) state.
3682 * In this case, the offsets and sizes associated with the
3683 * binding points are set to default values, ignoring
3684 * <offsets> and <sizes>."
3686 unbind_uniform_buffers(ctx
, first
, count
);
3690 /* Note that the error semantics for multi-bind commands differ from
3691 * those of other GL commands.
3693 * The Issues section in the ARB_multi_bind spec says:
3695 * "(11) Typically, OpenGL specifies that if an error is generated by a
3696 * command, that command has no effect. This is somewhat
3697 * unfortunate for multi-bind commands, because it would require a
3698 * first pass to scan the entire list of bound objects for errors
3699 * and then a second pass to actually perform the bindings.
3700 * Should we have different error semantics?
3702 * RESOLVED: Yes. In this specification, when the parameters for
3703 * one of the <count> binding points are invalid, that binding point
3704 * is not updated and an error will be generated. However, other
3705 * binding points in the same command will be updated if their
3706 * parameters are valid and no other error occurs."
3709 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3711 for (int i
= 0; i
< count
; i
++) {
3712 struct gl_uniform_buffer_binding
*binding
=
3713 &ctx
->UniformBufferBindings
[first
+ i
];
3714 struct gl_buffer_object
*bufObj
;
3715 GLintptr offset
= 0;
3716 GLsizeiptr size
= 0;
3719 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3722 /* The ARB_multi_bind spec says:
3724 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3725 * pair of values in <offsets> and <sizes> does not respectively
3726 * satisfy the constraints described for those parameters for the
3727 * specified target, as described in section 6.7.1 (per binding)."
3729 * Section 6.7.1 refers to table 6.5, which says:
3731 * "┌───────────────────────────────────────────────────────────────┐
3732 * │ Uniform buffer array bindings (see sec. 7.6) │
3733 * ├─────────────────────┬─────────────────────────────────────────┤
3735 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3736 * │ │ OFFSET_ALIGNMENT │
3738 * │ size restriction │ none │
3739 * └─────────────────────┴─────────────────────────────────────────┘"
3741 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3742 _mesa_error(ctx
, GL_INVALID_VALUE
,
3743 "glBindBuffersRange(offsets[%u]=%" PRId64
3744 " is misaligned; it must be a multiple of the value of "
3745 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3746 "target=GL_UNIFORM_BUFFER)",
3747 i
, (int64_t) offsets
[i
],
3748 ctx
->Const
.UniformBufferOffsetAlignment
);
3752 offset
= offsets
[i
];
3756 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3757 bufObj
= binding
->BufferObject
;
3759 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3762 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3763 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3765 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3769 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3773 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3774 GLsizei count
, const GLuint
*buffers
,
3776 const GLintptr
*offsets
,
3777 const GLsizeiptr
*sizes
,
3780 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3783 /* Assume that at least one binding will be changed */
3784 FLUSH_VERTICES(ctx
, 0);
3785 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3788 /* The ARB_multi_bind spec says:
3790 * "If <buffers> is NULL, all bindings from <first> through
3791 * <first>+<count>-1 are reset to their unbound (zero) state.
3792 * In this case, the offsets and sizes associated with the
3793 * binding points are set to default values, ignoring
3794 * <offsets> and <sizes>."
3796 unbind_shader_storage_buffers(ctx
, first
, count
);
3800 /* Note that the error semantics for multi-bind commands differ from
3801 * those of other GL commands.
3803 * The Issues section in the ARB_multi_bind spec says:
3805 * "(11) Typically, OpenGL specifies that if an error is generated by a
3806 * command, that command has no effect. This is somewhat
3807 * unfortunate for multi-bind commands, because it would require a
3808 * first pass to scan the entire list of bound objects for errors
3809 * and then a second pass to actually perform the bindings.
3810 * Should we have different error semantics?
3812 * RESOLVED: Yes. In this specification, when the parameters for
3813 * one of the <count> binding points are invalid, that binding point
3814 * is not updated and an error will be generated. However, other
3815 * binding points in the same command will be updated if their
3816 * parameters are valid and no other error occurs."
3819 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3821 for (int i
= 0; i
< count
; i
++) {
3822 struct gl_shader_storage_buffer_binding
*binding
=
3823 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3824 struct gl_buffer_object
*bufObj
;
3825 GLintptr offset
= 0;
3826 GLsizeiptr size
= 0;
3829 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3832 /* The ARB_multi_bind spec says:
3834 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3835 * pair of values in <offsets> and <sizes> does not respectively
3836 * satisfy the constraints described for those parameters for the
3837 * specified target, as described in section 6.7.1 (per binding)."
3839 * Section 6.7.1 refers to table 6.5, which says:
3841 * "┌───────────────────────────────────────────────────────────────┐
3842 * │ Shader storage buffer array bindings (see sec. 7.8) │
3843 * ├─────────────────────┬─────────────────────────────────────────┤
3845 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3846 * │ │ BUFFER_OFFSET_ALIGNMENT │
3848 * │ size restriction │ none │
3849 * └─────────────────────┴─────────────────────────────────────────┘"
3851 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3852 _mesa_error(ctx
, GL_INVALID_VALUE
,
3853 "glBindBuffersRange(offsets[%u]=%" PRId64
3854 " is misaligned; it must be a multiple of the value of "
3855 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3856 "target=GL_SHADER_STORAGE_BUFFER)",
3857 i
, (int64_t) offsets
[i
],
3858 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3862 offset
= offsets
[i
];
3866 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3867 bufObj
= binding
->BufferObject
;
3869 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3872 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3873 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3875 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3879 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3883 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3884 struct gl_transform_feedback_object
*tfObj
,
3885 GLuint first
, GLsizei count
, const char *caller
)
3887 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3888 _mesa_error(ctx
, GL_INVALID_ENUM
,
3889 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3893 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3895 * "An INVALID_OPERATION error is generated :
3898 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3899 * FEEDBACK_BUFFER and transform feedback is currently active."
3901 * We assume that this is also meant to apply to BindBuffersRange
3902 * and BindBuffersBase.
3904 if (tfObj
->Active
) {
3905 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3906 "%s(Changing transform feedback buffers while "
3907 "transform feedback is active)", caller
);
3911 /* The ARB_multi_bind_spec says:
3913 * "An INVALID_OPERATION error is generated if <first> + <count> is
3914 * greater than the number of target-specific indexed binding points,
3915 * as described in section 6.7.1."
3917 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3918 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3919 "%s(first=%u + count=%d > the value of "
3920 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3921 caller
, first
, count
,
3922 ctx
->Const
.MaxTransformFeedbackBuffers
);
3930 * Unbind all transform feedback buffers in the range
3931 * <first> through <first>+<count>-1
3934 unbind_xfb_buffers(struct gl_context
*ctx
,
3935 struct gl_transform_feedback_object
*tfObj
,
3936 GLuint first
, GLsizei count
)
3938 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3940 for (int i
= 0; i
< count
; i
++)
3941 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3946 bind_xfb_buffers(struct gl_context
*ctx
,
3947 GLuint first
, GLsizei count
,
3948 const GLuint
*buffers
,
3950 const GLintptr
*offsets
,
3951 const GLsizeiptr
*sizes
,
3954 struct gl_transform_feedback_object
*tfObj
=
3955 ctx
->TransformFeedback
.CurrentObject
;
3957 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
3960 /* Assume that at least one binding will be changed */
3961 FLUSH_VERTICES(ctx
, 0);
3962 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3965 /* The ARB_multi_bind spec says:
3967 * "If <buffers> is NULL, all bindings from <first> through
3968 * <first>+<count>-1 are reset to their unbound (zero) state.
3969 * In this case, the offsets and sizes associated with the
3970 * binding points are set to default values, ignoring
3971 * <offsets> and <sizes>."
3973 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3977 /* Note that the error semantics for multi-bind commands differ from
3978 * those of other GL commands.
3980 * The Issues section in the ARB_multi_bind spec says:
3982 * "(11) Typically, OpenGL specifies that if an error is generated by a
3983 * command, that command has no effect. This is somewhat
3984 * unfortunate for multi-bind commands, because it would require a
3985 * first pass to scan the entire list of bound objects for errors
3986 * and then a second pass to actually perform the bindings.
3987 * Should we have different error semantics?
3989 * RESOLVED: Yes. In this specification, when the parameters for
3990 * one of the <count> binding points are invalid, that binding point
3991 * is not updated and an error will be generated. However, other
3992 * binding points in the same command will be updated if their
3993 * parameters are valid and no other error occurs."
3996 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3998 for (int i
= 0; i
< count
; i
++) {
3999 const GLuint index
= first
+ i
;
4000 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
4001 struct gl_buffer_object
*bufObj
;
4002 GLintptr offset
= 0;
4003 GLsizeiptr size
= 0;
4006 offset
= offsets
[i
];
4009 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4012 /* The ARB_multi_bind spec says:
4014 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4015 * pair of values in <offsets> and <sizes> does not respectively
4016 * satisfy the constraints described for those parameters for the
4017 * specified target, as described in section 6.7.1 (per binding)."
4019 * Section 6.7.1 refers to table 6.5, which says:
4021 * "┌───────────────────────────────────────────────────────────────┐
4022 * │ Transform feedback array bindings (see sec. 13.2.2) │
4023 * ├───────────────────────┬───────────────────────────────────────┤
4025 * │ offset restriction │ multiple of 4 │
4027 * │ size restriction │ multiple of 4 │
4028 * └───────────────────────┴───────────────────────────────────────┘"
4030 if (offsets
[i
] & 0x3) {
4031 _mesa_error(ctx
, GL_INVALID_VALUE
,
4032 "glBindBuffersRange(offsets[%u]=%" PRId64
4033 " is misaligned; it must be a multiple of 4 when "
4034 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4035 i
, (int64_t) offsets
[i
]);
4039 if (sizes
[i
] & 0x3) {
4040 _mesa_error(ctx
, GL_INVALID_VALUE
,
4041 "glBindBuffersRange(sizes[%u]=%" PRId64
4042 " is misaligned; it must be a multiple of 4 when "
4043 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4044 i
, (int64_t) sizes
[i
]);
4048 offset
= offsets
[i
];
4052 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
4053 bufObj
= boundBufObj
;
4055 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
4058 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
4062 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4066 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
4067 GLuint first
, GLsizei count
,
4070 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
4071 _mesa_error(ctx
, GL_INVALID_ENUM
,
4072 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
4076 /* The ARB_multi_bind_spec says:
4078 * "An INVALID_OPERATION error is generated if <first> + <count> is
4079 * greater than the number of target-specific indexed binding points,
4080 * as described in section 6.7.1."
4082 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
4083 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4084 "%s(first=%u + count=%d > the value of "
4085 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4086 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
4094 * Unbind all atomic counter buffers in the range
4095 * <first> through <first>+<count>-1
4098 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
4100 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
4102 for (int i
= 0; i
< count
; i
++)
4103 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
4108 bind_atomic_buffers(struct gl_context
*ctx
,
4111 const GLuint
*buffers
,
4113 const GLintptr
*offsets
,
4114 const GLsizeiptr
*sizes
,
4117 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
4120 /* Assume that at least one binding will be changed */
4121 FLUSH_VERTICES(ctx
, 0);
4122 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
4125 /* The ARB_multi_bind spec says:
4127 * "If <buffers> is NULL, all bindings from <first> through
4128 * <first>+<count>-1 are reset to their unbound (zero) state.
4129 * In this case, the offsets and sizes associated with the
4130 * binding points are set to default values, ignoring
4131 * <offsets> and <sizes>."
4133 unbind_atomic_buffers(ctx
, first
, count
);
4137 /* Note that the error semantics for multi-bind commands differ from
4138 * those of other GL commands.
4140 * The Issues section in the ARB_multi_bind spec says:
4142 * "(11) Typically, OpenGL specifies that if an error is generated by a
4143 * command, that command has no effect. This is somewhat
4144 * unfortunate for multi-bind commands, because it would require a
4145 * first pass to scan the entire list of bound objects for errors
4146 * and then a second pass to actually perform the bindings.
4147 * Should we have different error semantics?
4149 * RESOLVED: Yes. In this specification, when the parameters for
4150 * one of the <count> binding points are invalid, that binding point
4151 * is not updated and an error will be generated. However, other
4152 * binding points in the same command will be updated if their
4153 * parameters are valid and no other error occurs."
4156 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4158 for (int i
= 0; i
< count
; i
++) {
4159 struct gl_atomic_buffer_binding
*binding
=
4160 &ctx
->AtomicBufferBindings
[first
+ i
];
4161 struct gl_buffer_object
*bufObj
;
4162 GLintptr offset
= 0;
4163 GLsizeiptr size
= 0;
4166 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4169 /* The ARB_multi_bind spec says:
4171 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4172 * pair of values in <offsets> and <sizes> does not respectively
4173 * satisfy the constraints described for those parameters for the
4174 * specified target, as described in section 6.7.1 (per binding)."
4176 * Section 6.7.1 refers to table 6.5, which says:
4178 * "┌───────────────────────────────────────────────────────────────┐
4179 * │ Atomic counter array bindings (see sec. 7.7.2) │
4180 * ├───────────────────────┬───────────────────────────────────────┤
4182 * │ offset restriction │ multiple of 4 │
4184 * │ size restriction │ none │
4185 * └───────────────────────┴───────────────────────────────────────┘"
4187 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
4188 _mesa_error(ctx
, GL_INVALID_VALUE
,
4189 "glBindBuffersRange(offsets[%u]=%" PRId64
4190 " is misaligned; it must be a multiple of %d when "
4191 "target=GL_ATOMIC_COUNTER_BUFFER)",
4192 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
4196 offset
= offsets
[i
];
4200 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
4201 bufObj
= binding
->BufferObject
;
4203 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
4206 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
4209 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4212 static ALWAYS_INLINE
void
4213 bind_buffer_range(GLenum target
, GLuint index
, GLuint buffer
, GLintptr offset
,
4214 GLsizeiptr size
, bool no_error
)
4216 GET_CURRENT_CONTEXT(ctx
);
4217 struct gl_buffer_object
*bufObj
;
4219 if (MESA_VERBOSE
& VERBOSE_API
) {
4220 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4221 _mesa_enum_to_string(target
), index
, buffer
,
4222 (unsigned long) offset
, (unsigned long) size
);
4226 bufObj
= ctx
->Shared
->NullBufferObj
;
4228 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4229 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4230 &bufObj
, "glBindBufferRange"))
4236 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4237 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4238 index
, bufObj
, offset
, size
);
4240 case GL_UNIFORM_BUFFER
:
4241 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
4243 case GL_SHADER_STORAGE_BUFFER
:
4244 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
,
4247 case GL_ATOMIC_COUNTER_BUFFER
:
4248 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
4251 unreachable("invalid BindBufferRange target with KHR_no_error");
4255 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4256 "glBindBufferRange(invalid buffer=%u)", buffer
);
4262 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
4269 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4270 if (!_mesa_validate_buffer_range_xfb(ctx
,
4271 ctx
->TransformFeedback
.CurrentObject
,
4272 index
, bufObj
, offset
, size
,
4276 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4277 index
, bufObj
, offset
, size
);
4279 case GL_UNIFORM_BUFFER
:
4280 bind_buffer_range_uniform_buffer_err(ctx
, index
, bufObj
, offset
,
4283 case GL_SHADER_STORAGE_BUFFER
:
4284 bind_buffer_range_shader_storage_buffer_err(ctx
, index
, bufObj
,
4287 case GL_ATOMIC_COUNTER_BUFFER
:
4288 bind_atomic_buffer_err(ctx
, index
, bufObj
, offset
, size
,
4289 "glBindBufferRange");
4292 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
4299 _mesa_BindBufferRange_no_error(GLenum target
, GLuint index
, GLuint buffer
,
4300 GLintptr offset
, GLsizeiptr size
)
4302 bind_buffer_range(target
, index
, buffer
, offset
, size
, true);
4306 _mesa_BindBufferRange(GLenum target
, GLuint index
,
4307 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
4309 bind_buffer_range(target
, index
, buffer
, offset
, size
, false);
4313 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
4315 GET_CURRENT_CONTEXT(ctx
);
4316 struct gl_buffer_object
*bufObj
;
4318 if (MESA_VERBOSE
& VERBOSE_API
) {
4319 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
4320 _mesa_enum_to_string(target
), index
, buffer
);
4324 bufObj
= ctx
->Shared
->NullBufferObj
;
4326 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4327 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4328 &bufObj
, "glBindBufferBase"))
4333 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4334 "glBindBufferBase(invalid buffer=%u)", buffer
);
4338 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4339 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4341 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4342 * zero and size equal to the size of buffer."
4344 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4346 * "If the parameter (starting offset or size) was not specified when the
4347 * buffer object was bound, zero is returned."
4349 * What happens if the size of the buffer changes? Does the size of the
4350 * buffer at the moment glBindBufferBase was called still play a role, like
4351 * the first quote would imply, or is the size meaningless in the
4352 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4353 * core spec page 45 says:
4355 * "It is equivalent to calling BindBufferRange with offset zero, while
4356 * size is determined by the size of the bound buffer at the time the
4359 * My interpretation is that the GL 4.1 spec was a clarification of the
4360 * behavior, not a change. In particular, this choice will only make
4361 * rendering work in cases where it would have had undefined results.
4365 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4366 _mesa_bind_buffer_base_transform_feedback(ctx
,
4367 ctx
->TransformFeedback
.CurrentObject
,
4368 index
, bufObj
, false);
4370 case GL_UNIFORM_BUFFER
:
4371 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4373 case GL_SHADER_STORAGE_BUFFER
:
4374 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4376 case GL_ATOMIC_COUNTER_BUFFER
:
4377 bind_atomic_buffer_err(ctx
, index
, bufObj
, 0, 0,
4378 "glBindBufferBase");
4381 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4387 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4388 const GLuint
*buffers
,
4389 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4391 GET_CURRENT_CONTEXT(ctx
);
4393 if (MESA_VERBOSE
& VERBOSE_API
) {
4394 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4395 _mesa_enum_to_string(target
), first
, count
,
4396 buffers
, offsets
, sizes
);
4400 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4401 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4402 "glBindBuffersRange");
4404 case GL_UNIFORM_BUFFER
:
4405 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4406 "glBindBuffersRange");
4408 case GL_SHADER_STORAGE_BUFFER
:
4409 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4410 "glBindBuffersRange");
4412 case GL_ATOMIC_COUNTER_BUFFER
:
4413 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4414 "glBindBuffersRange");
4417 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4418 _mesa_enum_to_string(target
));
4424 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4425 const GLuint
*buffers
)
4427 GET_CURRENT_CONTEXT(ctx
);
4429 if (MESA_VERBOSE
& VERBOSE_API
) {
4430 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4431 _mesa_enum_to_string(target
), first
, count
, buffers
);
4435 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4436 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4437 "glBindBuffersBase");
4439 case GL_UNIFORM_BUFFER
:
4440 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4441 "glBindBuffersBase");
4443 case GL_SHADER_STORAGE_BUFFER
:
4444 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4445 "glBindBuffersBase");
4447 case GL_ATOMIC_COUNTER_BUFFER
:
4448 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4449 "glBindBuffersBase");
4452 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4453 _mesa_enum_to_string(target
));
4458 static ALWAYS_INLINE
void
4459 invalidate_buffer_subdata(struct gl_context
*ctx
,
4460 struct gl_buffer_object
*bufObj
, GLintptr offset
,
4463 if (ctx
->Driver
.InvalidateBufferSubData
)
4464 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4468 _mesa_InvalidateBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
4471 GET_CURRENT_CONTEXT(ctx
);
4473 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4474 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4478 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4481 GET_CURRENT_CONTEXT(ctx
);
4482 struct gl_buffer_object
*bufObj
;
4483 const GLintptr end
= offset
+ length
;
4485 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4486 * Profile) spec says:
4488 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4489 * name of an existing buffer object."
4491 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4492 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4493 _mesa_error(ctx
, GL_INVALID_VALUE
,
4494 "glInvalidateBufferSubData(name = %u) invalid object",
4499 /* The GL_ARB_invalidate_subdata spec says:
4501 * "An INVALID_VALUE error is generated if <offset> or <length> is
4502 * negative, or if <offset> + <length> is greater than the value of
4505 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
4506 _mesa_error(ctx
, GL_INVALID_VALUE
,
4507 "glInvalidateBufferSubData(invalid offset or length)");
4511 /* The OpenGL 4.4 (Core Profile) spec says:
4513 * "An INVALID_OPERATION error is generated if buffer is currently
4514 * mapped by MapBuffer or if the invalidate range intersects the range
4515 * currently mapped by MapBufferRange, unless it was mapped
4516 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4518 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4519 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4520 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4521 "glInvalidateBufferSubData(intersection with mapped "
4526 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4530 _mesa_InvalidateBufferData_no_error(GLuint buffer
)
4532 GET_CURRENT_CONTEXT(ctx
);
4534 struct gl_buffer_object
*bufObj
=_mesa_lookup_bufferobj(ctx
, buffer
);
4535 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4539 _mesa_InvalidateBufferData(GLuint buffer
)
4541 GET_CURRENT_CONTEXT(ctx
);
4542 struct gl_buffer_object
*bufObj
;
4544 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4545 * Profile) spec says:
4547 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4548 * name of an existing buffer object."
4550 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4551 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4552 _mesa_error(ctx
, GL_INVALID_VALUE
,
4553 "glInvalidateBufferData(name = %u) invalid object",
4558 /* The OpenGL 4.4 (Core Profile) spec says:
4560 * "An INVALID_OPERATION error is generated if buffer is currently
4561 * mapped by MapBuffer or if the invalidate range intersects the range
4562 * currently mapped by MapBufferRange, unless it was mapped
4563 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4565 if (_mesa_check_disallowed_mapping(bufObj
)) {
4566 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4567 "glInvalidateBufferData(intersection with mapped "
4572 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4576 buffer_page_commitment(struct gl_context
*ctx
,
4577 struct gl_buffer_object
*bufferObj
,
4578 GLintptr offset
, GLsizeiptr size
,
4579 GLboolean commit
, const char *func
)
4581 if (!(bufferObj
->StorageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)) {
4582 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(not a sparse buffer object)",
4587 if (size
< 0 || size
> bufferObj
->Size
||
4588 offset
< 0 || offset
> bufferObj
->Size
- size
) {
4589 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(out of bounds)",
4594 /* The GL_ARB_sparse_buffer extension specification says:
4596 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4597 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4598 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4599 * not extend to the end of the buffer's data store."
4601 if (offset
% ctx
->Const
.SparseBufferPageSize
!= 0) {
4602 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset not aligned to page size)",
4607 if (size
% ctx
->Const
.SparseBufferPageSize
!= 0 &&
4608 offset
+ size
!= bufferObj
->Size
) {
4609 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size not aligned to page size)",
4614 ctx
->Driver
.BufferPageCommitment(ctx
, bufferObj
, offset
, size
, commit
);
4618 _mesa_BufferPageCommitmentARB(GLenum target
, GLintptr offset
, GLsizeiptr size
,
4621 GET_CURRENT_CONTEXT(ctx
);
4622 struct gl_buffer_object
*bufferObj
;
4624 bufferObj
= get_buffer(ctx
, "glBufferPageCommitmentARB", target
,
4629 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4630 "glBufferPageCommitmentARB");
4634 _mesa_NamedBufferPageCommitmentARB(GLuint buffer
, GLintptr offset
,
4635 GLsizeiptr size
, GLboolean commit
)
4637 GET_CURRENT_CONTEXT(ctx
);
4638 struct gl_buffer_object
*bufferObj
;
4640 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4641 if (!bufferObj
|| bufferObj
== &DummyBufferObject
) {
4642 /* Note: the extension spec is not clear about the excpected error value. */
4643 _mesa_error(ctx
, GL_INVALID_VALUE
,
4644 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4649 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4650 "glNamedBufferPageCommitmentARB");