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 */
40 #include "bufferobj.h"
43 #include "glformats.h"
45 #include "transformfeedback.h"
50 /*#define BOUNDS_CHECK*/
54 * We count the number of buffer modification calls to check for
55 * inefficient buffer use. This is the number of such calls before we
58 #define BUFFER_WARNING_CALL_COUNT 4
62 * Helper to warn of possible performance issues, such as frequently
63 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
67 buffer_usage_warning(struct gl_context
*ctx
, GLuint
*id
, const char *fmt
, ...)
72 _mesa_gl_vdebug(ctx
, id
,
73 MESA_DEBUG_SOURCE_API
,
74 MESA_DEBUG_TYPE_PERFORMANCE
,
75 MESA_DEBUG_SEVERITY_MEDIUM
,
80 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
82 static GLuint id = 0; \
83 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
88 * Used as a placeholder for buffer objects between glGenBuffers() and
89 * glBindBuffer() so that glIsBuffer() can work correctly.
91 static struct gl_buffer_object DummyBufferObject
;
95 * Return pointer to address of a buffer object target.
96 * \param ctx the GL context
97 * \param target the buffer object target to be retrieved.
98 * \return pointer to pointer to the buffer object bound to \c target in the
99 * specified context or \c NULL if \c target is invalid.
101 static inline struct gl_buffer_object
**
102 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
104 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
106 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
107 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
111 case GL_ARRAY_BUFFER_ARB
:
112 return &ctx
->Array
.ArrayBufferObj
;
113 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
114 return &ctx
->Array
.VAO
->IndexBufferObj
;
115 case GL_PIXEL_PACK_BUFFER_EXT
:
116 return &ctx
->Pack
.BufferObj
;
117 case GL_PIXEL_UNPACK_BUFFER_EXT
:
118 return &ctx
->Unpack
.BufferObj
;
119 case GL_COPY_READ_BUFFER
:
120 return &ctx
->CopyReadBuffer
;
121 case GL_COPY_WRITE_BUFFER
:
122 return &ctx
->CopyWriteBuffer
;
123 case GL_DRAW_INDIRECT_BUFFER
:
124 if ((ctx
->API
== API_OPENGL_CORE
&&
125 ctx
->Extensions
.ARB_draw_indirect
) ||
126 _mesa_is_gles31(ctx
)) {
127 return &ctx
->DrawIndirectBuffer
;
130 case GL_PARAMETER_BUFFER_ARB
:
131 if (_mesa_has_ARB_indirect_parameters(ctx
)) {
132 return &ctx
->ParameterBuffer
;
135 case GL_DISPATCH_INDIRECT_BUFFER
:
136 if (_mesa_has_compute_shaders(ctx
)) {
137 return &ctx
->DispatchIndirectBuffer
;
140 case GL_TRANSFORM_FEEDBACK_BUFFER
:
141 if (ctx
->Extensions
.EXT_transform_feedback
) {
142 return &ctx
->TransformFeedback
.CurrentBuffer
;
145 case GL_TEXTURE_BUFFER
:
146 if (ctx
->API
== API_OPENGL_CORE
&&
147 ctx
->Extensions
.ARB_texture_buffer_object
) {
148 return &ctx
->Texture
.BufferObject
;
151 case GL_UNIFORM_BUFFER
:
152 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
153 return &ctx
->UniformBuffer
;
156 case GL_SHADER_STORAGE_BUFFER
:
157 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
158 return &ctx
->ShaderStorageBuffer
;
161 case GL_ATOMIC_COUNTER_BUFFER
:
162 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
163 return &ctx
->AtomicBuffer
;
166 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
167 if (ctx
->Extensions
.AMD_pinned_memory
) {
168 return &ctx
->ExternalVirtualMemoryBuffer
;
179 * Get the buffer object bound to the specified target in a GL context.
180 * \param ctx the GL context
181 * \param target the buffer object target to be retrieved.
182 * \param error the GL error to record if target is illegal.
183 * \return pointer to the buffer object bound to \c target in the
184 * specified context or \c NULL if \c target is invalid.
186 static inline struct gl_buffer_object
*
187 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
190 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
193 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
197 if (!_mesa_is_bufferobj(*bufObj
)) {
198 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
207 * Convert a GLbitfield describing the mapped buffer access flags
208 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
211 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
213 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
214 if ((access
& rwFlags
) == rwFlags
)
215 return GL_READ_WRITE
;
216 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
218 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
219 return GL_WRITE_ONLY
;
221 /* Otherwise, AccessFlags is zero (the default state).
223 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
225 * Name Type Initial Value Legal Values
227 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
230 * However, table 6.8 in the GL_OES_mapbuffer extension says:
232 * Get Value Type Get Command Value Description
233 * --------- ---- ----------- ----- -----------
234 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
236 * The difference is because GL_OES_mapbuffer only supports mapping buffers
241 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
246 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
248 * The regions do not overlap if and only if the end of the given
249 * region is before the mapped region or the start of the given region
250 * is after the mapped region.
252 * \param obj Buffer object target on which to operate.
253 * \param offset Offset of the first byte of the subdata range.
254 * \param size Size, in bytes, of the subdata range.
255 * \return true if ranges overlap, false otherwise
259 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
260 GLintptr offset
, GLsizeiptr size
)
262 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
263 const GLintptr end
= offset
+ size
;
264 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
265 obj
->Mappings
[MAP_USER
].Length
;
267 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
276 * Tests the subdata range parameters and sets the GL error code for
277 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
278 * \c glClearBufferSubData.
280 * \param ctx GL context.
281 * \param bufObj The buffer object.
282 * \param offset Offset of the first byte of the subdata range.
283 * \param size Size, in bytes, of the subdata range.
284 * \param mappedRange If true, checks if an overlapping range is mapped.
285 * If false, checks if buffer is mapped.
286 * \param caller Name of calling function for recording errors.
287 * \return false if error, true otherwise
289 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
292 buffer_object_subdata_range_good(struct gl_context
*ctx
,
293 const struct gl_buffer_object
*bufObj
,
294 GLintptr offset
, GLsizeiptr size
,
295 bool mappedRange
, const char *caller
)
298 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
303 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
307 if (offset
+ size
> bufObj
->Size
) {
308 _mesa_error(ctx
, GL_INVALID_VALUE
,
309 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
310 (unsigned long) offset
,
311 (unsigned long) size
,
312 (unsigned long) bufObj
->Size
);
316 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
320 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
321 _mesa_error(ctx
, GL_INVALID_OPERATION
,
322 "%s(range is mapped without persistent bit)",
328 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
329 _mesa_error(ctx
, GL_INVALID_OPERATION
,
330 "%s(buffer is mapped without persistent bit)",
341 * Test the format and type parameters and set the GL error code for
342 * \c glClearBufferData and \c glClearBufferSubData.
344 * \param ctx GL context.
345 * \param internalformat Format to which the data is to be converted.
346 * \param format Format of the supplied data.
347 * \param type Type of the supplied data.
348 * \param caller Name of calling function for recording errors.
349 * \return If internalformat, format and type are legal the mesa_format
350 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
352 * \sa glClearBufferData and glClearBufferSubData
355 validate_clear_buffer_format(struct gl_context
*ctx
,
356 GLenum internalformat
,
357 GLenum format
, GLenum type
,
360 mesa_format mesaFormat
;
361 GLenum errorFormatType
;
363 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
364 if (mesaFormat
== MESA_FORMAT_NONE
) {
365 _mesa_error(ctx
, GL_INVALID_ENUM
,
366 "%s(invalid internalformat)", caller
);
367 return MESA_FORMAT_NONE
;
370 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
371 * EXT_texture_integer there is no conversion between integer and
372 * non-integer formats
374 if (_mesa_is_enum_format_signed_int(format
) !=
375 _mesa_is_format_integer_color(mesaFormat
)) {
376 _mesa_error(ctx
, GL_INVALID_OPERATION
,
377 "%s(integer vs non-integer)", caller
);
378 return MESA_FORMAT_NONE
;
381 if (!_mesa_is_color_format(format
)) {
382 _mesa_error(ctx
, GL_INVALID_ENUM
,
383 "%s(format is not a color format)", caller
);
384 return MESA_FORMAT_NONE
;
387 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
388 if (errorFormatType
!= GL_NO_ERROR
) {
389 _mesa_error(ctx
, GL_INVALID_ENUM
,
390 "%s(invalid format or type)", caller
);
391 return MESA_FORMAT_NONE
;
399 * Convert user-specified clear value to the specified internal format.
401 * \param ctx GL context.
402 * \param internalformat Format to which the data is converted.
403 * \param clearValue Points to the converted clear value.
404 * \param format Format of the supplied data.
405 * \param type Type of the supplied data.
406 * \param data Data which is to be converted to internalformat.
407 * \param caller Name of calling function for recording errors.
408 * \return true if data could be converted, false otherwise.
410 * \sa glClearBufferData, glClearBufferSubData
413 convert_clear_buffer_data(struct gl_context
*ctx
,
414 mesa_format internalformat
,
415 GLubyte
*clearValue
, GLenum format
, GLenum type
,
416 const GLvoid
*data
, const char *caller
)
418 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
420 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
421 0, &clearValue
, 1, 1, 1,
422 format
, type
, data
, &ctx
->Unpack
)) {
426 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
433 * Allocate and initialize a new buffer object.
435 * Default callback for the \c dd_function_table::NewBufferObject() hook.
437 static struct gl_buffer_object
*
438 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
440 struct gl_buffer_object
*obj
;
444 obj
= MALLOC_STRUCT(gl_buffer_object
);
445 _mesa_initialize_buffer_object(ctx
, obj
, name
);
451 * Delete a buffer object.
453 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
456 _mesa_delete_buffer_object(struct gl_context
*ctx
,
457 struct gl_buffer_object
*bufObj
)
461 _mesa_align_free(bufObj
->Data
);
463 /* assign strange values here to help w/ debugging */
464 bufObj
->RefCount
= -1000;
467 mtx_destroy(&bufObj
->Mutex
);
475 * Set ptr to bufObj w/ reference counting.
476 * This is normally only called from the _mesa_reference_buffer_object() macro
477 * when there's a real pointer change.
480 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
481 struct gl_buffer_object
**ptr
,
482 struct gl_buffer_object
*bufObj
)
485 /* Unreference the old buffer */
486 GLboolean deleteFlag
= GL_FALSE
;
487 struct gl_buffer_object
*oldObj
= *ptr
;
489 mtx_lock(&oldObj
->Mutex
);
490 assert(oldObj
->RefCount
> 0);
492 deleteFlag
= (oldObj
->RefCount
== 0);
493 mtx_unlock(&oldObj
->Mutex
);
496 assert(ctx
->Driver
.DeleteBuffer
);
497 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
505 /* reference new buffer */
506 mtx_lock(&bufObj
->Mutex
);
507 if (bufObj
->RefCount
== 0) {
508 /* this buffer's being deleted (look just above) */
509 /* Not sure this can every really happen. Warn if it does. */
510 _mesa_problem(NULL
, "referencing deleted buffer object");
517 mtx_unlock(&bufObj
->Mutex
);
523 * Initialize a buffer object to default values.
526 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
527 struct gl_buffer_object
*obj
,
530 memset(obj
, 0, sizeof(struct gl_buffer_object
));
531 mtx_init(&obj
->Mutex
, mtx_plain
);
534 obj
->Usage
= GL_STATIC_DRAW_ARB
;
540 * Callback called from _mesa_HashWalk()
543 count_buffer_size(GLuint key
, void *data
, void *userData
)
545 const struct gl_buffer_object
*bufObj
=
546 (const struct gl_buffer_object
*) data
;
547 GLuint
*total
= (GLuint
*) userData
;
550 *total
= *total
+ bufObj
->Size
;
555 * Compute total size (in bytes) of all buffer objects for the given context.
556 * For debugging purposes.
559 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
563 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
570 * Allocate space for and store data in a buffer object. Any data that was
571 * previously stored in the buffer object is lost. If \c data is \c NULL,
572 * memory will be allocated, but no copy will occur.
574 * This is the default callback for \c dd_function_table::BufferData()
575 * Note that all GL error checking will have been done already.
577 * \param ctx GL context.
578 * \param target Buffer object target on which to operate.
579 * \param size Size, in bytes, of the new data store.
580 * \param data Pointer to the data to store in the buffer object. This
581 * pointer may be \c NULL.
582 * \param usage Hints about how the data will be used.
583 * \param bufObj Object to be used.
585 * \return GL_TRUE for success, GL_FALSE for failure
586 * \sa glBufferDataARB, dd_function_table::BufferData.
589 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptr size
,
590 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
591 struct gl_buffer_object
*bufObj
)
597 _mesa_align_free( bufObj
->Data
);
599 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
601 bufObj
->Data
= (GLubyte
*) new_data
;
603 bufObj
->Usage
= usage
;
604 bufObj
->StorageFlags
= storageFlags
;
607 memcpy( bufObj
->Data
, data
, size
);
619 * Replace data in a subrange of buffer object. If the data range
620 * specified by \c size + \c offset extends beyond the end of the buffer or
621 * if \c data is \c NULL, no copy is performed.
623 * This is the default callback for \c dd_function_table::BufferSubData()
624 * Note that all GL error checking will have been done already.
626 * \param ctx GL context.
627 * \param offset Offset of the first byte to be modified.
628 * \param size Size, in bytes, of the data range.
629 * \param data Pointer to the data to store in the buffer object.
630 * \param bufObj Object to be used.
632 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
635 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptr offset
,
636 GLsizeiptr size
, const GLvoid
*data
,
637 struct gl_buffer_object
*bufObj
)
641 /* this should have been caught in _mesa_BufferSubData() */
642 assert(size
+ offset
<= bufObj
->Size
);
645 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
651 * Retrieve data from a subrange of buffer object. If the data range
652 * specified by \c size + \c offset extends beyond the end of the buffer or
653 * if \c data is \c NULL, no copy is performed.
655 * This is the default callback for \c dd_function_table::GetBufferSubData()
656 * Note that all GL error checking will have been done already.
658 * \param ctx GL context.
659 * \param target Buffer object target on which to operate.
660 * \param offset Offset of the first byte to be fetched.
661 * \param size Size, in bytes, of the data range.
662 * \param data Destination for data
663 * \param bufObj Object to be used.
665 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
668 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
669 GLsizeiptrARB size
, GLvoid
* data
,
670 struct gl_buffer_object
* bufObj
)
674 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
675 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
681 * Clear a subrange of the buffer object with copies of the supplied data.
682 * If data is NULL the buffer is filled with zeros.
684 * This is the default callback for \c dd_function_table::ClearBufferSubData()
685 * Note that all GL error checking will have been done already.
687 * \param ctx GL context.
688 * \param offset Offset of the first byte to be cleared.
689 * \param size Size, in bytes, of the to be cleared range.
690 * \param clearValue Source of the data.
691 * \param clearValueSize Size, in bytes, of the supplied data.
692 * \param bufObj Object to be cleared.
694 * \sa glClearBufferSubData, glClearBufferData and
695 * dd_function_table::ClearBufferSubData.
698 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
699 GLintptr offset
, GLsizeiptr size
,
700 const GLvoid
*clearValue
,
701 GLsizeiptr clearValueSize
,
702 struct gl_buffer_object
*bufObj
)
707 assert(ctx
->Driver
.MapBufferRange
);
708 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
710 GL_MAP_INVALIDATE_RANGE_BIT
,
711 bufObj
, MAP_INTERNAL
);
714 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
718 if (clearValue
== NULL
) {
719 /* Clear with zeros, per the spec */
720 memset(dest
, 0, size
);
721 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
725 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
726 memcpy(dest
, clearValue
, clearValueSize
);
727 dest
+= clearValueSize
;
730 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
735 * Default fallback for \c dd_function_table::MapBufferRange().
736 * Called via glMapBufferRange().
739 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
740 GLsizeiptr length
, GLbitfield access
,
741 struct gl_buffer_object
*bufObj
,
742 gl_map_buffer_index index
)
745 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
746 /* Just return a direct pointer to the data */
747 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
748 bufObj
->Mappings
[index
].Length
= length
;
749 bufObj
->Mappings
[index
].Offset
= offset
;
750 bufObj
->Mappings
[index
].AccessFlags
= access
;
751 return bufObj
->Mappings
[index
].Pointer
;
756 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
757 * Called via glFlushMappedBufferRange().
760 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
761 GLintptr offset
, GLsizeiptr length
,
762 struct gl_buffer_object
*obj
,
763 gl_map_buffer_index index
)
775 * Default callback for \c dd_function_table::UnmapBuffer().
777 * The input parameters will have been already tested for errors.
779 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
782 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
783 gl_map_buffer_index index
)
786 /* XXX we might assert here that bufObj->Pointer is non-null */
787 bufObj
->Mappings
[index
].Pointer
= NULL
;
788 bufObj
->Mappings
[index
].Length
= 0;
789 bufObj
->Mappings
[index
].Offset
= 0;
790 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
796 * Default fallback for \c dd_function_table::CopyBufferSubData().
797 * Called via glCopyBufferSubData().
800 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
801 struct gl_buffer_object
*src
,
802 struct gl_buffer_object
*dst
,
803 GLintptr readOffset
, GLintptr writeOffset
,
806 GLubyte
*srcPtr
, *dstPtr
;
809 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
811 GL_MAP_WRITE_BIT
, src
,
817 srcPtr
+= readOffset
;
818 dstPtr
+= writeOffset
;
820 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
821 GL_MAP_READ_BIT
, src
,
823 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
825 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
829 /* Note: the src and dst regions will never overlap. Trying to do so
830 * would generate GL_INVALID_VALUE earlier.
832 if (srcPtr
&& dstPtr
)
833 memcpy(dstPtr
, srcPtr
, size
);
835 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
837 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
843 * Initialize the state associated with buffer objects
846 _mesa_init_buffer_objects( struct gl_context
*ctx
)
850 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
851 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
852 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
854 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
855 ctx
->Shared
->NullBufferObj
);
857 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
858 ctx
->Shared
->NullBufferObj
);
859 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
860 ctx
->Shared
->NullBufferObj
);
862 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
863 ctx
->Shared
->NullBufferObj
);
865 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
866 ctx
->Shared
->NullBufferObj
);
868 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
869 ctx
->Shared
->NullBufferObj
);
871 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
872 ctx
->Shared
->NullBufferObj
);
874 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
,
875 ctx
->Shared
->NullBufferObj
);
877 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
,
878 ctx
->Shared
->NullBufferObj
);
880 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
881 _mesa_reference_buffer_object(ctx
,
882 &ctx
->UniformBufferBindings
[i
].BufferObject
,
883 ctx
->Shared
->NullBufferObj
);
884 ctx
->UniformBufferBindings
[i
].Offset
= -1;
885 ctx
->UniformBufferBindings
[i
].Size
= -1;
888 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
889 _mesa_reference_buffer_object(ctx
,
890 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
891 ctx
->Shared
->NullBufferObj
);
892 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
893 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
896 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
897 _mesa_reference_buffer_object(ctx
,
898 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
899 ctx
->Shared
->NullBufferObj
);
900 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
901 ctx
->AtomicBufferBindings
[i
].Size
= 0;
907 _mesa_free_buffer_objects( struct gl_context
*ctx
)
911 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
913 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
914 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
916 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
918 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
920 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
922 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
924 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
, NULL
);
926 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
928 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
929 _mesa_reference_buffer_object(ctx
,
930 &ctx
->UniformBufferBindings
[i
].BufferObject
,
934 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
935 _mesa_reference_buffer_object(ctx
,
936 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
940 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
941 _mesa_reference_buffer_object(ctx
,
942 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
949 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
951 struct gl_buffer_object
**buf_handle
,
954 struct gl_buffer_object
*buf
= *buf_handle
;
956 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
)) {
957 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
961 if (!buf
|| buf
== &DummyBufferObject
) {
962 /* If this is a new buffer object id, or one which was generated but
963 * never used before, allocate a buffer object now.
965 assert(ctx
->Driver
.NewBufferObject
);
966 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
968 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
971 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
979 * Bind the specified target to buffer for the specified context.
980 * Called by glBindBuffer() and other functions.
983 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
985 struct gl_buffer_object
*oldBufObj
;
986 struct gl_buffer_object
*newBufObj
= NULL
;
987 struct gl_buffer_object
**bindTarget
= NULL
;
989 bindTarget
= get_buffer_target(ctx
, target
);
991 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
995 /* Get pointer to old buffer object (to be unbound) */
996 oldBufObj
= *bindTarget
;
997 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
998 return; /* rebinding the same buffer object- no change */
1001 * Get pointer to new buffer object (newBufObj)
1004 /* The spec says there's not a buffer object named 0, but we use
1005 * one internally because it simplifies things.
1007 newBufObj
= ctx
->Shared
->NullBufferObj
;
1010 /* non-default buffer object */
1011 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1012 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1013 &newBufObj
, "glBindBuffer"))
1017 /* record usage history */
1019 case GL_PIXEL_PACK_BUFFER
:
1020 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1026 /* bind new buffer */
1027 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1032 * Update the default buffer objects in the given context to reference those
1033 * specified in the shared state and release those referencing the old
1037 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1039 /* Bind the NullBufferObj to remove references to those
1040 * in the shared context hash table.
1042 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
1043 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
1044 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
1045 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1051 * Return the gl_buffer_object for the given ID.
1052 * Always return NULL for ID 0.
1054 struct gl_buffer_object
*
1055 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1060 return (struct gl_buffer_object
*)
1061 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1065 struct gl_buffer_object
*
1066 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1068 return (struct gl_buffer_object
*)
1069 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1073 * A convenience function for direct state access functions that throws
1074 * GL_INVALID_OPERATION if buffer is not the name of an existing
1077 struct gl_buffer_object
*
1078 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1081 struct gl_buffer_object
*bufObj
;
1083 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1084 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1085 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1086 "%s(non-existent buffer object %u)", caller
, buffer
);
1095 _mesa_begin_bufferobj_lookups(struct gl_context
*ctx
)
1097 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1102 _mesa_end_bufferobj_lookups(struct gl_context
*ctx
)
1104 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1109 * Look up a buffer object for a multi-bind function.
1111 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1112 * of generating an error if the buffer ID is not zero or the name
1113 * of an existing buffer object.
1115 * If the buffer ID refers to an existing buffer object, a pointer
1116 * to the buffer object is returned. If the ID is zero, a pointer
1117 * to the shared NullBufferObj is returned. If the ID is not zero
1118 * and does not refer to a valid buffer object, this function
1121 * This function assumes that the caller has already locked the
1122 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1124 struct gl_buffer_object
*
1125 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1126 const GLuint
*buffers
,
1127 GLuint index
, const char *caller
)
1129 struct gl_buffer_object
*bufObj
;
1131 if (buffers
[index
] != 0) {
1132 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1134 /* The multi-bind functions don't create the buffer objects
1135 when they don't exist. */
1136 if (bufObj
== &DummyBufferObject
)
1139 bufObj
= ctx
->Shared
->NullBufferObj
;
1142 /* The ARB_multi_bind spec says:
1144 * "An INVALID_OPERATION error is generated if any value
1145 * in <buffers> is not zero or the name of an existing
1146 * buffer object (per binding)."
1148 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1149 "%s(buffers[%u]=%u is not zero or the name "
1150 "of an existing buffer object)",
1151 caller
, index
, buffers
[index
]);
1159 * If *ptr points to obj, set ptr = the Null/default buffer object.
1160 * This is a helper for buffer object deletion.
1161 * The GL spec says that deleting a buffer object causes it to get
1162 * unbound from all arrays in the current context.
1165 unbind(struct gl_context
*ctx
,
1166 struct gl_buffer_object
**ptr
,
1167 struct gl_buffer_object
*obj
)
1170 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
1176 * Plug default/fallback buffer object functions into the device
1180 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1182 /* GL_ARB_vertex/pixel_buffer_object */
1183 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1184 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1185 driver
->BufferData
= buffer_data_fallback
;
1186 driver
->BufferSubData
= buffer_sub_data_fallback
;
1187 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1188 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1190 /* GL_ARB_clear_buffer_object */
1191 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1193 /* GL_ARB_map_buffer_range */
1194 driver
->MapBufferRange
= map_buffer_range_fallback
;
1195 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1197 /* GL_ARB_copy_buffer */
1198 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1203 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1204 struct gl_buffer_object
*bufObj
)
1208 for (i
= 0; i
< MAP_COUNT
; i
++) {
1209 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1210 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1211 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1212 bufObj
->Mappings
[i
].AccessFlags
= 0;
1218 /**********************************************************************/
1220 /**********************************************************************/
1223 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1225 GET_CURRENT_CONTEXT(ctx
);
1227 if (MESA_VERBOSE
& VERBOSE_API
) {
1228 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1229 _mesa_enum_to_string(target
), buffer
);
1232 bind_buffer_object(ctx
, target
, buffer
);
1237 * Delete a set of buffer objects.
1239 * \param n Number of buffer objects to delete.
1240 * \param ids Array of \c n buffer object IDs.
1243 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1245 GET_CURRENT_CONTEXT(ctx
);
1247 FLUSH_VERTICES(ctx
, 0);
1250 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1254 mtx_lock(&ctx
->Shared
->Mutex
);
1256 for (i
= 0; i
< n
; i
++) {
1257 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
1259 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1262 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1264 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1266 /* unbind any vertex pointers bound to this buffer */
1267 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1268 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1271 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1272 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1274 if (vao
->IndexBufferObj
== bufObj
) {
1275 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1278 /* unbind ARB_draw_indirect binding point */
1279 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1280 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1283 /* unbind ARB_indirect_parameters binding point */
1284 if (ctx
->ParameterBuffer
== bufObj
) {
1285 _mesa_BindBuffer(GL_PARAMETER_BUFFER_ARB
, 0);
1288 /* unbind ARB_compute_shader binding point */
1289 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1290 _mesa_BindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0);
1293 /* unbind ARB_copy_buffer binding points */
1294 if (ctx
->CopyReadBuffer
== bufObj
) {
1295 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1297 if (ctx
->CopyWriteBuffer
== bufObj
) {
1298 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1301 /* unbind transform feedback binding points */
1302 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1303 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1305 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1306 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1307 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1311 /* unbind UBO binding points */
1312 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1313 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1314 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1318 if (ctx
->UniformBuffer
== bufObj
) {
1319 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1322 /* unbind SSBO binding points */
1323 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1324 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1325 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1329 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1330 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER
, 0);
1333 /* unbind Atomci Buffer binding points */
1334 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1335 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1336 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1340 if (ctx
->AtomicBuffer
== bufObj
) {
1341 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1344 /* unbind any pixel pack/unpack pointers bound to this buffer */
1345 if (ctx
->Pack
.BufferObj
== bufObj
) {
1346 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1348 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1349 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1352 if (ctx
->Texture
.BufferObject
== bufObj
) {
1353 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1356 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1357 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1360 /* The ID is immediately freed for re-use */
1361 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1362 /* Make sure we do not run into the classic ABA problem on bind.
1363 * We don't want to allow re-binding a buffer object that's been
1364 * "deleted" by glDeleteBuffers().
1366 * The explicit rebinding to the default object in the current context
1367 * prevents the above in the current context, but another context
1368 * sharing the same objects might suffer from this problem.
1369 * The alternative would be to do the hash lookup in any case on bind
1370 * which would introduce more runtime overhead than this.
1372 bufObj
->DeletePending
= GL_TRUE
;
1373 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1377 mtx_unlock(&ctx
->Shared
->Mutex
);
1382 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1383 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1387 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1389 GET_CURRENT_CONTEXT(ctx
);
1392 struct gl_buffer_object
*buf
;
1394 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1396 if (MESA_VERBOSE
& VERBOSE_API
)
1397 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1400 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1409 * This must be atomic (generation and allocation of buffer object IDs)
1411 mtx_lock(&ctx
->Shared
->Mutex
);
1413 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1415 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1416 * DummyBufferObject. Otherwise, create a new buffer object and insert
1419 for (i
= 0; i
< n
; i
++) {
1420 buffers
[i
] = first
+ i
;
1422 assert(ctx
->Driver
.NewBufferObject
);
1423 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1425 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1426 mtx_unlock(&ctx
->Shared
->Mutex
);
1431 buf
= &DummyBufferObject
;
1433 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1436 mtx_unlock(&ctx
->Shared
->Mutex
);
1440 * Generate a set of unique buffer object IDs and store them in \c buffers.
1442 * \param n Number of IDs to generate.
1443 * \param buffers Array of \c n locations to store the IDs.
1446 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1448 create_buffers(n
, buffers
, false);
1452 * Create a set of buffer objects and store their unique IDs in \c buffers.
1454 * \param n Number of IDs to generate.
1455 * \param buffers Array of \c n locations to store the IDs.
1458 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1460 create_buffers(n
, buffers
, true);
1465 * Determine if ID is the name of a buffer object.
1467 * \param id ID of the potential buffer object.
1468 * \return \c GL_TRUE if \c id is the name of a buffer object,
1469 * \c GL_FALSE otherwise.
1471 GLboolean GLAPIENTRY
1472 _mesa_IsBuffer(GLuint id
)
1474 struct gl_buffer_object
*bufObj
;
1475 GET_CURRENT_CONTEXT(ctx
);
1476 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1478 mtx_lock(&ctx
->Shared
->Mutex
);
1479 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1480 mtx_unlock(&ctx
->Shared
->Mutex
);
1482 return bufObj
&& bufObj
!= &DummyBufferObject
;
1487 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1488 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1489 GLbitfield flags
, const char *func
)
1492 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1496 if (flags
& ~(GL_MAP_READ_BIT
|
1498 GL_MAP_PERSISTENT_BIT
|
1499 GL_MAP_COHERENT_BIT
|
1500 GL_DYNAMIC_STORAGE_BIT
|
1501 GL_CLIENT_STORAGE_BIT
)) {
1502 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1506 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1507 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1508 _mesa_error(ctx
, GL_INVALID_VALUE
,
1509 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1513 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1514 _mesa_error(ctx
, GL_INVALID_VALUE
,
1515 "%s(COHERENT and flags!=PERSISTENT)", func
);
1519 if (bufObj
->Immutable
) {
1520 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1524 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1525 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1527 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1529 bufObj
->Written
= GL_TRUE
;
1530 bufObj
->Immutable
= GL_TRUE
;
1532 assert(ctx
->Driver
.BufferData
);
1533 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1535 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1536 /* Even though the interaction between AMD_pinned_memory and
1537 * glBufferStorage is not described in the spec, Graham Sellers
1538 * said that it should behave the same as glBufferData.
1540 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1543 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1549 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1552 GET_CURRENT_CONTEXT(ctx
);
1553 struct gl_buffer_object
*bufObj
;
1555 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1559 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1564 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1567 GET_CURRENT_CONTEXT(ctx
);
1568 struct gl_buffer_object
*bufObj
;
1570 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1575 * In direct state access, buffer objects have an unspecified target since
1576 * they are not required to be bound.
1578 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1579 "glNamedBufferStorage");
1584 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1585 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1586 GLenum usage
, const char *func
)
1590 if (MESA_VERBOSE
& VERBOSE_API
) {
1591 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1593 _mesa_enum_to_string(target
),
1594 (long int) size
, data
,
1595 _mesa_enum_to_string(usage
));
1599 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1604 case GL_STREAM_DRAW_ARB
:
1605 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1608 case GL_STATIC_DRAW_ARB
:
1609 case GL_DYNAMIC_DRAW_ARB
:
1613 case GL_STREAM_READ_ARB
:
1614 case GL_STREAM_COPY_ARB
:
1615 case GL_STATIC_READ_ARB
:
1616 case GL_STATIC_COPY_ARB
:
1617 case GL_DYNAMIC_READ_ARB
:
1618 case GL_DYNAMIC_COPY_ARB
:
1619 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1623 valid_usage
= false;
1628 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1629 _mesa_enum_to_string(usage
));
1633 if (bufObj
->Immutable
) {
1634 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1638 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1639 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1641 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1643 bufObj
->Written
= GL_TRUE
;
1646 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1647 bufObj
->Name
, size
, data
, usage
);
1654 assert(ctx
->Driver
.BufferData
);
1655 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1658 GL_DYNAMIC_STORAGE_BIT
,
1660 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1661 /* From GL_AMD_pinned_memory:
1663 * INVALID_OPERATION is generated by BufferData if <target> is
1664 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1665 * mapped to the GPU address space.
1667 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1670 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1676 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1677 const GLvoid
*data
, GLenum usage
)
1679 GET_CURRENT_CONTEXT(ctx
);
1680 struct gl_buffer_object
*bufObj
;
1682 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1686 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1691 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1694 GET_CURRENT_CONTEXT(ctx
);
1695 struct gl_buffer_object
*bufObj
;
1697 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1701 /* In direct state access, buffer objects have an unspecified target since
1702 * they are not required to be bound.
1704 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1705 "glNamedBufferData");
1710 * Implementation for glBufferSubData and glNamedBufferSubData.
1712 * \param ctx GL context.
1713 * \param bufObj The buffer object.
1714 * \param offset Offset of the first byte of the subdata range.
1715 * \param size Size, in bytes, of the subdata range.
1716 * \param data The data store.
1717 * \param func Name of calling function for recording errors.
1721 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1722 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1725 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1727 /* error already recorded */
1731 if (bufObj
->Immutable
&&
1732 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1733 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1740 bufObj
->NumSubDataCalls
++;
1742 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
1743 bufObj
->Usage
== GL_STATIC_COPY
) &&
1744 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
) {
1745 /* If the application declared the buffer as static draw/copy or stream
1746 * draw, it should not be frequently modified with glBufferSubData.
1748 BUFFER_USAGE_WARNING(ctx
,
1749 "using %s(buffer %u, offset %u, size %u) to "
1750 "update a %s buffer",
1751 func
, bufObj
->Name
, offset
, size
,
1752 _mesa_enum_to_string(bufObj
->Usage
));
1755 bufObj
->Written
= GL_TRUE
;
1757 assert(ctx
->Driver
.BufferSubData
);
1758 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1762 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1763 GLsizeiptr size
, const GLvoid
*data
)
1765 GET_CURRENT_CONTEXT(ctx
);
1766 struct gl_buffer_object
*bufObj
;
1768 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1772 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1776 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1777 GLsizeiptr size
, const GLvoid
*data
)
1779 GET_CURRENT_CONTEXT(ctx
);
1780 struct gl_buffer_object
*bufObj
;
1782 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1786 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1787 "glNamedBufferSubData");
1792 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1793 GLsizeiptr size
, GLvoid
*data
)
1795 GET_CURRENT_CONTEXT(ctx
);
1796 struct gl_buffer_object
*bufObj
;
1798 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1799 GL_INVALID_OPERATION
);
1803 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1804 "glGetBufferSubData")) {
1808 assert(ctx
->Driver
.GetBufferSubData
);
1809 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1813 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1814 GLsizeiptr size
, GLvoid
*data
)
1816 GET_CURRENT_CONTEXT(ctx
);
1817 struct gl_buffer_object
*bufObj
;
1819 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1820 "glGetNamedBufferSubData");
1824 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1825 "glGetNamedBufferSubData")) {
1829 assert(ctx
->Driver
.GetBufferSubData
);
1830 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1835 * \param subdata true if caller is *SubData, false if *Data
1838 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1839 struct gl_buffer_object
*bufObj
,
1840 GLenum internalformat
,
1841 GLintptr offset
, GLsizeiptr size
,
1842 GLenum format
, GLenum type
,
1844 const char *func
, bool subdata
)
1846 mesa_format mesaFormat
;
1847 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1848 GLsizeiptr clearValueSize
;
1850 /* This checks for disallowed mappings. */
1851 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1856 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1857 format
, type
, func
);
1859 if (mesaFormat
== MESA_FORMAT_NONE
) {
1863 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1864 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1865 _mesa_error(ctx
, GL_INVALID_VALUE
,
1866 "%s(offset or size is not a multiple of "
1867 "internalformat size)", func
);
1871 /* Bail early. Negative size has already been checked. */
1876 /* clear to zeros, per the spec */
1877 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1878 NULL
, clearValueSize
, bufObj
);
1882 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1883 format
, type
, data
, func
)) {
1887 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1888 clearValue
, clearValueSize
, bufObj
);
1892 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1893 GLenum type
, const GLvoid
*data
)
1895 GET_CURRENT_CONTEXT(ctx
);
1896 struct gl_buffer_object
*bufObj
;
1898 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1902 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1904 "glClearBufferData", false);
1908 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1909 GLenum format
, GLenum type
, const GLvoid
*data
)
1911 GET_CURRENT_CONTEXT(ctx
);
1912 struct gl_buffer_object
*bufObj
;
1914 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1918 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1920 "glClearNamedBufferData", false);
1925 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1926 GLintptr offset
, GLsizeiptr size
,
1927 GLenum format
, GLenum type
,
1930 GET_CURRENT_CONTEXT(ctx
);
1931 struct gl_buffer_object
*bufObj
;
1933 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1937 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1939 "glClearBufferSubData", true);
1943 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1944 GLintptr offset
, GLsizeiptr size
,
1945 GLenum format
, GLenum type
,
1948 GET_CURRENT_CONTEXT(ctx
);
1949 struct gl_buffer_object
*bufObj
;
1951 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1952 "glClearNamedBufferSubData");
1956 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1958 "glClearNamedBufferSubData", true);
1963 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1966 GLboolean status
= GL_TRUE
;
1967 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1969 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
1970 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1971 "%s(buffer is not mapped)", func
);
1976 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1977 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1979 /* check that last 100 bytes are still = magic value */
1980 for (i
= 0; i
< 100; i
++) {
1981 GLuint pos
= bufObj
->Size
- i
- 1;
1982 if (buf
[pos
] != 123) {
1983 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1984 " at position %d (value = %u)\n",
1992 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1993 GLuint i
, unchanged
= 0;
1994 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1996 /* check which bytes changed */
1997 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1998 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2005 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2006 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2011 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2012 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2013 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2014 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2015 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2020 GLboolean GLAPIENTRY
2021 _mesa_UnmapBuffer(GLenum target
)
2023 GET_CURRENT_CONTEXT(ctx
);
2024 struct gl_buffer_object
*bufObj
;
2026 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2030 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2033 GLboolean GLAPIENTRY
2034 _mesa_UnmapNamedBuffer(GLuint buffer
)
2036 GET_CURRENT_CONTEXT(ctx
);
2037 struct gl_buffer_object
*bufObj
;
2039 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2043 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2048 get_buffer_parameter(struct gl_context
*ctx
,
2049 struct gl_buffer_object
*bufObj
, GLenum pname
,
2050 GLint64
*params
, const char *func
)
2053 case GL_BUFFER_SIZE_ARB
:
2054 *params
= bufObj
->Size
;
2056 case GL_BUFFER_USAGE_ARB
:
2057 *params
= bufObj
->Usage
;
2059 case GL_BUFFER_ACCESS_ARB
:
2060 *params
= simplified_access_mode(ctx
,
2061 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2063 case GL_BUFFER_MAPPED_ARB
:
2064 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2066 case GL_BUFFER_ACCESS_FLAGS
:
2067 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2069 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2071 case GL_BUFFER_MAP_OFFSET
:
2072 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2074 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2076 case GL_BUFFER_MAP_LENGTH
:
2077 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2079 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2081 case GL_BUFFER_IMMUTABLE_STORAGE
:
2082 if (!ctx
->Extensions
.ARB_buffer_storage
)
2084 *params
= bufObj
->Immutable
;
2086 case GL_BUFFER_STORAGE_FLAGS
:
2087 if (!ctx
->Extensions
.ARB_buffer_storage
)
2089 *params
= bufObj
->StorageFlags
;
2098 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2099 _mesa_enum_to_string(pname
));
2104 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2106 GET_CURRENT_CONTEXT(ctx
);
2107 struct gl_buffer_object
*bufObj
;
2110 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2111 GL_INVALID_OPERATION
);
2115 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2116 "glGetBufferParameteriv"))
2117 return; /* Error already recorded. */
2119 *params
= (GLint
) parameter
;
2123 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2125 GET_CURRENT_CONTEXT(ctx
);
2126 struct gl_buffer_object
*bufObj
;
2129 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2130 GL_INVALID_OPERATION
);
2134 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2135 "glGetBufferParameteri64v"))
2136 return; /* Error already recorded. */
2138 *params
= parameter
;
2142 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2144 GET_CURRENT_CONTEXT(ctx
);
2145 struct gl_buffer_object
*bufObj
;
2148 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2149 "glGetNamedBufferParameteriv");
2153 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2154 "glGetNamedBufferParameteriv"))
2155 return; /* Error already recorded. */
2157 *params
= (GLint
) parameter
;
2161 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2164 GET_CURRENT_CONTEXT(ctx
);
2165 struct gl_buffer_object
*bufObj
;
2168 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2169 "glGetNamedBufferParameteri64v");
2173 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2174 "glGetNamedBufferParameteri64v"))
2175 return; /* Error already recorded. */
2177 *params
= parameter
;
2182 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2184 GET_CURRENT_CONTEXT(ctx
);
2185 struct gl_buffer_object
*bufObj
;
2187 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2188 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2189 "GL_BUFFER_MAP_POINTER)");
2193 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2194 GL_INVALID_OPERATION
);
2198 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2202 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2204 GET_CURRENT_CONTEXT(ctx
);
2205 struct gl_buffer_object
*bufObj
;
2207 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2208 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2209 "GL_BUFFER_MAP_POINTER)");
2213 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2214 "glGetNamedBufferPointerv");
2218 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2223 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2224 struct gl_buffer_object
*src
,
2225 struct gl_buffer_object
*dst
,
2226 GLintptr readOffset
, GLintptr writeOffset
,
2227 GLsizeiptr size
, const char *func
)
2229 if (_mesa_check_disallowed_mapping(src
)) {
2230 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2231 "%s(readBuffer is mapped)", func
);
2235 if (_mesa_check_disallowed_mapping(dst
)) {
2236 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2237 "%s(writeBuffer is mapped)", func
);
2241 if (readOffset
< 0) {
2242 _mesa_error(ctx
, GL_INVALID_VALUE
,
2243 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2247 if (writeOffset
< 0) {
2248 _mesa_error(ctx
, GL_INVALID_VALUE
,
2249 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2254 _mesa_error(ctx
, GL_INVALID_VALUE
,
2255 "%s(size %d < 0)", func
, (int) size
);
2259 if (readOffset
+ size
> src
->Size
) {
2260 _mesa_error(ctx
, GL_INVALID_VALUE
,
2261 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2262 (int) readOffset
, (int) size
, (int) src
->Size
);
2266 if (writeOffset
+ size
> dst
->Size
) {
2267 _mesa_error(ctx
, GL_INVALID_VALUE
,
2268 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2269 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2274 if (readOffset
+ size
<= writeOffset
) {
2277 else if (writeOffset
+ size
<= readOffset
) {
2281 /* overlapping src/dst is illegal */
2282 _mesa_error(ctx
, GL_INVALID_VALUE
,
2283 "%s(overlapping src/dst)", func
);
2288 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2292 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2293 GLintptr readOffset
, GLintptr writeOffset
,
2296 GET_CURRENT_CONTEXT(ctx
);
2297 struct gl_buffer_object
*src
, *dst
;
2299 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2300 GL_INVALID_OPERATION
);
2304 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2305 GL_INVALID_OPERATION
);
2309 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2310 "glCopyBufferSubData");
2314 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2315 GLintptr readOffset
, GLintptr writeOffset
,
2318 GET_CURRENT_CONTEXT(ctx
);
2319 struct gl_buffer_object
*src
, *dst
;
2321 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2322 "glCopyNamedBufferSubData");
2326 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2327 "glCopyNamedBufferSubData");
2331 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2332 "glCopyNamedBufferSubData");
2337 _mesa_map_buffer_range(struct gl_context
*ctx
,
2338 struct gl_buffer_object
*bufObj
,
2339 GLintptr offset
, GLsizeiptr length
,
2340 GLbitfield access
, const char *func
)
2343 GLbitfield allowed_access
;
2345 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2348 _mesa_error(ctx
, GL_INVALID_VALUE
,
2349 "%s(offset %ld < 0)", func
, (long) offset
);
2354 _mesa_error(ctx
, GL_INVALID_VALUE
,
2355 "%s(length %ld < 0)", func
, (long) length
);
2359 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2361 * "An INVALID_OPERATION error is generated for any of the following
2364 * * <length> is zero."
2366 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2367 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2371 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2375 allowed_access
= GL_MAP_READ_BIT
|
2377 GL_MAP_INVALIDATE_RANGE_BIT
|
2378 GL_MAP_INVALIDATE_BUFFER_BIT
|
2379 GL_MAP_FLUSH_EXPLICIT_BIT
|
2380 GL_MAP_UNSYNCHRONIZED_BIT
;
2382 if (ctx
->Extensions
.ARB_buffer_storage
) {
2383 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2384 GL_MAP_COHERENT_BIT
;
2387 if (access
& ~allowed_access
) {
2388 /* generate an error if any bits other than those allowed are set */
2389 _mesa_error(ctx
, GL_INVALID_VALUE
,
2390 "%s(access has undefined bits set)", func
);
2394 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2395 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2396 "%s(access indicates neither read or write)", func
);
2400 if ((access
& GL_MAP_READ_BIT
) &&
2401 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2402 GL_MAP_INVALIDATE_BUFFER_BIT
|
2403 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2404 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2405 "%s(read access with disallowed bits)", func
);
2409 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2410 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2411 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2412 "%s(access has flush explicit without write)", func
);
2416 if (access
& GL_MAP_READ_BIT
&&
2417 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2418 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2419 "%s(buffer does not allow read access)", func
);
2423 if (access
& GL_MAP_WRITE_BIT
&&
2424 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2425 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2426 "%s(buffer does not allow write access)", func
);
2430 if (access
& GL_MAP_COHERENT_BIT
&&
2431 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2432 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2433 "%s(buffer does not allow coherent access)", func
);
2437 if (access
& GL_MAP_PERSISTENT_BIT
&&
2438 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2439 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2440 "%s(buffer does not allow persistent access)", func
);
2444 if (offset
+ length
> bufObj
->Size
) {
2445 _mesa_error(ctx
, GL_INVALID_VALUE
,
2446 "%s(offset %td + length %td > buffer_size %td)", func
,
2447 offset
, length
, bufObj
->Size
);
2451 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2452 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2453 "%s(buffer already mapped)", func
);
2457 if (!bufObj
->Size
) {
2458 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2462 if (access
& GL_MAP_WRITE_BIT
) {
2463 bufObj
->NumMapBufferWriteCalls
++;
2464 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2465 bufObj
->Usage
== GL_STATIC_COPY
) &&
2466 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
2467 BUFFER_USAGE_WARNING(ctx
,
2468 "using %s(buffer %u, offset %u, length %u) to "
2469 "update a %s buffer",
2470 func
, bufObj
->Name
, offset
, length
,
2471 _mesa_enum_to_string(bufObj
->Usage
));
2475 assert(ctx
->Driver
.MapBufferRange
);
2476 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2479 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2482 /* The driver callback should have set all these fields.
2483 * This is important because other modules (like VBO) might call
2484 * the driver function directly.
2486 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2487 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2488 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2489 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2492 if (access
& GL_MAP_WRITE_BIT
)
2493 bufObj
->Written
= GL_TRUE
;
2496 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2497 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2498 bufObj
->Name
, bufObj
->Size
, access
);
2499 /* Access must be write only */
2500 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2502 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2503 for (i
= 0; i
< bufObj
->Size
; i
++)
2510 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2511 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2513 /* buffer is 100 bytes larger than requested, fill with magic value */
2514 for (i
= 0; i
< 100; i
++) {
2515 buf
[bufObj
->Size
- i
- 1] = 123;
2524 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2527 GET_CURRENT_CONTEXT(ctx
);
2528 struct gl_buffer_object
*bufObj
;
2530 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2531 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2532 "glMapBufferRange(ARB_map_buffer_range not supported)");
2536 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2540 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2541 "glMapBufferRange");
2545 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2548 GET_CURRENT_CONTEXT(ctx
);
2549 struct gl_buffer_object
*bufObj
;
2551 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2552 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2553 "glMapNamedBufferRange("
2554 "ARB_map_buffer_range not supported)");
2558 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2562 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2563 "glMapNamedBufferRange");
2567 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2568 * flags for input to _mesa_map_buffer_range.
2570 * \return true if the type of requested access is permissible.
2573 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2577 case GL_READ_ONLY_ARB
:
2578 *flags
= GL_MAP_READ_BIT
;
2579 return _mesa_is_desktop_gl(ctx
);
2580 case GL_WRITE_ONLY_ARB
:
2581 *flags
= GL_MAP_WRITE_BIT
;
2583 case GL_READ_WRITE_ARB
:
2584 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2585 return _mesa_is_desktop_gl(ctx
);
2592 _mesa_MapBuffer(GLenum target
, GLenum access
)
2594 GET_CURRENT_CONTEXT(ctx
);
2595 struct gl_buffer_object
*bufObj
;
2596 GLbitfield accessFlags
;
2598 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2599 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2603 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2607 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2612 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2614 GET_CURRENT_CONTEXT(ctx
);
2615 struct gl_buffer_object
*bufObj
;
2616 GLbitfield accessFlags
;
2618 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2619 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2623 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2627 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2628 "glMapNamedBuffer");
2633 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2634 struct gl_buffer_object
*bufObj
,
2635 GLintptr offset
, GLsizeiptr length
,
2638 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2639 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2640 "%s(ARB_map_buffer_range not supported)", func
);
2645 _mesa_error(ctx
, GL_INVALID_VALUE
,
2646 "%s(offset %ld < 0)", func
, (long) offset
);
2651 _mesa_error(ctx
, GL_INVALID_VALUE
,
2652 "%s(length %ld < 0)", func
, (long) length
);
2656 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2657 /* buffer is not mapped */
2658 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2659 "%s(buffer is not mapped)", func
);
2663 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2664 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2665 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2666 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2670 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2671 _mesa_error(ctx
, GL_INVALID_VALUE
,
2672 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2673 (long) offset
, (long) length
,
2674 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2678 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2680 if (ctx
->Driver
.FlushMappedBufferRange
)
2681 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2686 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2689 GET_CURRENT_CONTEXT(ctx
);
2690 struct gl_buffer_object
*bufObj
;
2692 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2693 GL_INVALID_OPERATION
);
2697 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2698 "glFlushMappedBufferRange");
2702 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2705 GET_CURRENT_CONTEXT(ctx
);
2706 struct gl_buffer_object
*bufObj
;
2708 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2709 "glFlushMappedNamedBufferRange");
2713 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2714 "glFlushMappedNamedBufferRange");
2719 * Binds a buffer object to a uniform buffer binding point.
2721 * The caller is responsible for flushing vertices and updating
2725 set_ubo_binding(struct gl_context
*ctx
,
2726 struct gl_uniform_buffer_binding
*binding
,
2727 struct gl_buffer_object
*bufObj
,
2732 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2734 binding
->Offset
= offset
;
2735 binding
->Size
= size
;
2736 binding
->AutomaticSize
= autoSize
;
2738 /* If this is a real buffer object, mark it has having been used
2739 * at some point as a UBO.
2742 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2746 * Binds a buffer object to a shader storage buffer binding point.
2748 * The caller is responsible for flushing vertices and updating
2752 set_ssbo_binding(struct gl_context
*ctx
,
2753 struct gl_shader_storage_buffer_binding
*binding
,
2754 struct gl_buffer_object
*bufObj
,
2759 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2761 binding
->Offset
= offset
;
2762 binding
->Size
= size
;
2763 binding
->AutomaticSize
= autoSize
;
2765 /* If this is a real buffer object, mark it has having been used
2766 * at some point as a SSBO.
2769 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
2773 * Binds a buffer object to a uniform buffer binding point.
2775 * Unlike set_ubo_binding(), this function also flushes vertices
2776 * and updates NewDriverState. It also checks if the binding
2777 * has actually changed before updating it.
2780 bind_uniform_buffer(struct gl_context
*ctx
,
2782 struct gl_buffer_object
*bufObj
,
2787 struct gl_uniform_buffer_binding
*binding
=
2788 &ctx
->UniformBufferBindings
[index
];
2790 if (binding
->BufferObject
== bufObj
&&
2791 binding
->Offset
== offset
&&
2792 binding
->Size
== size
&&
2793 binding
->AutomaticSize
== autoSize
) {
2797 FLUSH_VERTICES(ctx
, 0);
2798 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2800 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2804 * Binds a buffer object to a shader storage buffer binding point.
2806 * Unlike set_ssbo_binding(), this function also flushes vertices
2807 * and updates NewDriverState. It also checks if the binding
2808 * has actually changed before updating it.
2811 bind_shader_storage_buffer(struct gl_context
*ctx
,
2813 struct gl_buffer_object
*bufObj
,
2818 struct gl_shader_storage_buffer_binding
*binding
=
2819 &ctx
->ShaderStorageBufferBindings
[index
];
2821 if (binding
->BufferObject
== bufObj
&&
2822 binding
->Offset
== offset
&&
2823 binding
->Size
== size
&&
2824 binding
->AutomaticSize
== autoSize
) {
2828 FLUSH_VERTICES(ctx
, 0);
2829 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
2831 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2835 * Bind a region of a buffer object to a uniform block binding point.
2836 * \param index the uniform buffer binding point index
2837 * \param bufObj the buffer object
2838 * \param offset offset to the start of buffer object region
2839 * \param size size of the buffer object region
2842 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2844 struct gl_buffer_object
*bufObj
,
2848 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2849 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2853 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2854 _mesa_error(ctx
, GL_INVALID_VALUE
,
2855 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2856 ctx
->Const
.UniformBufferOffsetAlignment
);
2860 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2865 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2866 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2870 * Bind a region of a buffer object to a shader storage block binding point.
2871 * \param index the shader storage buffer binding point index
2872 * \param bufObj the buffer object
2873 * \param offset offset to the start of buffer object region
2874 * \param size size of the buffer object region
2877 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
2879 struct gl_buffer_object
*bufObj
,
2883 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2884 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2888 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
2889 _mesa_error(ctx
, GL_INVALID_VALUE
,
2890 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2891 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
2895 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2900 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2901 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2905 * Bind a buffer object to a uniform block binding point.
2906 * As above, but offset = 0.
2909 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2911 struct gl_buffer_object
*bufObj
)
2913 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2914 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2918 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2920 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2921 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2923 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2927 * Bind a buffer object to a shader storage block binding point.
2928 * As above, but offset = 0.
2931 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
2933 struct gl_buffer_object
*bufObj
)
2935 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2936 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2940 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2942 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2943 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2945 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2949 * Binds a buffer object to an atomic buffer binding point.
2951 * The caller is responsible for validating the offset,
2952 * flushing the vertices and updating NewDriverState.
2955 set_atomic_buffer_binding(struct gl_context
*ctx
,
2956 struct gl_atomic_buffer_binding
*binding
,
2957 struct gl_buffer_object
*bufObj
,
2961 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2963 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2964 binding
->Offset
= -1;
2967 binding
->Offset
= offset
;
2968 binding
->Size
= size
;
2969 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
2974 * Binds a buffer object to an atomic buffer binding point.
2976 * Unlike set_atomic_buffer_binding(), this function also validates the
2977 * index and offset, flushes vertices, and updates NewDriverState.
2978 * It also checks if the binding has actually changing before
2982 bind_atomic_buffer(struct gl_context
*ctx
,
2984 struct gl_buffer_object
*bufObj
,
2989 struct gl_atomic_buffer_binding
*binding
;
2991 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
2992 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
2996 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
2997 _mesa_error(ctx
, GL_INVALID_VALUE
,
2998 "%s(offset misaligned %d/%d)", name
, (int) offset
,
2999 ATOMIC_COUNTER_SIZE
);
3003 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3005 binding
= &ctx
->AtomicBufferBindings
[index
];
3006 if (binding
->BufferObject
== bufObj
&&
3007 binding
->Offset
== offset
&&
3008 binding
->Size
== size
) {
3012 FLUSH_VERTICES(ctx
, 0);
3013 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3015 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3019 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3021 const GLintptr
*offsets
,
3022 const GLsizeiptr
*sizes
)
3024 if (offsets
[index
] < 0) {
3025 /* The ARB_multi_bind spec says:
3027 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3028 * value in <offsets> is less than zero (per binding)."
3030 _mesa_error(ctx
, GL_INVALID_VALUE
,
3031 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3032 index
, (int64_t) offsets
[index
]);
3036 if (sizes
[index
] <= 0) {
3037 /* The ARB_multi_bind spec says:
3039 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3040 * value in <sizes> is less than or equal to zero (per binding)."
3042 _mesa_error(ctx
, GL_INVALID_VALUE
,
3043 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3044 index
, (int64_t) sizes
[index
]);
3052 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3053 GLuint first
, GLsizei count
,
3056 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3057 _mesa_error(ctx
, GL_INVALID_ENUM
,
3058 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3062 /* The ARB_multi_bind_spec says:
3064 * "An INVALID_OPERATION error is generated if <first> + <count> is
3065 * greater than the number of target-specific indexed binding points,
3066 * as described in section 6.7.1."
3068 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3069 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3070 "%s(first=%u + count=%d > the value of "
3071 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3072 caller
, first
, count
,
3073 ctx
->Const
.MaxUniformBufferBindings
);
3081 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3082 GLuint first
, GLsizei count
,
3085 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3086 _mesa_error(ctx
, GL_INVALID_ENUM
,
3087 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3091 /* The ARB_multi_bind_spec says:
3093 * "An INVALID_OPERATION error is generated if <first> + <count> is
3094 * greater than the number of target-specific indexed binding points,
3095 * as described in section 6.7.1."
3097 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3098 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3099 "%s(first=%u + count=%d > the value of "
3100 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3101 caller
, first
, count
,
3102 ctx
->Const
.MaxShaderStorageBufferBindings
);
3110 * Unbind all uniform buffers in the range
3111 * <first> through <first>+<count>-1
3114 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3116 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3119 for (i
= 0; i
< count
; i
++)
3120 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3121 bufObj
, -1, -1, GL_TRUE
);
3125 * Unbind all shader storage buffers in the range
3126 * <first> through <first>+<count>-1
3129 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3132 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3135 for (i
= 0; i
< count
; i
++)
3136 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3137 bufObj
, -1, -1, GL_TRUE
);
3141 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3142 const GLuint
*buffers
,
3144 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3149 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3152 /* Assume that at least one binding will be changed */
3153 FLUSH_VERTICES(ctx
, 0);
3154 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3157 /* The ARB_multi_bind spec says:
3159 * "If <buffers> is NULL, all bindings from <first> through
3160 * <first>+<count>-1 are reset to their unbound (zero) state.
3161 * In this case, the offsets and sizes associated with the
3162 * binding points are set to default values, ignoring
3163 * <offsets> and <sizes>."
3165 unbind_uniform_buffers(ctx
, first
, count
);
3169 /* Note that the error semantics for multi-bind commands differ from
3170 * those of other GL commands.
3172 * The Issues section in the ARB_multi_bind spec says:
3174 * "(11) Typically, OpenGL specifies that if an error is generated by a
3175 * command, that command has no effect. This is somewhat
3176 * unfortunate for multi-bind commands, because it would require a
3177 * first pass to scan the entire list of bound objects for errors
3178 * and then a second pass to actually perform the bindings.
3179 * Should we have different error semantics?
3181 * RESOLVED: Yes. In this specification, when the parameters for
3182 * one of the <count> binding points are invalid, that binding point
3183 * is not updated and an error will be generated. However, other
3184 * binding points in the same command will be updated if their
3185 * parameters are valid and no other error occurs."
3188 _mesa_begin_bufferobj_lookups(ctx
);
3190 for (i
= 0; i
< count
; i
++) {
3191 struct gl_uniform_buffer_binding
*binding
=
3192 &ctx
->UniformBufferBindings
[first
+ i
];
3193 struct gl_buffer_object
*bufObj
;
3194 GLintptr offset
= 0;
3195 GLsizeiptr size
= 0;
3198 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3201 /* The ARB_multi_bind spec says:
3203 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3204 * pair of values in <offsets> and <sizes> does not respectively
3205 * satisfy the constraints described for those parameters for the
3206 * specified target, as described in section 6.7.1 (per binding)."
3208 * Section 6.7.1 refers to table 6.5, which says:
3210 * "┌───────────────────────────────────────────────────────────────┐
3211 * │ Uniform buffer array bindings (see sec. 7.6) │
3212 * ├─────────────────────┬─────────────────────────────────────────┤
3214 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3215 * │ │ OFFSET_ALIGNMENT │
3217 * │ size restriction │ none │
3218 * └─────────────────────┴─────────────────────────────────────────┘"
3220 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3221 _mesa_error(ctx
, GL_INVALID_VALUE
,
3222 "glBindBuffersRange(offsets[%u]=%" PRId64
3223 " is misaligned; it must be a multiple of the value of "
3224 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3225 "target=GL_UNIFORM_BUFFER)",
3226 i
, (int64_t) offsets
[i
],
3227 ctx
->Const
.UniformBufferOffsetAlignment
);
3231 offset
= offsets
[i
];
3235 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3236 bufObj
= binding
->BufferObject
;
3238 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3241 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3242 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3244 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3248 _mesa_end_bufferobj_lookups(ctx
);
3252 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3253 GLsizei count
, const GLuint
*buffers
,
3255 const GLintptr
*offsets
,
3256 const GLsizeiptr
*sizes
,
3261 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3264 /* Assume that at least one binding will be changed */
3265 FLUSH_VERTICES(ctx
, 0);
3266 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3269 /* The ARB_multi_bind spec says:
3271 * "If <buffers> is NULL, all bindings from <first> through
3272 * <first>+<count>-1 are reset to their unbound (zero) state.
3273 * In this case, the offsets and sizes associated with the
3274 * binding points are set to default values, ignoring
3275 * <offsets> and <sizes>."
3277 unbind_shader_storage_buffers(ctx
, first
, count
);
3281 /* Note that the error semantics for multi-bind commands differ from
3282 * those of other GL commands.
3284 * The Issues section in the ARB_multi_bind spec says:
3286 * "(11) Typically, OpenGL specifies that if an error is generated by a
3287 * command, that command has no effect. This is somewhat
3288 * unfortunate for multi-bind commands, because it would require a
3289 * first pass to scan the entire list of bound objects for errors
3290 * and then a second pass to actually perform the bindings.
3291 * Should we have different error semantics?
3293 * RESOLVED: Yes. In this specification, when the parameters for
3294 * one of the <count> binding points are invalid, that binding point
3295 * is not updated and an error will be generated. However, other
3296 * binding points in the same command will be updated if their
3297 * parameters are valid and no other error occurs."
3300 _mesa_begin_bufferobj_lookups(ctx
);
3302 for (i
= 0; i
< count
; i
++) {
3303 struct gl_shader_storage_buffer_binding
*binding
=
3304 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3305 struct gl_buffer_object
*bufObj
;
3306 GLintptr offset
= 0;
3307 GLsizeiptr size
= 0;
3310 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3313 /* The ARB_multi_bind spec says:
3315 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3316 * pair of values in <offsets> and <sizes> does not respectively
3317 * satisfy the constraints described for those parameters for the
3318 * specified target, as described in section 6.7.1 (per binding)."
3320 * Section 6.7.1 refers to table 6.5, which says:
3322 * "┌───────────────────────────────────────────────────────────────┐
3323 * │ Shader storage buffer array bindings (see sec. 7.8) │
3324 * ├─────────────────────┬─────────────────────────────────────────┤
3326 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3327 * │ │ BUFFER_OFFSET_ALIGNMENT │
3329 * │ size restriction │ none │
3330 * └─────────────────────┴─────────────────────────────────────────┘"
3332 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3333 _mesa_error(ctx
, GL_INVALID_VALUE
,
3334 "glBindBuffersRange(offsets[%u]=%" PRId64
3335 " is misaligned; it must be a multiple of the value of "
3336 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3337 "target=GL_SHADER_STORAGE_BUFFER)",
3338 i
, (int64_t) offsets
[i
],
3339 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3343 offset
= offsets
[i
];
3347 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3348 bufObj
= binding
->BufferObject
;
3350 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3353 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3354 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3356 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3360 _mesa_end_bufferobj_lookups(ctx
);
3364 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3365 struct gl_transform_feedback_object
*tfObj
,
3366 GLuint first
, GLsizei count
, const char *caller
)
3368 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3369 _mesa_error(ctx
, GL_INVALID_ENUM
,
3370 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3374 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3376 * "An INVALID_OPERATION error is generated :
3379 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3380 * FEEDBACK_BUFFER and transform feedback is currently active."
3382 * We assume that this is also meant to apply to BindBuffersRange
3383 * and BindBuffersBase.
3385 if (tfObj
->Active
) {
3386 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3387 "%s(Changing transform feedback buffers while "
3388 "transform feedback is active)", caller
);
3392 /* The ARB_multi_bind_spec says:
3394 * "An INVALID_OPERATION error is generated if <first> + <count> is
3395 * greater than the number of target-specific indexed binding points,
3396 * as described in section 6.7.1."
3398 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3400 "%s(first=%u + count=%d > the value of "
3401 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3402 caller
, first
, count
,
3403 ctx
->Const
.MaxTransformFeedbackBuffers
);
3411 * Unbind all transform feedback buffers in the range
3412 * <first> through <first>+<count>-1
3415 unbind_xfb_buffers(struct gl_context
*ctx
,
3416 struct gl_transform_feedback_object
*tfObj
,
3417 GLuint first
, GLsizei count
)
3419 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3422 for (i
= 0; i
< count
; i
++)
3423 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3428 bind_xfb_buffers(struct gl_context
*ctx
,
3429 GLuint first
, GLsizei count
,
3430 const GLuint
*buffers
,
3432 const GLintptr
*offsets
,
3433 const GLsizeiptr
*sizes
,
3436 struct gl_transform_feedback_object
*tfObj
=
3437 ctx
->TransformFeedback
.CurrentObject
;
3440 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
3443 /* Assume that at least one binding will be changed */
3444 FLUSH_VERTICES(ctx
, 0);
3445 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3448 /* The ARB_multi_bind spec says:
3450 * "If <buffers> is NULL, all bindings from <first> through
3451 * <first>+<count>-1 are reset to their unbound (zero) state.
3452 * In this case, the offsets and sizes associated with the
3453 * binding points are set to default values, ignoring
3454 * <offsets> and <sizes>."
3456 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3460 /* Note that the error semantics for multi-bind commands differ from
3461 * those of other GL commands.
3463 * The Issues section in the ARB_multi_bind spec says:
3465 * "(11) Typically, OpenGL specifies that if an error is generated by a
3466 * command, that command has no effect. This is somewhat
3467 * unfortunate for multi-bind commands, because it would require a
3468 * first pass to scan the entire list of bound objects for errors
3469 * and then a second pass to actually perform the bindings.
3470 * Should we have different error semantics?
3472 * RESOLVED: Yes. In this specification, when the parameters for
3473 * one of the <count> binding points are invalid, that binding point
3474 * is not updated and an error will be generated. However, other
3475 * binding points in the same command will be updated if their
3476 * parameters are valid and no other error occurs."
3479 _mesa_begin_bufferobj_lookups(ctx
);
3481 for (i
= 0; i
< count
; i
++) {
3482 const GLuint index
= first
+ i
;
3483 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3484 struct gl_buffer_object
*bufObj
;
3485 GLintptr offset
= 0;
3486 GLsizeiptr size
= 0;
3489 offset
= offsets
[i
];
3492 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3495 /* The ARB_multi_bind spec says:
3497 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3498 * pair of values in <offsets> and <sizes> does not respectively
3499 * satisfy the constraints described for those parameters for the
3500 * specified target, as described in section 6.7.1 (per binding)."
3502 * Section 6.7.1 refers to table 6.5, which says:
3504 * "┌───────────────────────────────────────────────────────────────┐
3505 * │ Transform feedback array bindings (see sec. 13.2.2) │
3506 * ├───────────────────────┬───────────────────────────────────────┤
3508 * │ offset restriction │ multiple of 4 │
3510 * │ size restriction │ multiple of 4 │
3511 * └───────────────────────┴───────────────────────────────────────┘"
3513 if (offsets
[i
] & 0x3) {
3514 _mesa_error(ctx
, GL_INVALID_VALUE
,
3515 "glBindBuffersRange(offsets[%u]=%" PRId64
3516 " is misaligned; it must be a multiple of 4 when "
3517 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3518 i
, (int64_t) offsets
[i
]);
3522 if (sizes
[i
] & 0x3) {
3523 _mesa_error(ctx
, GL_INVALID_VALUE
,
3524 "glBindBuffersRange(sizes[%u]=%" PRId64
3525 " is misaligned; it must be a multiple of 4 when "
3526 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3527 i
, (int64_t) sizes
[i
]);
3531 offset
= offsets
[i
];
3535 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3536 bufObj
= boundBufObj
;
3538 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3541 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3545 _mesa_end_bufferobj_lookups(ctx
);
3549 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3550 GLuint first
, GLsizei count
,
3553 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3554 _mesa_error(ctx
, GL_INVALID_ENUM
,
3555 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3559 /* The ARB_multi_bind_spec says:
3561 * "An INVALID_OPERATION error is generated if <first> + <count> is
3562 * greater than the number of target-specific indexed binding points,
3563 * as described in section 6.7.1."
3565 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3566 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3567 "%s(first=%u + count=%d > the value of "
3568 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3569 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3577 * Unbind all atomic counter buffers in the range
3578 * <first> through <first>+<count>-1
3581 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3583 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3586 for (i
= 0; i
< count
; i
++)
3587 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3592 bind_atomic_buffers(struct gl_context
*ctx
,
3595 const GLuint
*buffers
,
3597 const GLintptr
*offsets
,
3598 const GLsizeiptr
*sizes
,
3603 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
3606 /* Assume that at least one binding will be changed */
3607 FLUSH_VERTICES(ctx
, 0);
3608 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3611 /* The ARB_multi_bind spec says:
3613 * "If <buffers> is NULL, all bindings from <first> through
3614 * <first>+<count>-1 are reset to their unbound (zero) state.
3615 * In this case, the offsets and sizes associated with the
3616 * binding points are set to default values, ignoring
3617 * <offsets> and <sizes>."
3619 unbind_atomic_buffers(ctx
, first
, count
);
3623 /* Note that the error semantics for multi-bind commands differ from
3624 * those of other GL commands.
3626 * The Issues section in the ARB_multi_bind spec says:
3628 * "(11) Typically, OpenGL specifies that if an error is generated by a
3629 * command, that command has no effect. This is somewhat
3630 * unfortunate for multi-bind commands, because it would require a
3631 * first pass to scan the entire list of bound objects for errors
3632 * and then a second pass to actually perform the bindings.
3633 * Should we have different error semantics?
3635 * RESOLVED: Yes. In this specification, when the parameters for
3636 * one of the <count> binding points are invalid, that binding point
3637 * is not updated and an error will be generated. However, other
3638 * binding points in the same command will be updated if their
3639 * parameters are valid and no other error occurs."
3642 _mesa_begin_bufferobj_lookups(ctx
);
3644 for (i
= 0; i
< count
; i
++) {
3645 struct gl_atomic_buffer_binding
*binding
=
3646 &ctx
->AtomicBufferBindings
[first
+ i
];
3647 struct gl_buffer_object
*bufObj
;
3648 GLintptr offset
= 0;
3649 GLsizeiptr size
= 0;
3652 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3655 /* The ARB_multi_bind spec says:
3657 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3658 * pair of values in <offsets> and <sizes> does not respectively
3659 * satisfy the constraints described for those parameters for the
3660 * specified target, as described in section 6.7.1 (per binding)."
3662 * Section 6.7.1 refers to table 6.5, which says:
3664 * "┌───────────────────────────────────────────────────────────────┐
3665 * │ Atomic counter array bindings (see sec. 7.7.2) │
3666 * ├───────────────────────┬───────────────────────────────────────┤
3668 * │ offset restriction │ multiple of 4 │
3670 * │ size restriction │ none │
3671 * └───────────────────────┴───────────────────────────────────────┘"
3673 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3674 _mesa_error(ctx
, GL_INVALID_VALUE
,
3675 "glBindBuffersRange(offsets[%u]=%" PRId64
3676 " is misaligned; it must be a multiple of %d when "
3677 "target=GL_ATOMIC_COUNTER_BUFFER)",
3678 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3682 offset
= offsets
[i
];
3686 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3687 bufObj
= binding
->BufferObject
;
3689 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3692 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3695 _mesa_end_bufferobj_lookups(ctx
);
3699 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3700 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3702 GET_CURRENT_CONTEXT(ctx
);
3703 struct gl_buffer_object
*bufObj
;
3705 if (MESA_VERBOSE
& VERBOSE_API
) {
3706 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %ld, %ld)\n",
3707 _mesa_enum_to_string(target
), index
, buffer
, offset
, size
);
3711 bufObj
= ctx
->Shared
->NullBufferObj
;
3713 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3715 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3716 &bufObj
, "glBindBufferRange"))
3720 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3721 "glBindBufferRange(invalid buffer=%u)", buffer
);
3727 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3734 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3735 _mesa_bind_buffer_range_transform_feedback(ctx
,
3736 ctx
->TransformFeedback
.CurrentObject
,
3737 index
, bufObj
, offset
, size
,
3740 case GL_UNIFORM_BUFFER
:
3741 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3743 case GL_SHADER_STORAGE_BUFFER
:
3744 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3746 case GL_ATOMIC_COUNTER_BUFFER
:
3747 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3748 "glBindBufferRange");
3751 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3757 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3759 GET_CURRENT_CONTEXT(ctx
);
3760 struct gl_buffer_object
*bufObj
;
3762 if (MESA_VERBOSE
& VERBOSE_API
) {
3763 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
3764 _mesa_enum_to_string(target
), index
, buffer
);
3768 bufObj
= ctx
->Shared
->NullBufferObj
;
3770 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3772 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3773 &bufObj
, "glBindBufferBase"))
3777 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3778 "glBindBufferBase(invalid buffer=%u)", buffer
);
3782 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3783 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3785 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3786 * zero and size equal to the size of buffer."
3788 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3790 * "If the parameter (starting offset or size) was not specified when the
3791 * buffer object was bound, zero is returned."
3793 * What happens if the size of the buffer changes? Does the size of the
3794 * buffer at the moment glBindBufferBase was called still play a role, like
3795 * the first quote would imply, or is the size meaningless in the
3796 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3797 * core spec page 45 says:
3799 * "It is equivalent to calling BindBufferRange with offset zero, while
3800 * size is determined by the size of the bound buffer at the time the
3803 * My interpretation is that the GL 4.1 spec was a clarification of the
3804 * behavior, not a change. In particular, this choice will only make
3805 * rendering work in cases where it would have had undefined results.
3809 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3810 _mesa_bind_buffer_base_transform_feedback(ctx
,
3811 ctx
->TransformFeedback
.CurrentObject
,
3812 index
, bufObj
, false);
3814 case GL_UNIFORM_BUFFER
:
3815 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
3817 case GL_SHADER_STORAGE_BUFFER
:
3818 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
3820 case GL_ATOMIC_COUNTER_BUFFER
:
3821 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
3822 "glBindBufferBase");
3825 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
3831 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
3832 const GLuint
*buffers
,
3833 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3835 GET_CURRENT_CONTEXT(ctx
);
3837 if (MESA_VERBOSE
& VERBOSE_API
) {
3838 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
3839 _mesa_enum_to_string(target
), first
, count
,
3840 buffers
, offsets
, sizes
);
3844 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3845 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3846 "glBindBuffersRange");
3848 case GL_UNIFORM_BUFFER
:
3849 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3850 "glBindBuffersRange");
3852 case GL_SHADER_STORAGE_BUFFER
:
3853 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3854 "glBindBuffersRange");
3856 case GL_ATOMIC_COUNTER_BUFFER
:
3857 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3858 "glBindBuffersRange");
3861 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
3862 _mesa_enum_to_string(target
));
3868 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
3869 const GLuint
*buffers
)
3871 GET_CURRENT_CONTEXT(ctx
);
3873 if (MESA_VERBOSE
& VERBOSE_API
) {
3874 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
3875 _mesa_enum_to_string(target
), first
, count
, buffers
);
3879 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3880 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3881 "glBindBuffersBase");
3883 case GL_UNIFORM_BUFFER
:
3884 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3885 "glBindBuffersBase");
3887 case GL_SHADER_STORAGE_BUFFER
:
3888 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3889 "glBindBuffersBase");
3891 case GL_ATOMIC_COUNTER_BUFFER
:
3892 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3893 "glBindBuffersBase");
3896 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
3897 _mesa_enum_to_string(target
));
3903 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
3906 GET_CURRENT_CONTEXT(ctx
);
3907 struct gl_buffer_object
*bufObj
;
3908 const GLintptr end
= offset
+ length
;
3910 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
3911 * Profile) spec says:
3913 * "An INVALID_VALUE error is generated if buffer is zero or is not the
3914 * name of an existing buffer object."
3916 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3917 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
3918 _mesa_error(ctx
, GL_INVALID_VALUE
,
3919 "glInvalidateBufferSubData(name = 0x%x) invalid object",
3924 /* The GL_ARB_invalidate_subdata spec says:
3926 * "An INVALID_VALUE error is generated if <offset> or <length> is
3927 * negative, or if <offset> + <length> is greater than the value of
3930 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
3931 _mesa_error(ctx
, GL_INVALID_VALUE
,
3932 "glInvalidateBufferSubData(invalid offset or length)");
3936 /* The OpenGL 4.4 (Core Profile) spec says:
3938 * "An INVALID_OPERATION error is generated if buffer is currently
3939 * mapped by MapBuffer or if the invalidate range intersects the range
3940 * currently mapped by MapBufferRange, unless it was mapped
3941 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
3943 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
3944 bufferobj_range_mapped(bufObj
, offset
, length
)) {
3945 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3946 "glInvalidateBufferSubData(intersection with mapped "
3951 if (ctx
->Driver
.InvalidateBufferSubData
)
3952 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
3956 _mesa_InvalidateBufferData(GLuint buffer
)
3958 GET_CURRENT_CONTEXT(ctx
);
3959 struct gl_buffer_object
*bufObj
;
3961 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
3962 * Profile) spec says:
3964 * "An INVALID_VALUE error is generated if buffer is zero or is not the
3965 * name of an existing buffer object."
3967 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3968 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
3969 _mesa_error(ctx
, GL_INVALID_VALUE
,
3970 "glInvalidateBufferData(name = 0x%x) invalid object",
3975 /* The OpenGL 4.4 (Core Profile) spec says:
3977 * "An INVALID_OPERATION error is generated if buffer is currently
3978 * mapped by MapBuffer or if the invalidate range intersects the range
3979 * currently mapped by MapBufferRange, unless it was mapped
3980 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
3982 if (_mesa_check_disallowed_mapping(bufObj
)) {
3983 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3984 "glInvalidateBufferData(intersection with mapped "
3989 if (ctx
->Driver
.InvalidateBufferSubData
)
3990 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, 0, bufObj
->Size
);