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"
51 /*#define BOUNDS_CHECK*/
55 * We count the number of buffer modification calls to check for
56 * inefficient buffer use. This is the number of such calls before we
59 #define BUFFER_WARNING_CALL_COUNT 4
63 * Helper to warn of possible performance issues, such as frequently
64 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
68 buffer_usage_warning(struct gl_context
*ctx
, GLuint
*id
, const char *fmt
, ...)
73 _mesa_gl_vdebug(ctx
, id
,
74 MESA_DEBUG_SOURCE_API
,
75 MESA_DEBUG_TYPE_PERFORMANCE
,
76 MESA_DEBUG_SEVERITY_MEDIUM
,
81 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
83 static GLuint id = 0; \
84 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
89 * Used as a placeholder for buffer objects between glGenBuffers() and
90 * glBindBuffer() so that glIsBuffer() can work correctly.
92 static struct gl_buffer_object DummyBufferObject
;
96 * Return pointer to address of a buffer object target.
97 * \param ctx the GL context
98 * \param target the buffer object target to be retrieved.
99 * \return pointer to pointer to the buffer object bound to \c target in the
100 * specified context or \c NULL if \c target is invalid.
102 static inline struct gl_buffer_object
**
103 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
105 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
107 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
108 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
112 case GL_ARRAY_BUFFER_ARB
:
113 return &ctx
->Array
.ArrayBufferObj
;
114 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
115 return &ctx
->Array
.VAO
->IndexBufferObj
;
116 case GL_PIXEL_PACK_BUFFER_EXT
:
117 return &ctx
->Pack
.BufferObj
;
118 case GL_PIXEL_UNPACK_BUFFER_EXT
:
119 return &ctx
->Unpack
.BufferObj
;
120 case GL_COPY_READ_BUFFER
:
121 return &ctx
->CopyReadBuffer
;
122 case GL_COPY_WRITE_BUFFER
:
123 return &ctx
->CopyWriteBuffer
;
124 case GL_DRAW_INDIRECT_BUFFER
:
125 if ((ctx
->API
== API_OPENGL_CORE
&&
126 ctx
->Extensions
.ARB_draw_indirect
) ||
127 _mesa_is_gles31(ctx
)) {
128 return &ctx
->DrawIndirectBuffer
;
131 case GL_PARAMETER_BUFFER_ARB
:
132 if (_mesa_has_ARB_indirect_parameters(ctx
)) {
133 return &ctx
->ParameterBuffer
;
136 case GL_DISPATCH_INDIRECT_BUFFER
:
137 if (_mesa_has_compute_shaders(ctx
)) {
138 return &ctx
->DispatchIndirectBuffer
;
141 case GL_TRANSFORM_FEEDBACK_BUFFER
:
142 if (ctx
->Extensions
.EXT_transform_feedback
) {
143 return &ctx
->TransformFeedback
.CurrentBuffer
;
146 case GL_TEXTURE_BUFFER
:
147 if (ctx
->API
== API_OPENGL_CORE
&&
148 ctx
->Extensions
.ARB_texture_buffer_object
) {
149 return &ctx
->Texture
.BufferObject
;
152 case GL_UNIFORM_BUFFER
:
153 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
154 return &ctx
->UniformBuffer
;
157 case GL_SHADER_STORAGE_BUFFER
:
158 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
159 return &ctx
->ShaderStorageBuffer
;
162 case GL_ATOMIC_COUNTER_BUFFER
:
163 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
164 return &ctx
->AtomicBuffer
;
167 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
168 if (ctx
->Extensions
.AMD_pinned_memory
) {
169 return &ctx
->ExternalVirtualMemoryBuffer
;
180 * Get the buffer object bound to the specified target in a GL context.
181 * \param ctx the GL context
182 * \param target the buffer object target to be retrieved.
183 * \param error the GL error to record if target is illegal.
184 * \return pointer to the buffer object bound to \c target in the
185 * specified context or \c NULL if \c target is invalid.
187 static inline struct gl_buffer_object
*
188 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
191 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
194 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
198 if (!_mesa_is_bufferobj(*bufObj
)) {
199 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
208 * Convert a GLbitfield describing the mapped buffer access flags
209 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
212 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
214 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
215 if ((access
& rwFlags
) == rwFlags
)
216 return GL_READ_WRITE
;
217 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
219 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
220 return GL_WRITE_ONLY
;
222 /* Otherwise, AccessFlags is zero (the default state).
224 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
226 * Name Type Initial Value Legal Values
228 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
231 * However, table 6.8 in the GL_OES_mapbuffer extension says:
233 * Get Value Type Get Command Value Description
234 * --------- ---- ----------- ----- -----------
235 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
237 * The difference is because GL_OES_mapbuffer only supports mapping buffers
242 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
247 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
249 * The regions do not overlap if and only if the end of the given
250 * region is before the mapped region or the start of the given region
251 * is after the mapped region.
253 * \param obj Buffer object target on which to operate.
254 * \param offset Offset of the first byte of the subdata range.
255 * \param size Size, in bytes, of the subdata range.
256 * \return true if ranges overlap, false otherwise
260 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
261 GLintptr offset
, GLsizeiptr size
)
263 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
264 const GLintptr end
= offset
+ size
;
265 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
266 obj
->Mappings
[MAP_USER
].Length
;
268 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
277 * Tests the subdata range parameters and sets the GL error code for
278 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
279 * \c glClearBufferSubData.
281 * \param ctx GL context.
282 * \param bufObj The buffer object.
283 * \param offset Offset of the first byte of the subdata range.
284 * \param size Size, in bytes, of the subdata range.
285 * \param mappedRange If true, checks if an overlapping range is mapped.
286 * If false, checks if buffer is mapped.
287 * \param caller Name of calling function for recording errors.
288 * \return false if error, true otherwise
290 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
293 buffer_object_subdata_range_good(struct gl_context
*ctx
,
294 const struct gl_buffer_object
*bufObj
,
295 GLintptr offset
, GLsizeiptr size
,
296 bool mappedRange
, const char *caller
)
299 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
304 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
308 if (offset
+ size
> bufObj
->Size
) {
309 _mesa_error(ctx
, GL_INVALID_VALUE
,
310 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
311 (unsigned long) offset
,
312 (unsigned long) size
,
313 (unsigned long) bufObj
->Size
);
317 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
321 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
322 _mesa_error(ctx
, GL_INVALID_OPERATION
,
323 "%s(range is mapped without persistent bit)",
329 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
330 _mesa_error(ctx
, GL_INVALID_OPERATION
,
331 "%s(buffer is mapped without persistent bit)",
342 * Test the format and type parameters and set the GL error code for
343 * \c glClearBufferData and \c glClearBufferSubData.
345 * \param ctx GL context.
346 * \param internalformat Format to which the data is to be converted.
347 * \param format Format of the supplied data.
348 * \param type Type of the supplied data.
349 * \param caller Name of calling function for recording errors.
350 * \return If internalformat, format and type are legal the mesa_format
351 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
353 * \sa glClearBufferData and glClearBufferSubData
356 validate_clear_buffer_format(struct gl_context
*ctx
,
357 GLenum internalformat
,
358 GLenum format
, GLenum type
,
361 mesa_format mesaFormat
;
362 GLenum errorFormatType
;
364 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
365 if (mesaFormat
== MESA_FORMAT_NONE
) {
366 _mesa_error(ctx
, GL_INVALID_ENUM
,
367 "%s(invalid internalformat)", caller
);
368 return MESA_FORMAT_NONE
;
371 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
372 * EXT_texture_integer there is no conversion between integer and
373 * non-integer formats
375 if (_mesa_is_enum_format_signed_int(format
) !=
376 _mesa_is_format_integer_color(mesaFormat
)) {
377 _mesa_error(ctx
, GL_INVALID_OPERATION
,
378 "%s(integer vs non-integer)", caller
);
379 return MESA_FORMAT_NONE
;
382 if (!_mesa_is_color_format(format
)) {
383 _mesa_error(ctx
, GL_INVALID_ENUM
,
384 "%s(format is not a color format)", caller
);
385 return MESA_FORMAT_NONE
;
388 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
389 if (errorFormatType
!= GL_NO_ERROR
) {
390 _mesa_error(ctx
, GL_INVALID_ENUM
,
391 "%s(invalid format or type)", caller
);
392 return MESA_FORMAT_NONE
;
400 * Convert user-specified clear value to the specified internal format.
402 * \param ctx GL context.
403 * \param internalformat Format to which the data is converted.
404 * \param clearValue Points to the converted clear value.
405 * \param format Format of the supplied data.
406 * \param type Type of the supplied data.
407 * \param data Data which is to be converted to internalformat.
408 * \param caller Name of calling function for recording errors.
409 * \return true if data could be converted, false otherwise.
411 * \sa glClearBufferData, glClearBufferSubData
414 convert_clear_buffer_data(struct gl_context
*ctx
,
415 mesa_format internalformat
,
416 GLubyte
*clearValue
, GLenum format
, GLenum type
,
417 const GLvoid
*data
, const char *caller
)
419 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
421 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
422 0, &clearValue
, 1, 1, 1,
423 format
, type
, data
, &ctx
->Unpack
)) {
427 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
434 * Allocate and initialize a new buffer object.
436 * Default callback for the \c dd_function_table::NewBufferObject() hook.
438 static struct gl_buffer_object
*
439 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
441 struct gl_buffer_object
*obj
;
445 obj
= MALLOC_STRUCT(gl_buffer_object
);
446 _mesa_initialize_buffer_object(ctx
, obj
, name
);
452 * Delete a buffer object.
454 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
457 _mesa_delete_buffer_object(struct gl_context
*ctx
,
458 struct gl_buffer_object
*bufObj
)
462 vbo_delete_minmax_cache(bufObj
);
463 _mesa_align_free(bufObj
->Data
);
465 /* assign strange values here to help w/ debugging */
466 bufObj
->RefCount
= -1000;
469 mtx_destroy(&bufObj
->Mutex
);
477 * Set ptr to bufObj w/ reference counting.
478 * This is normally only called from the _mesa_reference_buffer_object() macro
479 * when there's a real pointer change.
482 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
483 struct gl_buffer_object
**ptr
,
484 struct gl_buffer_object
*bufObj
)
487 /* Unreference the old buffer */
488 GLboolean deleteFlag
= GL_FALSE
;
489 struct gl_buffer_object
*oldObj
= *ptr
;
491 mtx_lock(&oldObj
->Mutex
);
492 assert(oldObj
->RefCount
> 0);
494 deleteFlag
= (oldObj
->RefCount
== 0);
495 mtx_unlock(&oldObj
->Mutex
);
498 assert(ctx
->Driver
.DeleteBuffer
);
499 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
507 /* reference new buffer */
508 mtx_lock(&bufObj
->Mutex
);
509 if (bufObj
->RefCount
== 0) {
510 /* this buffer's being deleted (look just above) */
511 /* Not sure this can every really happen. Warn if it does. */
512 _mesa_problem(NULL
, "referencing deleted buffer object");
519 mtx_unlock(&bufObj
->Mutex
);
525 * Get the value of MESA_NO_MINMAX_CACHE.
528 get_no_minmax_cache()
530 static bool read
= false;
531 static bool disable
= false;
534 disable
= env_var_as_boolean("MESA_NO_MINMAX_CACHE", false);
543 * Initialize a buffer object to default values.
546 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
547 struct gl_buffer_object
*obj
,
550 memset(obj
, 0, sizeof(struct gl_buffer_object
));
551 mtx_init(&obj
->Mutex
, mtx_plain
);
554 obj
->Usage
= GL_STATIC_DRAW_ARB
;
556 if (get_no_minmax_cache())
557 obj
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
563 * Callback called from _mesa_HashWalk()
566 count_buffer_size(GLuint key
, void *data
, void *userData
)
568 const struct gl_buffer_object
*bufObj
=
569 (const struct gl_buffer_object
*) data
;
570 GLuint
*total
= (GLuint
*) userData
;
573 *total
= *total
+ bufObj
->Size
;
578 * Compute total size (in bytes) of all buffer objects for the given context.
579 * For debugging purposes.
582 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
586 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
593 * Allocate space for and store data in a buffer object. Any data that was
594 * previously stored in the buffer object is lost. If \c data is \c NULL,
595 * memory will be allocated, but no copy will occur.
597 * This is the default callback for \c dd_function_table::BufferData()
598 * Note that all GL error checking will have been done already.
600 * \param ctx GL context.
601 * \param target Buffer object target on which to operate.
602 * \param size Size, in bytes, of the new data store.
603 * \param data Pointer to the data to store in the buffer object. This
604 * pointer may be \c NULL.
605 * \param usage Hints about how the data will be used.
606 * \param bufObj Object to be used.
608 * \return GL_TRUE for success, GL_FALSE for failure
609 * \sa glBufferDataARB, dd_function_table::BufferData.
612 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptr size
,
613 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
614 struct gl_buffer_object
*bufObj
)
620 _mesa_align_free( bufObj
->Data
);
622 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
624 bufObj
->Data
= (GLubyte
*) new_data
;
626 bufObj
->Usage
= usage
;
627 bufObj
->StorageFlags
= storageFlags
;
630 memcpy( bufObj
->Data
, data
, size
);
642 * Replace data in a subrange of buffer object. If the data range
643 * specified by \c size + \c offset extends beyond the end of the buffer or
644 * if \c data is \c NULL, no copy is performed.
646 * This is the default callback for \c dd_function_table::BufferSubData()
647 * Note that all GL error checking will have been done already.
649 * \param ctx GL context.
650 * \param offset Offset of the first byte to be modified.
651 * \param size Size, in bytes, of the data range.
652 * \param data Pointer to the data to store in the buffer object.
653 * \param bufObj Object to be used.
655 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
658 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptr offset
,
659 GLsizeiptr size
, const GLvoid
*data
,
660 struct gl_buffer_object
*bufObj
)
664 /* this should have been caught in _mesa_BufferSubData() */
665 assert(size
+ offset
<= bufObj
->Size
);
668 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
674 * Retrieve data from a subrange of buffer object. If the data range
675 * specified by \c size + \c offset extends beyond the end of the buffer or
676 * if \c data is \c NULL, no copy is performed.
678 * This is the default callback for \c dd_function_table::GetBufferSubData()
679 * Note that all GL error checking will have been done already.
681 * \param ctx GL context.
682 * \param target Buffer object target on which to operate.
683 * \param offset Offset of the first byte to be fetched.
684 * \param size Size, in bytes, of the data range.
685 * \param data Destination for data
686 * \param bufObj Object to be used.
688 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
691 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
692 GLsizeiptrARB size
, GLvoid
* data
,
693 struct gl_buffer_object
* bufObj
)
697 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
698 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
704 * Clear a subrange of the buffer object with copies of the supplied data.
705 * If data is NULL the buffer is filled with zeros.
707 * This is the default callback for \c dd_function_table::ClearBufferSubData()
708 * Note that all GL error checking will have been done already.
710 * \param ctx GL context.
711 * \param offset Offset of the first byte to be cleared.
712 * \param size Size, in bytes, of the to be cleared range.
713 * \param clearValue Source of the data.
714 * \param clearValueSize Size, in bytes, of the supplied data.
715 * \param bufObj Object to be cleared.
717 * \sa glClearBufferSubData, glClearBufferData and
718 * dd_function_table::ClearBufferSubData.
721 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
722 GLintptr offset
, GLsizeiptr size
,
723 const GLvoid
*clearValue
,
724 GLsizeiptr clearValueSize
,
725 struct gl_buffer_object
*bufObj
)
730 assert(ctx
->Driver
.MapBufferRange
);
731 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
733 GL_MAP_INVALIDATE_RANGE_BIT
,
734 bufObj
, MAP_INTERNAL
);
737 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
741 if (clearValue
== NULL
) {
742 /* Clear with zeros, per the spec */
743 memset(dest
, 0, size
);
744 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
748 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
749 memcpy(dest
, clearValue
, clearValueSize
);
750 dest
+= clearValueSize
;
753 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
758 * Default fallback for \c dd_function_table::MapBufferRange().
759 * Called via glMapBufferRange().
762 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
763 GLsizeiptr length
, GLbitfield access
,
764 struct gl_buffer_object
*bufObj
,
765 gl_map_buffer_index index
)
768 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
769 /* Just return a direct pointer to the data */
770 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
771 bufObj
->Mappings
[index
].Length
= length
;
772 bufObj
->Mappings
[index
].Offset
= offset
;
773 bufObj
->Mappings
[index
].AccessFlags
= access
;
774 return bufObj
->Mappings
[index
].Pointer
;
779 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
780 * Called via glFlushMappedBufferRange().
783 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
784 GLintptr offset
, GLsizeiptr length
,
785 struct gl_buffer_object
*obj
,
786 gl_map_buffer_index index
)
798 * Default callback for \c dd_function_table::UnmapBuffer().
800 * The input parameters will have been already tested for errors.
802 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
805 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
806 gl_map_buffer_index index
)
809 /* XXX we might assert here that bufObj->Pointer is non-null */
810 bufObj
->Mappings
[index
].Pointer
= NULL
;
811 bufObj
->Mappings
[index
].Length
= 0;
812 bufObj
->Mappings
[index
].Offset
= 0;
813 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
819 * Default fallback for \c dd_function_table::CopyBufferSubData().
820 * Called via glCopyBufferSubData().
823 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
824 struct gl_buffer_object
*src
,
825 struct gl_buffer_object
*dst
,
826 GLintptr readOffset
, GLintptr writeOffset
,
829 GLubyte
*srcPtr
, *dstPtr
;
832 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
834 GL_MAP_WRITE_BIT
, src
,
840 srcPtr
+= readOffset
;
841 dstPtr
+= writeOffset
;
843 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
844 GL_MAP_READ_BIT
, src
,
846 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
848 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
852 /* Note: the src and dst regions will never overlap. Trying to do so
853 * would generate GL_INVALID_VALUE earlier.
855 if (srcPtr
&& dstPtr
)
856 memcpy(dstPtr
, srcPtr
, size
);
858 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
860 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
866 * Initialize the state associated with buffer objects
869 _mesa_init_buffer_objects( struct gl_context
*ctx
)
873 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
874 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
875 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
877 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
878 ctx
->Shared
->NullBufferObj
);
880 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
881 ctx
->Shared
->NullBufferObj
);
882 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
883 ctx
->Shared
->NullBufferObj
);
885 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
886 ctx
->Shared
->NullBufferObj
);
888 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
889 ctx
->Shared
->NullBufferObj
);
891 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
892 ctx
->Shared
->NullBufferObj
);
894 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
895 ctx
->Shared
->NullBufferObj
);
897 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
,
898 ctx
->Shared
->NullBufferObj
);
900 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
,
901 ctx
->Shared
->NullBufferObj
);
903 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
904 _mesa_reference_buffer_object(ctx
,
905 &ctx
->UniformBufferBindings
[i
].BufferObject
,
906 ctx
->Shared
->NullBufferObj
);
907 ctx
->UniformBufferBindings
[i
].Offset
= -1;
908 ctx
->UniformBufferBindings
[i
].Size
= -1;
911 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
912 _mesa_reference_buffer_object(ctx
,
913 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
914 ctx
->Shared
->NullBufferObj
);
915 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
916 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
919 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
920 _mesa_reference_buffer_object(ctx
,
921 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
922 ctx
->Shared
->NullBufferObj
);
923 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
924 ctx
->AtomicBufferBindings
[i
].Size
= 0;
930 _mesa_free_buffer_objects( struct gl_context
*ctx
)
934 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
936 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
937 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
939 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
941 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
943 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
945 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
947 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
, NULL
);
949 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
951 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
952 _mesa_reference_buffer_object(ctx
,
953 &ctx
->UniformBufferBindings
[i
].BufferObject
,
957 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
958 _mesa_reference_buffer_object(ctx
,
959 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
963 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
964 _mesa_reference_buffer_object(ctx
,
965 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
972 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
974 struct gl_buffer_object
**buf_handle
,
977 struct gl_buffer_object
*buf
= *buf_handle
;
979 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
)) {
980 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
984 if (!buf
|| buf
== &DummyBufferObject
) {
985 /* If this is a new buffer object id, or one which was generated but
986 * never used before, allocate a buffer object now.
988 assert(ctx
->Driver
.NewBufferObject
);
989 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
991 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
994 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
1002 * Bind the specified target to buffer for the specified context.
1003 * Called by glBindBuffer() and other functions.
1006 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
1008 struct gl_buffer_object
*oldBufObj
;
1009 struct gl_buffer_object
*newBufObj
= NULL
;
1010 struct gl_buffer_object
**bindTarget
= NULL
;
1012 bindTarget
= get_buffer_target(ctx
, target
);
1014 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
1018 /* Get pointer to old buffer object (to be unbound) */
1019 oldBufObj
= *bindTarget
;
1020 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
1021 return; /* rebinding the same buffer object- no change */
1024 * Get pointer to new buffer object (newBufObj)
1027 /* The spec says there's not a buffer object named 0, but we use
1028 * one internally because it simplifies things.
1030 newBufObj
= ctx
->Shared
->NullBufferObj
;
1033 /* non-default buffer object */
1034 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1035 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1036 &newBufObj
, "glBindBuffer"))
1040 /* record usage history */
1042 case GL_PIXEL_PACK_BUFFER
:
1043 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1049 /* bind new buffer */
1050 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1055 * Update the default buffer objects in the given context to reference those
1056 * specified in the shared state and release those referencing the old
1060 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1062 /* Bind the NullBufferObj to remove references to those
1063 * in the shared context hash table.
1065 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
1066 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
1067 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
1068 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1074 * Return the gl_buffer_object for the given ID.
1075 * Always return NULL for ID 0.
1077 struct gl_buffer_object
*
1078 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1083 return (struct gl_buffer_object
*)
1084 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1088 struct gl_buffer_object
*
1089 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1091 return (struct gl_buffer_object
*)
1092 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1096 * A convenience function for direct state access functions that throws
1097 * GL_INVALID_OPERATION if buffer is not the name of an existing
1100 struct gl_buffer_object
*
1101 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1104 struct gl_buffer_object
*bufObj
;
1106 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1107 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1108 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1109 "%s(non-existent buffer object %u)", caller
, buffer
);
1118 _mesa_begin_bufferobj_lookups(struct gl_context
*ctx
)
1120 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1125 _mesa_end_bufferobj_lookups(struct gl_context
*ctx
)
1127 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1132 * Look up a buffer object for a multi-bind function.
1134 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1135 * of generating an error if the buffer ID is not zero or the name
1136 * of an existing buffer object.
1138 * If the buffer ID refers to an existing buffer object, a pointer
1139 * to the buffer object is returned. If the ID is zero, a pointer
1140 * to the shared NullBufferObj is returned. If the ID is not zero
1141 * and does not refer to a valid buffer object, this function
1144 * This function assumes that the caller has already locked the
1145 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1147 struct gl_buffer_object
*
1148 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1149 const GLuint
*buffers
,
1150 GLuint index
, const char *caller
)
1152 struct gl_buffer_object
*bufObj
;
1154 if (buffers
[index
] != 0) {
1155 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1157 /* The multi-bind functions don't create the buffer objects
1158 when they don't exist. */
1159 if (bufObj
== &DummyBufferObject
)
1162 bufObj
= ctx
->Shared
->NullBufferObj
;
1165 /* The ARB_multi_bind spec says:
1167 * "An INVALID_OPERATION error is generated if any value
1168 * in <buffers> is not zero or the name of an existing
1169 * buffer object (per binding)."
1171 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1172 "%s(buffers[%u]=%u is not zero or the name "
1173 "of an existing buffer object)",
1174 caller
, index
, buffers
[index
]);
1182 * If *ptr points to obj, set ptr = the Null/default buffer object.
1183 * This is a helper for buffer object deletion.
1184 * The GL spec says that deleting a buffer object causes it to get
1185 * unbound from all arrays in the current context.
1188 unbind(struct gl_context
*ctx
,
1189 struct gl_buffer_object
**ptr
,
1190 struct gl_buffer_object
*obj
)
1193 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
1199 * Plug default/fallback buffer object functions into the device
1203 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1205 /* GL_ARB_vertex/pixel_buffer_object */
1206 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1207 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1208 driver
->BufferData
= buffer_data_fallback
;
1209 driver
->BufferSubData
= buffer_sub_data_fallback
;
1210 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1211 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1213 /* GL_ARB_clear_buffer_object */
1214 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1216 /* GL_ARB_map_buffer_range */
1217 driver
->MapBufferRange
= map_buffer_range_fallback
;
1218 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1220 /* GL_ARB_copy_buffer */
1221 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1226 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1227 struct gl_buffer_object
*bufObj
)
1231 for (i
= 0; i
< MAP_COUNT
; i
++) {
1232 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1233 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1234 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1235 bufObj
->Mappings
[i
].AccessFlags
= 0;
1241 /**********************************************************************/
1243 /**********************************************************************/
1246 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1248 GET_CURRENT_CONTEXT(ctx
);
1250 if (MESA_VERBOSE
& VERBOSE_API
) {
1251 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1252 _mesa_enum_to_string(target
), buffer
);
1255 bind_buffer_object(ctx
, target
, buffer
);
1260 * Delete a set of buffer objects.
1262 * \param n Number of buffer objects to delete.
1263 * \param ids Array of \c n buffer object IDs.
1266 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1268 GET_CURRENT_CONTEXT(ctx
);
1270 FLUSH_VERTICES(ctx
, 0);
1273 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1277 mtx_lock(&ctx
->Shared
->Mutex
);
1279 for (i
= 0; i
< n
; i
++) {
1280 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
1282 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1285 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1287 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1289 /* unbind any vertex pointers bound to this buffer */
1290 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1291 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1294 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1295 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1297 if (vao
->IndexBufferObj
== bufObj
) {
1298 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1301 /* unbind ARB_draw_indirect binding point */
1302 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1303 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1306 /* unbind ARB_indirect_parameters binding point */
1307 if (ctx
->ParameterBuffer
== bufObj
) {
1308 _mesa_BindBuffer(GL_PARAMETER_BUFFER_ARB
, 0);
1311 /* unbind ARB_compute_shader binding point */
1312 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1313 _mesa_BindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0);
1316 /* unbind ARB_copy_buffer binding points */
1317 if (ctx
->CopyReadBuffer
== bufObj
) {
1318 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1320 if (ctx
->CopyWriteBuffer
== bufObj
) {
1321 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1324 /* unbind transform feedback binding points */
1325 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1326 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1328 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1329 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1330 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1334 /* unbind UBO binding points */
1335 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1336 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1337 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1341 if (ctx
->UniformBuffer
== bufObj
) {
1342 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1345 /* unbind SSBO binding points */
1346 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1347 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1348 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1352 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1353 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER
, 0);
1356 /* unbind Atomci Buffer binding points */
1357 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1358 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1359 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1363 if (ctx
->AtomicBuffer
== bufObj
) {
1364 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1367 /* unbind any pixel pack/unpack pointers bound to this buffer */
1368 if (ctx
->Pack
.BufferObj
== bufObj
) {
1369 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1371 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1372 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1375 if (ctx
->Texture
.BufferObject
== bufObj
) {
1376 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1379 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1380 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1383 /* The ID is immediately freed for re-use */
1384 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1385 /* Make sure we do not run into the classic ABA problem on bind.
1386 * We don't want to allow re-binding a buffer object that's been
1387 * "deleted" by glDeleteBuffers().
1389 * The explicit rebinding to the default object in the current context
1390 * prevents the above in the current context, but another context
1391 * sharing the same objects might suffer from this problem.
1392 * The alternative would be to do the hash lookup in any case on bind
1393 * which would introduce more runtime overhead than this.
1395 bufObj
->DeletePending
= GL_TRUE
;
1396 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1400 mtx_unlock(&ctx
->Shared
->Mutex
);
1405 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1406 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1410 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1412 GET_CURRENT_CONTEXT(ctx
);
1415 struct gl_buffer_object
*buf
;
1417 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1419 if (MESA_VERBOSE
& VERBOSE_API
)
1420 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1423 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1432 * This must be atomic (generation and allocation of buffer object IDs)
1434 mtx_lock(&ctx
->Shared
->Mutex
);
1436 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1438 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1439 * DummyBufferObject. Otherwise, create a new buffer object and insert
1442 for (i
= 0; i
< n
; i
++) {
1443 buffers
[i
] = first
+ i
;
1445 assert(ctx
->Driver
.NewBufferObject
);
1446 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1448 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1449 mtx_unlock(&ctx
->Shared
->Mutex
);
1454 buf
= &DummyBufferObject
;
1456 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1459 mtx_unlock(&ctx
->Shared
->Mutex
);
1463 * Generate a set of unique buffer object IDs and store them in \c buffers.
1465 * \param n Number of IDs to generate.
1466 * \param buffers Array of \c n locations to store the IDs.
1469 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1471 create_buffers(n
, buffers
, false);
1475 * Create a set of buffer objects and store their unique IDs in \c buffers.
1477 * \param n Number of IDs to generate.
1478 * \param buffers Array of \c n locations to store the IDs.
1481 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1483 create_buffers(n
, buffers
, true);
1488 * Determine if ID is the name of a buffer object.
1490 * \param id ID of the potential buffer object.
1491 * \return \c GL_TRUE if \c id is the name of a buffer object,
1492 * \c GL_FALSE otherwise.
1494 GLboolean GLAPIENTRY
1495 _mesa_IsBuffer(GLuint id
)
1497 struct gl_buffer_object
*bufObj
;
1498 GET_CURRENT_CONTEXT(ctx
);
1499 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1501 mtx_lock(&ctx
->Shared
->Mutex
);
1502 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1503 mtx_unlock(&ctx
->Shared
->Mutex
);
1505 return bufObj
&& bufObj
!= &DummyBufferObject
;
1510 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1511 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1512 GLbitfield flags
, const char *func
)
1515 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1519 if (flags
& ~(GL_MAP_READ_BIT
|
1521 GL_MAP_PERSISTENT_BIT
|
1522 GL_MAP_COHERENT_BIT
|
1523 GL_DYNAMIC_STORAGE_BIT
|
1524 GL_CLIENT_STORAGE_BIT
)) {
1525 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1529 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1530 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1531 _mesa_error(ctx
, GL_INVALID_VALUE
,
1532 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1536 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1537 _mesa_error(ctx
, GL_INVALID_VALUE
,
1538 "%s(COHERENT and flags!=PERSISTENT)", func
);
1542 if (bufObj
->Immutable
) {
1543 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1547 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1548 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1550 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1552 bufObj
->Written
= GL_TRUE
;
1553 bufObj
->Immutable
= GL_TRUE
;
1554 bufObj
->MinMaxCacheDirty
= true;
1556 assert(ctx
->Driver
.BufferData
);
1557 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1559 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1560 /* Even though the interaction between AMD_pinned_memory and
1561 * glBufferStorage is not described in the spec, Graham Sellers
1562 * said that it should behave the same as glBufferData.
1564 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1567 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1573 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1576 GET_CURRENT_CONTEXT(ctx
);
1577 struct gl_buffer_object
*bufObj
;
1579 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1583 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1588 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1591 GET_CURRENT_CONTEXT(ctx
);
1592 struct gl_buffer_object
*bufObj
;
1594 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1599 * In direct state access, buffer objects have an unspecified target since
1600 * they are not required to be bound.
1602 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1603 "glNamedBufferStorage");
1608 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1609 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1610 GLenum usage
, const char *func
)
1614 if (MESA_VERBOSE
& VERBOSE_API
) {
1615 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1617 _mesa_enum_to_string(target
),
1618 (long int) size
, data
,
1619 _mesa_enum_to_string(usage
));
1623 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1628 case GL_STREAM_DRAW_ARB
:
1629 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1632 case GL_STATIC_DRAW_ARB
:
1633 case GL_DYNAMIC_DRAW_ARB
:
1637 case GL_STREAM_READ_ARB
:
1638 case GL_STREAM_COPY_ARB
:
1639 case GL_STATIC_READ_ARB
:
1640 case GL_STATIC_COPY_ARB
:
1641 case GL_DYNAMIC_READ_ARB
:
1642 case GL_DYNAMIC_COPY_ARB
:
1643 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1647 valid_usage
= false;
1652 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1653 _mesa_enum_to_string(usage
));
1657 if (bufObj
->Immutable
) {
1658 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1662 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1663 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1665 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1667 bufObj
->Written
= GL_TRUE
;
1668 bufObj
->MinMaxCacheDirty
= true;
1671 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1672 bufObj
->Name
, size
, data
, usage
);
1679 assert(ctx
->Driver
.BufferData
);
1680 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1683 GL_DYNAMIC_STORAGE_BIT
,
1685 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1686 /* From GL_AMD_pinned_memory:
1688 * INVALID_OPERATION is generated by BufferData if <target> is
1689 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1690 * mapped to the GPU address space.
1692 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1695 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1701 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1702 const GLvoid
*data
, GLenum usage
)
1704 GET_CURRENT_CONTEXT(ctx
);
1705 struct gl_buffer_object
*bufObj
;
1707 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1711 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1716 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1719 GET_CURRENT_CONTEXT(ctx
);
1720 struct gl_buffer_object
*bufObj
;
1722 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1726 /* In direct state access, buffer objects have an unspecified target since
1727 * they are not required to be bound.
1729 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1730 "glNamedBufferData");
1735 * Implementation for glBufferSubData and glNamedBufferSubData.
1737 * \param ctx GL context.
1738 * \param bufObj The buffer object.
1739 * \param offset Offset of the first byte of the subdata range.
1740 * \param size Size, in bytes, of the subdata range.
1741 * \param data The data store.
1742 * \param func Name of calling function for recording errors.
1746 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1747 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1750 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1752 /* error already recorded */
1756 if (bufObj
->Immutable
&&
1757 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1758 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1765 bufObj
->NumSubDataCalls
++;
1767 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
1768 bufObj
->Usage
== GL_STATIC_COPY
) &&
1769 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
) {
1770 /* If the application declared the buffer as static draw/copy or stream
1771 * draw, it should not be frequently modified with glBufferSubData.
1773 BUFFER_USAGE_WARNING(ctx
,
1774 "using %s(buffer %u, offset %u, size %u) to "
1775 "update a %s buffer",
1776 func
, bufObj
->Name
, offset
, size
,
1777 _mesa_enum_to_string(bufObj
->Usage
));
1780 bufObj
->Written
= GL_TRUE
;
1781 bufObj
->MinMaxCacheDirty
= true;
1783 assert(ctx
->Driver
.BufferSubData
);
1784 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1788 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1789 GLsizeiptr size
, const GLvoid
*data
)
1791 GET_CURRENT_CONTEXT(ctx
);
1792 struct gl_buffer_object
*bufObj
;
1794 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1798 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1802 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1803 GLsizeiptr size
, const GLvoid
*data
)
1805 GET_CURRENT_CONTEXT(ctx
);
1806 struct gl_buffer_object
*bufObj
;
1808 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1812 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1813 "glNamedBufferSubData");
1818 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1819 GLsizeiptr size
, GLvoid
*data
)
1821 GET_CURRENT_CONTEXT(ctx
);
1822 struct gl_buffer_object
*bufObj
;
1824 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1825 GL_INVALID_OPERATION
);
1829 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1830 "glGetBufferSubData")) {
1834 assert(ctx
->Driver
.GetBufferSubData
);
1835 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1839 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1840 GLsizeiptr size
, GLvoid
*data
)
1842 GET_CURRENT_CONTEXT(ctx
);
1843 struct gl_buffer_object
*bufObj
;
1845 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1846 "glGetNamedBufferSubData");
1850 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1851 "glGetNamedBufferSubData")) {
1855 assert(ctx
->Driver
.GetBufferSubData
);
1856 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1861 * \param subdata true if caller is *SubData, false if *Data
1864 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1865 struct gl_buffer_object
*bufObj
,
1866 GLenum internalformat
,
1867 GLintptr offset
, GLsizeiptr size
,
1868 GLenum format
, GLenum type
,
1870 const char *func
, bool subdata
)
1872 mesa_format mesaFormat
;
1873 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1874 GLsizeiptr clearValueSize
;
1876 /* This checks for disallowed mappings. */
1877 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1882 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1883 format
, type
, func
);
1885 if (mesaFormat
== MESA_FORMAT_NONE
) {
1889 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1890 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1891 _mesa_error(ctx
, GL_INVALID_VALUE
,
1892 "%s(offset or size is not a multiple of "
1893 "internalformat size)", func
);
1897 /* Bail early. Negative size has already been checked. */
1901 bufObj
->MinMaxCacheDirty
= true;
1904 /* clear to zeros, per the spec */
1905 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1906 NULL
, clearValueSize
, bufObj
);
1910 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1911 format
, type
, data
, func
)) {
1915 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1916 clearValue
, clearValueSize
, bufObj
);
1920 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1921 GLenum type
, const GLvoid
*data
)
1923 GET_CURRENT_CONTEXT(ctx
);
1924 struct gl_buffer_object
*bufObj
;
1926 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1930 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1932 "glClearBufferData", false);
1936 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1937 GLenum format
, GLenum type
, const GLvoid
*data
)
1939 GET_CURRENT_CONTEXT(ctx
);
1940 struct gl_buffer_object
*bufObj
;
1942 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1946 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1948 "glClearNamedBufferData", false);
1953 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1954 GLintptr offset
, GLsizeiptr size
,
1955 GLenum format
, GLenum type
,
1958 GET_CURRENT_CONTEXT(ctx
);
1959 struct gl_buffer_object
*bufObj
;
1961 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1965 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1967 "glClearBufferSubData", true);
1971 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1972 GLintptr offset
, GLsizeiptr size
,
1973 GLenum format
, GLenum type
,
1976 GET_CURRENT_CONTEXT(ctx
);
1977 struct gl_buffer_object
*bufObj
;
1979 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1980 "glClearNamedBufferSubData");
1984 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1986 "glClearNamedBufferSubData", true);
1991 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1994 GLboolean status
= GL_TRUE
;
1995 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1997 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
1998 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1999 "%s(buffer is not mapped)", func
);
2004 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2005 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2007 /* check that last 100 bytes are still = magic value */
2008 for (i
= 0; i
< 100; i
++) {
2009 GLuint pos
= bufObj
->Size
- i
- 1;
2010 if (buf
[pos
] != 123) {
2011 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2012 " at position %d (value = %u)\n",
2020 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2021 GLuint i
, unchanged
= 0;
2022 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2024 /* check which bytes changed */
2025 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2026 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2033 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2034 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2039 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2040 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2041 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2042 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2043 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2048 GLboolean GLAPIENTRY
2049 _mesa_UnmapBuffer(GLenum target
)
2051 GET_CURRENT_CONTEXT(ctx
);
2052 struct gl_buffer_object
*bufObj
;
2054 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2058 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2061 GLboolean GLAPIENTRY
2062 _mesa_UnmapNamedBuffer(GLuint buffer
)
2064 GET_CURRENT_CONTEXT(ctx
);
2065 struct gl_buffer_object
*bufObj
;
2067 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2071 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2076 get_buffer_parameter(struct gl_context
*ctx
,
2077 struct gl_buffer_object
*bufObj
, GLenum pname
,
2078 GLint64
*params
, const char *func
)
2081 case GL_BUFFER_SIZE_ARB
:
2082 *params
= bufObj
->Size
;
2084 case GL_BUFFER_USAGE_ARB
:
2085 *params
= bufObj
->Usage
;
2087 case GL_BUFFER_ACCESS_ARB
:
2088 *params
= simplified_access_mode(ctx
,
2089 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2091 case GL_BUFFER_MAPPED_ARB
:
2092 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2094 case GL_BUFFER_ACCESS_FLAGS
:
2095 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2097 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2099 case GL_BUFFER_MAP_OFFSET
:
2100 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2102 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2104 case GL_BUFFER_MAP_LENGTH
:
2105 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2107 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2109 case GL_BUFFER_IMMUTABLE_STORAGE
:
2110 if (!ctx
->Extensions
.ARB_buffer_storage
)
2112 *params
= bufObj
->Immutable
;
2114 case GL_BUFFER_STORAGE_FLAGS
:
2115 if (!ctx
->Extensions
.ARB_buffer_storage
)
2117 *params
= bufObj
->StorageFlags
;
2126 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2127 _mesa_enum_to_string(pname
));
2132 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2134 GET_CURRENT_CONTEXT(ctx
);
2135 struct gl_buffer_object
*bufObj
;
2138 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2139 GL_INVALID_OPERATION
);
2143 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2144 "glGetBufferParameteriv"))
2145 return; /* Error already recorded. */
2147 *params
= (GLint
) parameter
;
2151 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2153 GET_CURRENT_CONTEXT(ctx
);
2154 struct gl_buffer_object
*bufObj
;
2157 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2158 GL_INVALID_OPERATION
);
2162 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2163 "glGetBufferParameteri64v"))
2164 return; /* Error already recorded. */
2166 *params
= parameter
;
2170 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2172 GET_CURRENT_CONTEXT(ctx
);
2173 struct gl_buffer_object
*bufObj
;
2176 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2177 "glGetNamedBufferParameteriv");
2181 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2182 "glGetNamedBufferParameteriv"))
2183 return; /* Error already recorded. */
2185 *params
= (GLint
) parameter
;
2189 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2192 GET_CURRENT_CONTEXT(ctx
);
2193 struct gl_buffer_object
*bufObj
;
2196 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2197 "glGetNamedBufferParameteri64v");
2201 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2202 "glGetNamedBufferParameteri64v"))
2203 return; /* Error already recorded. */
2205 *params
= parameter
;
2210 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2212 GET_CURRENT_CONTEXT(ctx
);
2213 struct gl_buffer_object
*bufObj
;
2215 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2216 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2217 "GL_BUFFER_MAP_POINTER)");
2221 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2222 GL_INVALID_OPERATION
);
2226 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2230 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2232 GET_CURRENT_CONTEXT(ctx
);
2233 struct gl_buffer_object
*bufObj
;
2235 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2236 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2237 "GL_BUFFER_MAP_POINTER)");
2241 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2242 "glGetNamedBufferPointerv");
2246 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2251 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2252 struct gl_buffer_object
*src
,
2253 struct gl_buffer_object
*dst
,
2254 GLintptr readOffset
, GLintptr writeOffset
,
2255 GLsizeiptr size
, const char *func
)
2257 if (_mesa_check_disallowed_mapping(src
)) {
2258 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2259 "%s(readBuffer is mapped)", func
);
2263 if (_mesa_check_disallowed_mapping(dst
)) {
2264 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2265 "%s(writeBuffer is mapped)", func
);
2269 if (readOffset
< 0) {
2270 _mesa_error(ctx
, GL_INVALID_VALUE
,
2271 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2275 if (writeOffset
< 0) {
2276 _mesa_error(ctx
, GL_INVALID_VALUE
,
2277 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2282 _mesa_error(ctx
, GL_INVALID_VALUE
,
2283 "%s(size %d < 0)", func
, (int) size
);
2287 if (readOffset
+ size
> src
->Size
) {
2288 _mesa_error(ctx
, GL_INVALID_VALUE
,
2289 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2290 (int) readOffset
, (int) size
, (int) src
->Size
);
2294 if (writeOffset
+ size
> dst
->Size
) {
2295 _mesa_error(ctx
, GL_INVALID_VALUE
,
2296 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2297 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2302 if (readOffset
+ size
<= writeOffset
) {
2305 else if (writeOffset
+ size
<= readOffset
) {
2309 /* overlapping src/dst is illegal */
2310 _mesa_error(ctx
, GL_INVALID_VALUE
,
2311 "%s(overlapping src/dst)", func
);
2316 dst
->MinMaxCacheDirty
= true;
2318 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2322 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2323 GLintptr readOffset
, GLintptr writeOffset
,
2326 GET_CURRENT_CONTEXT(ctx
);
2327 struct gl_buffer_object
*src
, *dst
;
2329 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2330 GL_INVALID_OPERATION
);
2334 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2335 GL_INVALID_OPERATION
);
2339 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2340 "glCopyBufferSubData");
2344 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2345 GLintptr readOffset
, GLintptr writeOffset
,
2348 GET_CURRENT_CONTEXT(ctx
);
2349 struct gl_buffer_object
*src
, *dst
;
2351 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2352 "glCopyNamedBufferSubData");
2356 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2357 "glCopyNamedBufferSubData");
2361 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2362 "glCopyNamedBufferSubData");
2367 _mesa_map_buffer_range(struct gl_context
*ctx
,
2368 struct gl_buffer_object
*bufObj
,
2369 GLintptr offset
, GLsizeiptr length
,
2370 GLbitfield access
, const char *func
)
2373 GLbitfield allowed_access
;
2375 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2378 _mesa_error(ctx
, GL_INVALID_VALUE
,
2379 "%s(offset %ld < 0)", func
, (long) offset
);
2384 _mesa_error(ctx
, GL_INVALID_VALUE
,
2385 "%s(length %ld < 0)", func
, (long) length
);
2389 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2391 * "An INVALID_OPERATION error is generated for any of the following
2394 * * <length> is zero."
2396 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2397 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2401 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2405 allowed_access
= GL_MAP_READ_BIT
|
2407 GL_MAP_INVALIDATE_RANGE_BIT
|
2408 GL_MAP_INVALIDATE_BUFFER_BIT
|
2409 GL_MAP_FLUSH_EXPLICIT_BIT
|
2410 GL_MAP_UNSYNCHRONIZED_BIT
;
2412 if (ctx
->Extensions
.ARB_buffer_storage
) {
2413 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2414 GL_MAP_COHERENT_BIT
;
2417 if (access
& ~allowed_access
) {
2418 /* generate an error if any bits other than those allowed are set */
2419 _mesa_error(ctx
, GL_INVALID_VALUE
,
2420 "%s(access has undefined bits set)", func
);
2424 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2425 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2426 "%s(access indicates neither read or write)", func
);
2430 if ((access
& GL_MAP_READ_BIT
) &&
2431 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2432 GL_MAP_INVALIDATE_BUFFER_BIT
|
2433 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2434 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2435 "%s(read access with disallowed bits)", func
);
2439 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2440 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2441 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2442 "%s(access has flush explicit without write)", func
);
2446 if (access
& GL_MAP_READ_BIT
&&
2447 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2448 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2449 "%s(buffer does not allow read access)", func
);
2453 if (access
& GL_MAP_WRITE_BIT
&&
2454 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2455 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2456 "%s(buffer does not allow write access)", func
);
2460 if (access
& GL_MAP_COHERENT_BIT
&&
2461 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2462 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2463 "%s(buffer does not allow coherent access)", func
);
2467 if (access
& GL_MAP_PERSISTENT_BIT
&&
2468 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2469 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2470 "%s(buffer does not allow persistent access)", func
);
2474 if (offset
+ length
> bufObj
->Size
) {
2475 _mesa_error(ctx
, GL_INVALID_VALUE
,
2476 "%s(offset %td + length %td > buffer_size %td)", func
,
2477 offset
, length
, bufObj
->Size
);
2481 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2482 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2483 "%s(buffer already mapped)", func
);
2487 if (!bufObj
->Size
) {
2488 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2492 if (access
& GL_MAP_WRITE_BIT
) {
2493 bufObj
->NumMapBufferWriteCalls
++;
2494 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2495 bufObj
->Usage
== GL_STATIC_COPY
) &&
2496 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
2497 BUFFER_USAGE_WARNING(ctx
,
2498 "using %s(buffer %u, offset %u, length %u) to "
2499 "update a %s buffer",
2500 func
, bufObj
->Name
, offset
, length
,
2501 _mesa_enum_to_string(bufObj
->Usage
));
2505 assert(ctx
->Driver
.MapBufferRange
);
2506 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2509 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2512 /* The driver callback should have set all these fields.
2513 * This is important because other modules (like VBO) might call
2514 * the driver function directly.
2516 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2517 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2518 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2519 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2522 if (access
& GL_MAP_WRITE_BIT
) {
2523 bufObj
->Written
= GL_TRUE
;
2524 bufObj
->MinMaxCacheDirty
= true;
2528 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2529 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2530 bufObj
->Name
, bufObj
->Size
, access
);
2531 /* Access must be write only */
2532 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2534 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2535 for (i
= 0; i
< bufObj
->Size
; i
++)
2542 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2543 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2545 /* buffer is 100 bytes larger than requested, fill with magic value */
2546 for (i
= 0; i
< 100; i
++) {
2547 buf
[bufObj
->Size
- i
- 1] = 123;
2556 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2559 GET_CURRENT_CONTEXT(ctx
);
2560 struct gl_buffer_object
*bufObj
;
2562 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2563 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2564 "glMapBufferRange(ARB_map_buffer_range not supported)");
2568 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2572 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2573 "glMapBufferRange");
2577 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2580 GET_CURRENT_CONTEXT(ctx
);
2581 struct gl_buffer_object
*bufObj
;
2583 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2584 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2585 "glMapNamedBufferRange("
2586 "ARB_map_buffer_range not supported)");
2590 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2594 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2595 "glMapNamedBufferRange");
2599 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2600 * flags for input to _mesa_map_buffer_range.
2602 * \return true if the type of requested access is permissible.
2605 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2609 case GL_READ_ONLY_ARB
:
2610 *flags
= GL_MAP_READ_BIT
;
2611 return _mesa_is_desktop_gl(ctx
);
2612 case GL_WRITE_ONLY_ARB
:
2613 *flags
= GL_MAP_WRITE_BIT
;
2615 case GL_READ_WRITE_ARB
:
2616 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2617 return _mesa_is_desktop_gl(ctx
);
2624 _mesa_MapBuffer(GLenum target
, GLenum access
)
2626 GET_CURRENT_CONTEXT(ctx
);
2627 struct gl_buffer_object
*bufObj
;
2628 GLbitfield accessFlags
;
2630 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2631 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2635 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2639 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2644 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2646 GET_CURRENT_CONTEXT(ctx
);
2647 struct gl_buffer_object
*bufObj
;
2648 GLbitfield accessFlags
;
2650 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2651 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2655 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2659 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2660 "glMapNamedBuffer");
2665 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2666 struct gl_buffer_object
*bufObj
,
2667 GLintptr offset
, GLsizeiptr length
,
2670 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2671 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2672 "%s(ARB_map_buffer_range not supported)", func
);
2677 _mesa_error(ctx
, GL_INVALID_VALUE
,
2678 "%s(offset %ld < 0)", func
, (long) offset
);
2683 _mesa_error(ctx
, GL_INVALID_VALUE
,
2684 "%s(length %ld < 0)", func
, (long) length
);
2688 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2689 /* buffer is not mapped */
2690 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2691 "%s(buffer is not mapped)", func
);
2695 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2696 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2697 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2698 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2702 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2703 _mesa_error(ctx
, GL_INVALID_VALUE
,
2704 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2705 (long) offset
, (long) length
,
2706 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2710 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2712 if (ctx
->Driver
.FlushMappedBufferRange
)
2713 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2718 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2721 GET_CURRENT_CONTEXT(ctx
);
2722 struct gl_buffer_object
*bufObj
;
2724 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2725 GL_INVALID_OPERATION
);
2729 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2730 "glFlushMappedBufferRange");
2734 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2737 GET_CURRENT_CONTEXT(ctx
);
2738 struct gl_buffer_object
*bufObj
;
2740 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2741 "glFlushMappedNamedBufferRange");
2745 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2746 "glFlushMappedNamedBufferRange");
2751 * Binds a buffer object to a uniform buffer binding point.
2753 * The caller is responsible for flushing vertices and updating
2757 set_ubo_binding(struct gl_context
*ctx
,
2758 struct gl_uniform_buffer_binding
*binding
,
2759 struct gl_buffer_object
*bufObj
,
2764 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2766 binding
->Offset
= offset
;
2767 binding
->Size
= size
;
2768 binding
->AutomaticSize
= autoSize
;
2770 /* If this is a real buffer object, mark it has having been used
2771 * at some point as a UBO.
2774 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2778 * Binds a buffer object to a shader storage buffer binding point.
2780 * The caller is responsible for flushing vertices and updating
2784 set_ssbo_binding(struct gl_context
*ctx
,
2785 struct gl_shader_storage_buffer_binding
*binding
,
2786 struct gl_buffer_object
*bufObj
,
2791 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2793 binding
->Offset
= offset
;
2794 binding
->Size
= size
;
2795 binding
->AutomaticSize
= autoSize
;
2797 /* If this is a real buffer object, mark it has having been used
2798 * at some point as a SSBO.
2801 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
2805 * Binds a buffer object to a uniform buffer binding point.
2807 * Unlike set_ubo_binding(), this function also flushes vertices
2808 * and updates NewDriverState. It also checks if the binding
2809 * has actually changed before updating it.
2812 bind_uniform_buffer(struct gl_context
*ctx
,
2814 struct gl_buffer_object
*bufObj
,
2819 struct gl_uniform_buffer_binding
*binding
=
2820 &ctx
->UniformBufferBindings
[index
];
2822 if (binding
->BufferObject
== bufObj
&&
2823 binding
->Offset
== offset
&&
2824 binding
->Size
== size
&&
2825 binding
->AutomaticSize
== autoSize
) {
2829 FLUSH_VERTICES(ctx
, 0);
2830 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2832 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2836 * Binds a buffer object to a shader storage buffer binding point.
2838 * Unlike set_ssbo_binding(), this function also flushes vertices
2839 * and updates NewDriverState. It also checks if the binding
2840 * has actually changed before updating it.
2843 bind_shader_storage_buffer(struct gl_context
*ctx
,
2845 struct gl_buffer_object
*bufObj
,
2850 struct gl_shader_storage_buffer_binding
*binding
=
2851 &ctx
->ShaderStorageBufferBindings
[index
];
2853 if (binding
->BufferObject
== bufObj
&&
2854 binding
->Offset
== offset
&&
2855 binding
->Size
== size
&&
2856 binding
->AutomaticSize
== autoSize
) {
2860 FLUSH_VERTICES(ctx
, 0);
2861 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
2863 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2867 * Bind a region of a buffer object to a uniform block binding point.
2868 * \param index the uniform buffer binding point index
2869 * \param bufObj the buffer object
2870 * \param offset offset to the start of buffer object region
2871 * \param size size of the buffer object region
2874 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2876 struct gl_buffer_object
*bufObj
,
2880 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2881 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2885 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2886 _mesa_error(ctx
, GL_INVALID_VALUE
,
2887 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2888 ctx
->Const
.UniformBufferOffsetAlignment
);
2892 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2897 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2898 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2902 * Bind a region of a buffer object to a shader storage block binding point.
2903 * \param index the shader storage buffer binding point index
2904 * \param bufObj the buffer object
2905 * \param offset offset to the start of buffer object region
2906 * \param size size of the buffer object region
2909 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
2911 struct gl_buffer_object
*bufObj
,
2915 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2916 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2920 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
2921 _mesa_error(ctx
, GL_INVALID_VALUE
,
2922 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2923 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
2927 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2932 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2933 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2937 * Bind a buffer object to a uniform block binding point.
2938 * As above, but offset = 0.
2941 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2943 struct gl_buffer_object
*bufObj
)
2945 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2946 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2950 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2952 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2953 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2955 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2959 * Bind a buffer object to a shader storage block binding point.
2960 * As above, but offset = 0.
2963 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
2965 struct gl_buffer_object
*bufObj
)
2967 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2968 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2972 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2974 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2975 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2977 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2981 * Binds a buffer object to an atomic buffer binding point.
2983 * The caller is responsible for validating the offset,
2984 * flushing the vertices and updating NewDriverState.
2987 set_atomic_buffer_binding(struct gl_context
*ctx
,
2988 struct gl_atomic_buffer_binding
*binding
,
2989 struct gl_buffer_object
*bufObj
,
2993 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2995 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2996 binding
->Offset
= -1;
2999 binding
->Offset
= offset
;
3000 binding
->Size
= size
;
3001 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
3006 * Binds a buffer object to an atomic buffer binding point.
3008 * Unlike set_atomic_buffer_binding(), this function also validates the
3009 * index and offset, flushes vertices, and updates NewDriverState.
3010 * It also checks if the binding has actually changing before
3014 bind_atomic_buffer(struct gl_context
*ctx
,
3016 struct gl_buffer_object
*bufObj
,
3021 struct gl_atomic_buffer_binding
*binding
;
3023 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3024 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3028 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3029 _mesa_error(ctx
, GL_INVALID_VALUE
,
3030 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3031 ATOMIC_COUNTER_SIZE
);
3035 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3037 binding
= &ctx
->AtomicBufferBindings
[index
];
3038 if (binding
->BufferObject
== bufObj
&&
3039 binding
->Offset
== offset
&&
3040 binding
->Size
== size
) {
3044 FLUSH_VERTICES(ctx
, 0);
3045 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3047 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3051 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3053 const GLintptr
*offsets
,
3054 const GLsizeiptr
*sizes
)
3056 if (offsets
[index
] < 0) {
3057 /* The ARB_multi_bind spec says:
3059 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3060 * value in <offsets> is less than zero (per binding)."
3062 _mesa_error(ctx
, GL_INVALID_VALUE
,
3063 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3064 index
, (int64_t) offsets
[index
]);
3068 if (sizes
[index
] <= 0) {
3069 /* The ARB_multi_bind spec says:
3071 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3072 * value in <sizes> is less than or equal to zero (per binding)."
3074 _mesa_error(ctx
, GL_INVALID_VALUE
,
3075 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3076 index
, (int64_t) sizes
[index
]);
3084 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3085 GLuint first
, GLsizei count
,
3088 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3089 _mesa_error(ctx
, GL_INVALID_ENUM
,
3090 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3094 /* The ARB_multi_bind_spec says:
3096 * "An INVALID_OPERATION error is generated if <first> + <count> is
3097 * greater than the number of target-specific indexed binding points,
3098 * as described in section 6.7.1."
3100 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3101 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3102 "%s(first=%u + count=%d > the value of "
3103 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3104 caller
, first
, count
,
3105 ctx
->Const
.MaxUniformBufferBindings
);
3113 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3114 GLuint first
, GLsizei count
,
3117 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3118 _mesa_error(ctx
, GL_INVALID_ENUM
,
3119 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3123 /* The ARB_multi_bind_spec says:
3125 * "An INVALID_OPERATION error is generated if <first> + <count> is
3126 * greater than the number of target-specific indexed binding points,
3127 * as described in section 6.7.1."
3129 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3130 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3131 "%s(first=%u + count=%d > the value of "
3132 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3133 caller
, first
, count
,
3134 ctx
->Const
.MaxShaderStorageBufferBindings
);
3142 * Unbind all uniform buffers in the range
3143 * <first> through <first>+<count>-1
3146 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3148 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3151 for (i
= 0; i
< count
; i
++)
3152 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3153 bufObj
, -1, -1, GL_TRUE
);
3157 * Unbind all shader storage buffers in the range
3158 * <first> through <first>+<count>-1
3161 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3164 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3167 for (i
= 0; i
< count
; i
++)
3168 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3169 bufObj
, -1, -1, GL_TRUE
);
3173 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3174 const GLuint
*buffers
,
3176 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3181 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3184 /* Assume that at least one binding will be changed */
3185 FLUSH_VERTICES(ctx
, 0);
3186 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3189 /* The ARB_multi_bind spec says:
3191 * "If <buffers> is NULL, all bindings from <first> through
3192 * <first>+<count>-1 are reset to their unbound (zero) state.
3193 * In this case, the offsets and sizes associated with the
3194 * binding points are set to default values, ignoring
3195 * <offsets> and <sizes>."
3197 unbind_uniform_buffers(ctx
, first
, count
);
3201 /* Note that the error semantics for multi-bind commands differ from
3202 * those of other GL commands.
3204 * The Issues section in the ARB_multi_bind spec says:
3206 * "(11) Typically, OpenGL specifies that if an error is generated by a
3207 * command, that command has no effect. This is somewhat
3208 * unfortunate for multi-bind commands, because it would require a
3209 * first pass to scan the entire list of bound objects for errors
3210 * and then a second pass to actually perform the bindings.
3211 * Should we have different error semantics?
3213 * RESOLVED: Yes. In this specification, when the parameters for
3214 * one of the <count> binding points are invalid, that binding point
3215 * is not updated and an error will be generated. However, other
3216 * binding points in the same command will be updated if their
3217 * parameters are valid and no other error occurs."
3220 _mesa_begin_bufferobj_lookups(ctx
);
3222 for (i
= 0; i
< count
; i
++) {
3223 struct gl_uniform_buffer_binding
*binding
=
3224 &ctx
->UniformBufferBindings
[first
+ i
];
3225 struct gl_buffer_object
*bufObj
;
3226 GLintptr offset
= 0;
3227 GLsizeiptr size
= 0;
3230 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3233 /* The ARB_multi_bind spec says:
3235 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3236 * pair of values in <offsets> and <sizes> does not respectively
3237 * satisfy the constraints described for those parameters for the
3238 * specified target, as described in section 6.7.1 (per binding)."
3240 * Section 6.7.1 refers to table 6.5, which says:
3242 * "┌───────────────────────────────────────────────────────────────┐
3243 * │ Uniform buffer array bindings (see sec. 7.6) │
3244 * ├─────────────────────┬─────────────────────────────────────────┤
3246 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3247 * │ │ OFFSET_ALIGNMENT │
3249 * │ size restriction │ none │
3250 * └─────────────────────┴─────────────────────────────────────────┘"
3252 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3253 _mesa_error(ctx
, GL_INVALID_VALUE
,
3254 "glBindBuffersRange(offsets[%u]=%" PRId64
3255 " is misaligned; it must be a multiple of the value of "
3256 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3257 "target=GL_UNIFORM_BUFFER)",
3258 i
, (int64_t) offsets
[i
],
3259 ctx
->Const
.UniformBufferOffsetAlignment
);
3263 offset
= offsets
[i
];
3267 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3268 bufObj
= binding
->BufferObject
;
3270 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3273 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3274 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3276 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3280 _mesa_end_bufferobj_lookups(ctx
);
3284 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3285 GLsizei count
, const GLuint
*buffers
,
3287 const GLintptr
*offsets
,
3288 const GLsizeiptr
*sizes
,
3293 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3296 /* Assume that at least one binding will be changed */
3297 FLUSH_VERTICES(ctx
, 0);
3298 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3301 /* The ARB_multi_bind spec says:
3303 * "If <buffers> is NULL, all bindings from <first> through
3304 * <first>+<count>-1 are reset to their unbound (zero) state.
3305 * In this case, the offsets and sizes associated with the
3306 * binding points are set to default values, ignoring
3307 * <offsets> and <sizes>."
3309 unbind_shader_storage_buffers(ctx
, first
, count
);
3313 /* Note that the error semantics for multi-bind commands differ from
3314 * those of other GL commands.
3316 * The Issues section in the ARB_multi_bind spec says:
3318 * "(11) Typically, OpenGL specifies that if an error is generated by a
3319 * command, that command has no effect. This is somewhat
3320 * unfortunate for multi-bind commands, because it would require a
3321 * first pass to scan the entire list of bound objects for errors
3322 * and then a second pass to actually perform the bindings.
3323 * Should we have different error semantics?
3325 * RESOLVED: Yes. In this specification, when the parameters for
3326 * one of the <count> binding points are invalid, that binding point
3327 * is not updated and an error will be generated. However, other
3328 * binding points in the same command will be updated if their
3329 * parameters are valid and no other error occurs."
3332 _mesa_begin_bufferobj_lookups(ctx
);
3334 for (i
= 0; i
< count
; i
++) {
3335 struct gl_shader_storage_buffer_binding
*binding
=
3336 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3337 struct gl_buffer_object
*bufObj
;
3338 GLintptr offset
= 0;
3339 GLsizeiptr size
= 0;
3342 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3345 /* The ARB_multi_bind spec says:
3347 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3348 * pair of values in <offsets> and <sizes> does not respectively
3349 * satisfy the constraints described for those parameters for the
3350 * specified target, as described in section 6.7.1 (per binding)."
3352 * Section 6.7.1 refers to table 6.5, which says:
3354 * "┌───────────────────────────────────────────────────────────────┐
3355 * │ Shader storage buffer array bindings (see sec. 7.8) │
3356 * ├─────────────────────┬─────────────────────────────────────────┤
3358 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3359 * │ │ BUFFER_OFFSET_ALIGNMENT │
3361 * │ size restriction │ none │
3362 * └─────────────────────┴─────────────────────────────────────────┘"
3364 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3365 _mesa_error(ctx
, GL_INVALID_VALUE
,
3366 "glBindBuffersRange(offsets[%u]=%" PRId64
3367 " is misaligned; it must be a multiple of the value of "
3368 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3369 "target=GL_SHADER_STORAGE_BUFFER)",
3370 i
, (int64_t) offsets
[i
],
3371 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3375 offset
= offsets
[i
];
3379 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3380 bufObj
= binding
->BufferObject
;
3382 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3385 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3386 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3388 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3392 _mesa_end_bufferobj_lookups(ctx
);
3396 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3397 struct gl_transform_feedback_object
*tfObj
,
3398 GLuint first
, GLsizei count
, const char *caller
)
3400 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3401 _mesa_error(ctx
, GL_INVALID_ENUM
,
3402 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3406 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3408 * "An INVALID_OPERATION error is generated :
3411 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3412 * FEEDBACK_BUFFER and transform feedback is currently active."
3414 * We assume that this is also meant to apply to BindBuffersRange
3415 * and BindBuffersBase.
3417 if (tfObj
->Active
) {
3418 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3419 "%s(Changing transform feedback buffers while "
3420 "transform feedback is active)", caller
);
3424 /* The ARB_multi_bind_spec says:
3426 * "An INVALID_OPERATION error is generated if <first> + <count> is
3427 * greater than the number of target-specific indexed binding points,
3428 * as described in section 6.7.1."
3430 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3431 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3432 "%s(first=%u + count=%d > the value of "
3433 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3434 caller
, first
, count
,
3435 ctx
->Const
.MaxTransformFeedbackBuffers
);
3443 * Unbind all transform feedback buffers in the range
3444 * <first> through <first>+<count>-1
3447 unbind_xfb_buffers(struct gl_context
*ctx
,
3448 struct gl_transform_feedback_object
*tfObj
,
3449 GLuint first
, GLsizei count
)
3451 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3454 for (i
= 0; i
< count
; i
++)
3455 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3460 bind_xfb_buffers(struct gl_context
*ctx
,
3461 GLuint first
, GLsizei count
,
3462 const GLuint
*buffers
,
3464 const GLintptr
*offsets
,
3465 const GLsizeiptr
*sizes
,
3468 struct gl_transform_feedback_object
*tfObj
=
3469 ctx
->TransformFeedback
.CurrentObject
;
3472 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
3475 /* Assume that at least one binding will be changed */
3476 FLUSH_VERTICES(ctx
, 0);
3477 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3480 /* The ARB_multi_bind spec says:
3482 * "If <buffers> is NULL, all bindings from <first> through
3483 * <first>+<count>-1 are reset to their unbound (zero) state.
3484 * In this case, the offsets and sizes associated with the
3485 * binding points are set to default values, ignoring
3486 * <offsets> and <sizes>."
3488 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3492 /* Note that the error semantics for multi-bind commands differ from
3493 * those of other GL commands.
3495 * The Issues section in the ARB_multi_bind spec says:
3497 * "(11) Typically, OpenGL specifies that if an error is generated by a
3498 * command, that command has no effect. This is somewhat
3499 * unfortunate for multi-bind commands, because it would require a
3500 * first pass to scan the entire list of bound objects for errors
3501 * and then a second pass to actually perform the bindings.
3502 * Should we have different error semantics?
3504 * RESOLVED: Yes. In this specification, when the parameters for
3505 * one of the <count> binding points are invalid, that binding point
3506 * is not updated and an error will be generated. However, other
3507 * binding points in the same command will be updated if their
3508 * parameters are valid and no other error occurs."
3511 _mesa_begin_bufferobj_lookups(ctx
);
3513 for (i
= 0; i
< count
; i
++) {
3514 const GLuint index
= first
+ i
;
3515 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3516 struct gl_buffer_object
*bufObj
;
3517 GLintptr offset
= 0;
3518 GLsizeiptr size
= 0;
3521 offset
= offsets
[i
];
3524 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3527 /* The ARB_multi_bind spec says:
3529 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3530 * pair of values in <offsets> and <sizes> does not respectively
3531 * satisfy the constraints described for those parameters for the
3532 * specified target, as described in section 6.7.1 (per binding)."
3534 * Section 6.7.1 refers to table 6.5, which says:
3536 * "┌───────────────────────────────────────────────────────────────┐
3537 * │ Transform feedback array bindings (see sec. 13.2.2) │
3538 * ├───────────────────────┬───────────────────────────────────────┤
3540 * │ offset restriction │ multiple of 4 │
3542 * │ size restriction │ multiple of 4 │
3543 * └───────────────────────┴───────────────────────────────────────┘"
3545 if (offsets
[i
] & 0x3) {
3546 _mesa_error(ctx
, GL_INVALID_VALUE
,
3547 "glBindBuffersRange(offsets[%u]=%" PRId64
3548 " is misaligned; it must be a multiple of 4 when "
3549 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3550 i
, (int64_t) offsets
[i
]);
3554 if (sizes
[i
] & 0x3) {
3555 _mesa_error(ctx
, GL_INVALID_VALUE
,
3556 "glBindBuffersRange(sizes[%u]=%" PRId64
3557 " is misaligned; it must be a multiple of 4 when "
3558 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3559 i
, (int64_t) sizes
[i
]);
3563 offset
= offsets
[i
];
3567 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3568 bufObj
= boundBufObj
;
3570 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3573 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3577 _mesa_end_bufferobj_lookups(ctx
);
3581 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3582 GLuint first
, GLsizei count
,
3585 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3586 _mesa_error(ctx
, GL_INVALID_ENUM
,
3587 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3591 /* The ARB_multi_bind_spec says:
3593 * "An INVALID_OPERATION error is generated if <first> + <count> is
3594 * greater than the number of target-specific indexed binding points,
3595 * as described in section 6.7.1."
3597 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3598 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3599 "%s(first=%u + count=%d > the value of "
3600 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3601 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3609 * Unbind all atomic counter buffers in the range
3610 * <first> through <first>+<count>-1
3613 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3615 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3618 for (i
= 0; i
< count
; i
++)
3619 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3624 bind_atomic_buffers(struct gl_context
*ctx
,
3627 const GLuint
*buffers
,
3629 const GLintptr
*offsets
,
3630 const GLsizeiptr
*sizes
,
3635 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
3638 /* Assume that at least one binding will be changed */
3639 FLUSH_VERTICES(ctx
, 0);
3640 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3643 /* The ARB_multi_bind spec says:
3645 * "If <buffers> is NULL, all bindings from <first> through
3646 * <first>+<count>-1 are reset to their unbound (zero) state.
3647 * In this case, the offsets and sizes associated with the
3648 * binding points are set to default values, ignoring
3649 * <offsets> and <sizes>."
3651 unbind_atomic_buffers(ctx
, first
, count
);
3655 /* Note that the error semantics for multi-bind commands differ from
3656 * those of other GL commands.
3658 * The Issues section in the ARB_multi_bind spec says:
3660 * "(11) Typically, OpenGL specifies that if an error is generated by a
3661 * command, that command has no effect. This is somewhat
3662 * unfortunate for multi-bind commands, because it would require a
3663 * first pass to scan the entire list of bound objects for errors
3664 * and then a second pass to actually perform the bindings.
3665 * Should we have different error semantics?
3667 * RESOLVED: Yes. In this specification, when the parameters for
3668 * one of the <count> binding points are invalid, that binding point
3669 * is not updated and an error will be generated. However, other
3670 * binding points in the same command will be updated if their
3671 * parameters are valid and no other error occurs."
3674 _mesa_begin_bufferobj_lookups(ctx
);
3676 for (i
= 0; i
< count
; i
++) {
3677 struct gl_atomic_buffer_binding
*binding
=
3678 &ctx
->AtomicBufferBindings
[first
+ i
];
3679 struct gl_buffer_object
*bufObj
;
3680 GLintptr offset
= 0;
3681 GLsizeiptr size
= 0;
3684 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3687 /* The ARB_multi_bind spec says:
3689 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3690 * pair of values in <offsets> and <sizes> does not respectively
3691 * satisfy the constraints described for those parameters for the
3692 * specified target, as described in section 6.7.1 (per binding)."
3694 * Section 6.7.1 refers to table 6.5, which says:
3696 * "┌───────────────────────────────────────────────────────────────┐
3697 * │ Atomic counter array bindings (see sec. 7.7.2) │
3698 * ├───────────────────────┬───────────────────────────────────────┤
3700 * │ offset restriction │ multiple of 4 │
3702 * │ size restriction │ none │
3703 * └───────────────────────┴───────────────────────────────────────┘"
3705 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3706 _mesa_error(ctx
, GL_INVALID_VALUE
,
3707 "glBindBuffersRange(offsets[%u]=%" PRId64
3708 " is misaligned; it must be a multiple of %d when "
3709 "target=GL_ATOMIC_COUNTER_BUFFER)",
3710 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3714 offset
= offsets
[i
];
3718 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3719 bufObj
= binding
->BufferObject
;
3721 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3724 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3727 _mesa_end_bufferobj_lookups(ctx
);
3731 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3732 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3734 GET_CURRENT_CONTEXT(ctx
);
3735 struct gl_buffer_object
*bufObj
;
3737 if (MESA_VERBOSE
& VERBOSE_API
) {
3738 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %ld, %ld)\n",
3739 _mesa_enum_to_string(target
), index
, buffer
, offset
, size
);
3743 bufObj
= ctx
->Shared
->NullBufferObj
;
3745 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3747 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3748 &bufObj
, "glBindBufferRange"))
3752 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3753 "glBindBufferRange(invalid buffer=%u)", buffer
);
3759 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3766 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3767 _mesa_bind_buffer_range_transform_feedback(ctx
,
3768 ctx
->TransformFeedback
.CurrentObject
,
3769 index
, bufObj
, offset
, size
,
3772 case GL_UNIFORM_BUFFER
:
3773 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3775 case GL_SHADER_STORAGE_BUFFER
:
3776 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3778 case GL_ATOMIC_COUNTER_BUFFER
:
3779 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3780 "glBindBufferRange");
3783 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3789 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3791 GET_CURRENT_CONTEXT(ctx
);
3792 struct gl_buffer_object
*bufObj
;
3794 if (MESA_VERBOSE
& VERBOSE_API
) {
3795 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
3796 _mesa_enum_to_string(target
), index
, buffer
);
3800 bufObj
= ctx
->Shared
->NullBufferObj
;
3802 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3804 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3805 &bufObj
, "glBindBufferBase"))
3809 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3810 "glBindBufferBase(invalid buffer=%u)", buffer
);
3814 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3815 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3817 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3818 * zero and size equal to the size of buffer."
3820 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3822 * "If the parameter (starting offset or size) was not specified when the
3823 * buffer object was bound, zero is returned."
3825 * What happens if the size of the buffer changes? Does the size of the
3826 * buffer at the moment glBindBufferBase was called still play a role, like
3827 * the first quote would imply, or is the size meaningless in the
3828 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3829 * core spec page 45 says:
3831 * "It is equivalent to calling BindBufferRange with offset zero, while
3832 * size is determined by the size of the bound buffer at the time the
3835 * My interpretation is that the GL 4.1 spec was a clarification of the
3836 * behavior, not a change. In particular, this choice will only make
3837 * rendering work in cases where it would have had undefined results.
3841 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3842 _mesa_bind_buffer_base_transform_feedback(ctx
,
3843 ctx
->TransformFeedback
.CurrentObject
,
3844 index
, bufObj
, false);
3846 case GL_UNIFORM_BUFFER
:
3847 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
3849 case GL_SHADER_STORAGE_BUFFER
:
3850 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
3852 case GL_ATOMIC_COUNTER_BUFFER
:
3853 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
3854 "glBindBufferBase");
3857 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
3863 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
3864 const GLuint
*buffers
,
3865 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3867 GET_CURRENT_CONTEXT(ctx
);
3869 if (MESA_VERBOSE
& VERBOSE_API
) {
3870 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
3871 _mesa_enum_to_string(target
), first
, count
,
3872 buffers
, offsets
, sizes
);
3876 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3877 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3878 "glBindBuffersRange");
3880 case GL_UNIFORM_BUFFER
:
3881 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3882 "glBindBuffersRange");
3884 case GL_SHADER_STORAGE_BUFFER
:
3885 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3886 "glBindBuffersRange");
3888 case GL_ATOMIC_COUNTER_BUFFER
:
3889 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3890 "glBindBuffersRange");
3893 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
3894 _mesa_enum_to_string(target
));
3900 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
3901 const GLuint
*buffers
)
3903 GET_CURRENT_CONTEXT(ctx
);
3905 if (MESA_VERBOSE
& VERBOSE_API
) {
3906 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
3907 _mesa_enum_to_string(target
), first
, count
, buffers
);
3911 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3912 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3913 "glBindBuffersBase");
3915 case GL_UNIFORM_BUFFER
:
3916 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3917 "glBindBuffersBase");
3919 case GL_SHADER_STORAGE_BUFFER
:
3920 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3921 "glBindBuffersBase");
3923 case GL_ATOMIC_COUNTER_BUFFER
:
3924 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3925 "glBindBuffersBase");
3928 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
3929 _mesa_enum_to_string(target
));
3935 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
3938 GET_CURRENT_CONTEXT(ctx
);
3939 struct gl_buffer_object
*bufObj
;
3940 const GLintptr end
= offset
+ length
;
3942 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
3943 * Profile) spec says:
3945 * "An INVALID_VALUE error is generated if buffer is zero or is not the
3946 * name of an existing buffer object."
3948 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3949 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
3950 _mesa_error(ctx
, GL_INVALID_VALUE
,
3951 "glInvalidateBufferSubData(name = 0x%x) invalid object",
3956 /* The GL_ARB_invalidate_subdata spec says:
3958 * "An INVALID_VALUE error is generated if <offset> or <length> is
3959 * negative, or if <offset> + <length> is greater than the value of
3962 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
3963 _mesa_error(ctx
, GL_INVALID_VALUE
,
3964 "glInvalidateBufferSubData(invalid offset or length)");
3968 /* The OpenGL 4.4 (Core Profile) spec says:
3970 * "An INVALID_OPERATION error is generated if buffer is currently
3971 * mapped by MapBuffer or if the invalidate range intersects the range
3972 * currently mapped by MapBufferRange, unless it was mapped
3973 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
3975 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
3976 bufferobj_range_mapped(bufObj
, offset
, length
)) {
3977 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3978 "glInvalidateBufferSubData(intersection with mapped "
3983 if (ctx
->Driver
.InvalidateBufferSubData
)
3984 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
3988 _mesa_InvalidateBufferData(GLuint buffer
)
3990 GET_CURRENT_CONTEXT(ctx
);
3991 struct gl_buffer_object
*bufObj
;
3993 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
3994 * Profile) spec says:
3996 * "An INVALID_VALUE error is generated if buffer is zero or is not the
3997 * name of an existing buffer object."
3999 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4000 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4001 _mesa_error(ctx
, GL_INVALID_VALUE
,
4002 "glInvalidateBufferData(name = 0x%x) invalid object",
4007 /* The OpenGL 4.4 (Core Profile) spec says:
4009 * "An INVALID_OPERATION error is generated if buffer is currently
4010 * mapped by MapBuffer or if the invalidate range intersects the range
4011 * currently mapped by MapBufferRange, unless it was mapped
4012 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4014 if (_mesa_check_disallowed_mapping(bufObj
)) {
4015 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4016 "glInvalidateBufferData(intersection with mapped "
4021 if (ctx
->Driver
.InvalidateBufferSubData
)
4022 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, 0, bufObj
->Size
);