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 * Used as a placeholder for buffer objects between glGenBuffers() and
55 * glBindBuffer() so that glIsBuffer() can work correctly.
57 static struct gl_buffer_object DummyBufferObject
;
61 * Return pointer to address of a buffer object target.
62 * \param ctx the GL context
63 * \param target the buffer object target to be retrieved.
64 * \return pointer to pointer to the buffer object bound to \c target in the
65 * specified context or \c NULL if \c target is invalid.
67 static inline struct gl_buffer_object
**
68 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
70 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
72 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
73 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
77 case GL_ARRAY_BUFFER_ARB
:
78 return &ctx
->Array
.ArrayBufferObj
;
79 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
80 return &ctx
->Array
.VAO
->IndexBufferObj
;
81 case GL_PIXEL_PACK_BUFFER_EXT
:
82 return &ctx
->Pack
.BufferObj
;
83 case GL_PIXEL_UNPACK_BUFFER_EXT
:
84 return &ctx
->Unpack
.BufferObj
;
85 case GL_COPY_READ_BUFFER
:
86 return &ctx
->CopyReadBuffer
;
87 case GL_COPY_WRITE_BUFFER
:
88 return &ctx
->CopyWriteBuffer
;
89 case GL_DRAW_INDIRECT_BUFFER
:
90 if ((ctx
->API
== API_OPENGL_CORE
&&
91 ctx
->Extensions
.ARB_draw_indirect
) ||
92 _mesa_is_gles31(ctx
)) {
93 return &ctx
->DrawIndirectBuffer
;
96 case GL_TRANSFORM_FEEDBACK_BUFFER
:
97 if (ctx
->Extensions
.EXT_transform_feedback
) {
98 return &ctx
->TransformFeedback
.CurrentBuffer
;
101 case GL_TEXTURE_BUFFER
:
102 if (ctx
->API
== API_OPENGL_CORE
&&
103 ctx
->Extensions
.ARB_texture_buffer_object
) {
104 return &ctx
->Texture
.BufferObject
;
107 case GL_UNIFORM_BUFFER
:
108 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
109 return &ctx
->UniformBuffer
;
112 case GL_SHADER_STORAGE_BUFFER
:
113 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
114 return &ctx
->ShaderStorageBuffer
;
117 case GL_ATOMIC_COUNTER_BUFFER
:
118 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
119 return &ctx
->AtomicBuffer
;
122 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
123 if (ctx
->Extensions
.AMD_pinned_memory
) {
124 return &ctx
->ExternalVirtualMemoryBuffer
;
135 * Get the buffer object bound to the specified target in a GL context.
136 * \param ctx the GL context
137 * \param target the buffer object target to be retrieved.
138 * \param error the GL error to record if target is illegal.
139 * \return pointer to the buffer object bound to \c target in the
140 * specified context or \c NULL if \c target is invalid.
142 static inline struct gl_buffer_object
*
143 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
146 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
149 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
153 if (!_mesa_is_bufferobj(*bufObj
)) {
154 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
163 * Convert a GLbitfield describing the mapped buffer access flags
164 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
167 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
169 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
170 if ((access
& rwFlags
) == rwFlags
)
171 return GL_READ_WRITE
;
172 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
174 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
175 return GL_WRITE_ONLY
;
177 /* Otherwise, AccessFlags is zero (the default state).
179 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
181 * Name Type Initial Value Legal Values
183 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
186 * However, table 6.8 in the GL_OES_mapbuffer extension says:
188 * Get Value Type Get Command Value Description
189 * --------- ---- ----------- ----- -----------
190 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
192 * The difference is because GL_OES_mapbuffer only supports mapping buffers
197 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
202 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
204 * The regions do not overlap if and only if the end of the given
205 * region is before the mapped region or the start of the given region
206 * is after the mapped region.
208 * \param obj Buffer object target on which to operate.
209 * \param offset Offset of the first byte of the subdata range.
210 * \param size Size, in bytes, of the subdata range.
211 * \return true if ranges overlap, false otherwise
215 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
216 GLintptr offset
, GLsizeiptr size
)
218 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
219 const GLintptr end
= offset
+ size
;
220 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
221 obj
->Mappings
[MAP_USER
].Length
;
223 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
232 * Tests the subdata range parameters and sets the GL error code for
233 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
234 * \c glClearBufferSubData.
236 * \param ctx GL context.
237 * \param bufObj The buffer object.
238 * \param offset Offset of the first byte of the subdata range.
239 * \param size Size, in bytes, of the subdata range.
240 * \param mappedRange If true, checks if an overlapping range is mapped.
241 * If false, checks if buffer is mapped.
242 * \param caller Name of calling function for recording errors.
243 * \return false if error, true otherwise
245 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
248 buffer_object_subdata_range_good(struct gl_context
*ctx
,
249 struct gl_buffer_object
*bufObj
,
250 GLintptr offset
, GLsizeiptr size
,
251 bool mappedRange
, const char *caller
)
254 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
259 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
263 if (offset
+ size
> bufObj
->Size
) {
264 _mesa_error(ctx
, GL_INVALID_VALUE
,
265 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
266 (unsigned long) offset
,
267 (unsigned long) size
,
268 (unsigned long) bufObj
->Size
);
272 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
276 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
277 _mesa_error(ctx
, GL_INVALID_OPERATION
,
278 "%s(range is mapped without persistent bit)",
284 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
285 _mesa_error(ctx
, GL_INVALID_OPERATION
,
286 "%s(buffer is mapped without persistent bit)",
297 * Test the format and type parameters and set the GL error code for
298 * \c glClearBufferData and \c glClearBufferSubData.
300 * \param ctx GL context.
301 * \param internalformat Format to which the data is to be converted.
302 * \param format Format of the supplied data.
303 * \param type Type of the supplied data.
304 * \param caller Name of calling function for recording errors.
305 * \return If internalformat, format and type are legal the mesa_format
306 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
308 * \sa glClearBufferData and glClearBufferSubData
311 validate_clear_buffer_format(struct gl_context
*ctx
,
312 GLenum internalformat
,
313 GLenum format
, GLenum type
,
316 mesa_format mesaFormat
;
317 GLenum errorFormatType
;
319 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
320 if (mesaFormat
== MESA_FORMAT_NONE
) {
321 _mesa_error(ctx
, GL_INVALID_ENUM
,
322 "%s(invalid internalformat)", caller
);
323 return MESA_FORMAT_NONE
;
326 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
327 * EXT_texture_integer there is no conversion between integer and
328 * non-integer formats
330 if (_mesa_is_enum_format_signed_int(format
) !=
331 _mesa_is_format_integer_color(mesaFormat
)) {
332 _mesa_error(ctx
, GL_INVALID_OPERATION
,
333 "%s(integer vs non-integer)", caller
);
334 return MESA_FORMAT_NONE
;
337 if (!_mesa_is_color_format(format
)) {
338 _mesa_error(ctx
, GL_INVALID_ENUM
,
339 "%s(format is not a color format)", caller
);
340 return MESA_FORMAT_NONE
;
343 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
344 if (errorFormatType
!= GL_NO_ERROR
) {
345 _mesa_error(ctx
, GL_INVALID_ENUM
,
346 "%s(invalid format or type)", caller
);
347 return MESA_FORMAT_NONE
;
355 * Convert user-specified clear value to the specified internal format.
357 * \param ctx GL context.
358 * \param internalformat Format to which the data is converted.
359 * \param clearValue Points to the converted clear value.
360 * \param format Format of the supplied data.
361 * \param type Type of the supplied data.
362 * \param data Data which is to be converted to internalformat.
363 * \param caller Name of calling function for recording errors.
364 * \return true if data could be converted, false otherwise.
366 * \sa glClearBufferData, glClearBufferSubData
369 convert_clear_buffer_data(struct gl_context
*ctx
,
370 mesa_format internalformat
,
371 GLubyte
*clearValue
, GLenum format
, GLenum type
,
372 const GLvoid
*data
, const char *caller
)
374 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
376 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
377 0, &clearValue
, 1, 1, 1,
378 format
, type
, data
, &ctx
->Unpack
)) {
382 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
389 * Allocate and initialize a new buffer object.
391 * Default callback for the \c dd_function_table::NewBufferObject() hook.
393 static struct gl_buffer_object
*
394 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
396 struct gl_buffer_object
*obj
;
400 obj
= MALLOC_STRUCT(gl_buffer_object
);
401 _mesa_initialize_buffer_object(ctx
, obj
, name
);
407 * Delete a buffer object.
409 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
412 _mesa_delete_buffer_object(struct gl_context
*ctx
,
413 struct gl_buffer_object
*bufObj
)
417 _mesa_align_free(bufObj
->Data
);
419 /* assign strange values here to help w/ debugging */
420 bufObj
->RefCount
= -1000;
423 mtx_destroy(&bufObj
->Mutex
);
431 * Set ptr to bufObj w/ reference counting.
432 * This is normally only called from the _mesa_reference_buffer_object() macro
433 * when there's a real pointer change.
436 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
437 struct gl_buffer_object
**ptr
,
438 struct gl_buffer_object
*bufObj
)
441 /* Unreference the old buffer */
442 GLboolean deleteFlag
= GL_FALSE
;
443 struct gl_buffer_object
*oldObj
= *ptr
;
445 mtx_lock(&oldObj
->Mutex
);
446 assert(oldObj
->RefCount
> 0);
449 printf("BufferObj %p %d DECR to %d\n",
450 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
452 deleteFlag
= (oldObj
->RefCount
== 0);
453 mtx_unlock(&oldObj
->Mutex
);
457 /* some sanity checking: don't delete a buffer still in use */
459 /* unfortunately, these tests are invalid during context tear-down */
460 assert(ctx
->Array
.ArrayBufferObj
!= bufObj
);
461 assert(ctx
->Array
.VAO
->IndexBufferObj
!= bufObj
);
462 assert(ctx
->Array
.VAO
->Vertex
.BufferObj
!= bufObj
);
465 assert(ctx
->Driver
.DeleteBuffer
);
466 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
474 /* reference new buffer */
475 mtx_lock(&bufObj
->Mutex
);
476 if (bufObj
->RefCount
== 0) {
477 /* this buffer's being deleted (look just above) */
478 /* Not sure this can every really happen. Warn if it does. */
479 _mesa_problem(NULL
, "referencing deleted buffer object");
485 printf("BufferObj %p %d INCR to %d\n",
486 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
490 mtx_unlock(&bufObj
->Mutex
);
496 * Initialize a buffer object to default values.
499 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
500 struct gl_buffer_object
*obj
,
503 memset(obj
, 0, sizeof(struct gl_buffer_object
));
504 mtx_init(&obj
->Mutex
, mtx_plain
);
507 obj
->Usage
= GL_STATIC_DRAW_ARB
;
513 * Callback called from _mesa_HashWalk()
516 count_buffer_size(GLuint key
, void *data
, void *userData
)
518 const struct gl_buffer_object
*bufObj
=
519 (const struct gl_buffer_object
*) data
;
520 GLuint
*total
= (GLuint
*) userData
;
523 *total
= *total
+ bufObj
->Size
;
528 * Compute total size (in bytes) of all buffer objects for the given context.
529 * For debugging purposes.
532 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
536 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
543 * Allocate space for and store data in a buffer object. Any data that was
544 * previously stored in the buffer object is lost. If \c data is \c NULL,
545 * memory will be allocated, but no copy will occur.
547 * This is the default callback for \c dd_function_table::BufferData()
548 * Note that all GL error checking will have been done already.
550 * \param ctx GL context.
551 * \param target Buffer object target on which to operate.
552 * \param size Size, in bytes, of the new data store.
553 * \param data Pointer to the data to store in the buffer object. This
554 * pointer may be \c NULL.
555 * \param usage Hints about how the data will be used.
556 * \param bufObj Object to be used.
558 * \return GL_TRUE for success, GL_FALSE for failure
559 * \sa glBufferDataARB, dd_function_table::BufferData.
562 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptr size
,
563 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
564 struct gl_buffer_object
*bufObj
)
570 _mesa_align_free( bufObj
->Data
);
572 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
574 bufObj
->Data
= (GLubyte
*) new_data
;
576 bufObj
->Usage
= usage
;
577 bufObj
->StorageFlags
= storageFlags
;
580 memcpy( bufObj
->Data
, data
, size
);
592 * Replace data in a subrange of buffer object. If the data range
593 * specified by \c size + \c offset extends beyond the end of the buffer or
594 * if \c data is \c NULL, no copy is performed.
596 * This is the default callback for \c dd_function_table::BufferSubData()
597 * Note that all GL error checking will have been done already.
599 * \param ctx GL context.
600 * \param offset Offset of the first byte to be modified.
601 * \param size Size, in bytes, of the data range.
602 * \param data Pointer to the data to store in the buffer object.
603 * \param bufObj Object to be used.
605 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
608 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptr offset
,
609 GLsizeiptr size
, const GLvoid
*data
,
610 struct gl_buffer_object
*bufObj
)
614 /* this should have been caught in _mesa_BufferSubData() */
615 assert(size
+ offset
<= bufObj
->Size
);
618 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
624 * Retrieve data from a subrange of buffer object. If the data range
625 * specified by \c size + \c offset extends beyond the end of the buffer or
626 * if \c data is \c NULL, no copy is performed.
628 * This is the default callback for \c dd_function_table::GetBufferSubData()
629 * Note that all GL error checking will have been done already.
631 * \param ctx GL context.
632 * \param target Buffer object target on which to operate.
633 * \param offset Offset of the first byte to be fetched.
634 * \param size Size, in bytes, of the data range.
635 * \param data Destination for data
636 * \param bufObj Object to be used.
638 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
641 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
642 GLsizeiptrARB size
, GLvoid
* data
,
643 struct gl_buffer_object
* bufObj
)
647 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
648 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
654 * Clear a subrange of the buffer object with copies of the supplied data.
655 * If data is NULL the buffer is filled with zeros.
657 * This is the default callback for \c dd_function_table::ClearBufferSubData()
658 * Note that all GL error checking will have been done already.
660 * \param ctx GL context.
661 * \param offset Offset of the first byte to be cleared.
662 * \param size Size, in bytes, of the to be cleared range.
663 * \param clearValue Source of the data.
664 * \param clearValueSize Size, in bytes, of the supplied data.
665 * \param bufObj Object to be cleared.
667 * \sa glClearBufferSubData, glClearBufferData and
668 * dd_function_table::ClearBufferSubData.
671 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
672 GLintptr offset
, GLsizeiptr size
,
673 const GLvoid
*clearValue
,
674 GLsizeiptr clearValueSize
,
675 struct gl_buffer_object
*bufObj
)
680 assert(ctx
->Driver
.MapBufferRange
);
681 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
683 GL_MAP_INVALIDATE_RANGE_BIT
,
684 bufObj
, MAP_INTERNAL
);
687 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
691 if (clearValue
== NULL
) {
692 /* Clear with zeros, per the spec */
693 memset(dest
, 0, size
);
694 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
698 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
699 memcpy(dest
, clearValue
, clearValueSize
);
700 dest
+= clearValueSize
;
703 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
708 * Default fallback for \c dd_function_table::MapBufferRange().
709 * Called via glMapBufferRange().
712 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
713 GLsizeiptr length
, GLbitfield access
,
714 struct gl_buffer_object
*bufObj
,
715 gl_map_buffer_index index
)
718 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
719 /* Just return a direct pointer to the data */
720 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
721 bufObj
->Mappings
[index
].Length
= length
;
722 bufObj
->Mappings
[index
].Offset
= offset
;
723 bufObj
->Mappings
[index
].AccessFlags
= access
;
724 return bufObj
->Mappings
[index
].Pointer
;
729 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
730 * Called via glFlushMappedBufferRange().
733 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
734 GLintptr offset
, GLsizeiptr length
,
735 struct gl_buffer_object
*obj
,
736 gl_map_buffer_index index
)
748 * Default callback for \c dd_function_table::UnmapBuffer().
750 * The input parameters will have been already tested for errors.
752 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
755 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
756 gl_map_buffer_index index
)
759 /* XXX we might assert here that bufObj->Pointer is non-null */
760 bufObj
->Mappings
[index
].Pointer
= NULL
;
761 bufObj
->Mappings
[index
].Length
= 0;
762 bufObj
->Mappings
[index
].Offset
= 0;
763 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
769 * Default fallback for \c dd_function_table::CopyBufferSubData().
770 * Called via glCopyBufferSubData().
773 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
774 struct gl_buffer_object
*src
,
775 struct gl_buffer_object
*dst
,
776 GLintptr readOffset
, GLintptr writeOffset
,
779 GLubyte
*srcPtr
, *dstPtr
;
782 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
784 GL_MAP_WRITE_BIT
, src
,
790 srcPtr
+= readOffset
;
791 dstPtr
+= writeOffset
;
793 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
794 GL_MAP_READ_BIT
, src
,
796 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
798 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
802 /* Note: the src and dst regions will never overlap. Trying to do so
803 * would generate GL_INVALID_VALUE earlier.
805 if (srcPtr
&& dstPtr
)
806 memcpy(dstPtr
, srcPtr
, size
);
808 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
810 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
816 * Initialize the state associated with buffer objects
819 _mesa_init_buffer_objects( struct gl_context
*ctx
)
823 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
824 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
825 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
827 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
828 ctx
->Shared
->NullBufferObj
);
830 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
831 ctx
->Shared
->NullBufferObj
);
832 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
833 ctx
->Shared
->NullBufferObj
);
835 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
836 ctx
->Shared
->NullBufferObj
);
838 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
839 ctx
->Shared
->NullBufferObj
);
841 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
842 ctx
->Shared
->NullBufferObj
);
844 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
845 ctx
->Shared
->NullBufferObj
);
847 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
848 _mesa_reference_buffer_object(ctx
,
849 &ctx
->UniformBufferBindings
[i
].BufferObject
,
850 ctx
->Shared
->NullBufferObj
);
851 ctx
->UniformBufferBindings
[i
].Offset
= -1;
852 ctx
->UniformBufferBindings
[i
].Size
= -1;
855 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
856 _mesa_reference_buffer_object(ctx
,
857 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
858 ctx
->Shared
->NullBufferObj
);
859 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
860 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
863 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
864 _mesa_reference_buffer_object(ctx
,
865 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
866 ctx
->Shared
->NullBufferObj
);
867 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
868 ctx
->AtomicBufferBindings
[i
].Size
= 0;
874 _mesa_free_buffer_objects( struct gl_context
*ctx
)
878 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
880 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
881 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
883 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
885 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
887 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
889 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
891 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
892 _mesa_reference_buffer_object(ctx
,
893 &ctx
->UniformBufferBindings
[i
].BufferObject
,
897 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
898 _mesa_reference_buffer_object(ctx
,
899 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
903 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
904 _mesa_reference_buffer_object(ctx
,
905 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
912 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
914 struct gl_buffer_object
**buf_handle
,
917 struct gl_buffer_object
*buf
= *buf_handle
;
919 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
|| _mesa_is_gles31(ctx
))) {
920 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
924 if (!buf
|| buf
== &DummyBufferObject
) {
925 /* If this is a new buffer object id, or one which was generated but
926 * never used before, allocate a buffer object now.
928 assert(ctx
->Driver
.NewBufferObject
);
929 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
931 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
934 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
942 * Bind the specified target to buffer for the specified context.
943 * Called by glBindBuffer() and other functions.
946 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
948 struct gl_buffer_object
*oldBufObj
;
949 struct gl_buffer_object
*newBufObj
= NULL
;
950 struct gl_buffer_object
**bindTarget
= NULL
;
952 bindTarget
= get_buffer_target(ctx
, target
);
954 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
958 /* Get pointer to old buffer object (to be unbound) */
959 oldBufObj
= *bindTarget
;
960 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
961 return; /* rebinding the same buffer object- no change */
964 * Get pointer to new buffer object (newBufObj)
967 /* The spec says there's not a buffer object named 0, but we use
968 * one internally because it simplifies things.
970 newBufObj
= ctx
->Shared
->NullBufferObj
;
973 /* non-default buffer object */
974 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
975 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
976 &newBufObj
, "glBindBuffer"))
980 /* bind new buffer */
981 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
986 * Update the default buffer objects in the given context to reference those
987 * specified in the shared state and release those referencing the old
991 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
993 /* Bind the NullBufferObj to remove references to those
994 * in the shared context hash table.
996 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
997 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
998 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
999 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1005 * Return the gl_buffer_object for the given ID.
1006 * Always return NULL for ID 0.
1008 struct gl_buffer_object
*
1009 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1014 return (struct gl_buffer_object
*)
1015 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1019 struct gl_buffer_object
*
1020 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1022 return (struct gl_buffer_object
*)
1023 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1027 * A convenience function for direct state access functions that throws
1028 * GL_INVALID_OPERATION if buffer is not the name of an existing
1031 struct gl_buffer_object
*
1032 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1035 struct gl_buffer_object
*bufObj
;
1037 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1038 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1039 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1040 "%s(non-existent buffer object %u)", caller
, buffer
);
1049 _mesa_begin_bufferobj_lookups(struct gl_context
*ctx
)
1051 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1056 _mesa_end_bufferobj_lookups(struct gl_context
*ctx
)
1058 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1063 * Look up a buffer object for a multi-bind function.
1065 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1066 * of generating an error if the buffer ID is not zero or the name
1067 * of an existing buffer object.
1069 * If the buffer ID refers to an existing buffer object, a pointer
1070 * to the buffer object is returned. If the ID is zero, a pointer
1071 * to the shared NullBufferObj is returned. If the ID is not zero
1072 * and does not refer to a valid buffer object, this function
1075 * This function assumes that the caller has already locked the
1076 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1078 struct gl_buffer_object
*
1079 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1080 const GLuint
*buffers
,
1081 GLuint index
, const char *caller
)
1083 struct gl_buffer_object
*bufObj
;
1085 if (buffers
[index
] != 0) {
1086 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1088 /* The multi-bind functions don't create the buffer objects
1089 when they don't exist. */
1090 if (bufObj
== &DummyBufferObject
)
1093 bufObj
= ctx
->Shared
->NullBufferObj
;
1096 /* The ARB_multi_bind spec says:
1098 * "An INVALID_OPERATION error is generated if any value
1099 * in <buffers> is not zero or the name of an existing
1100 * buffer object (per binding)."
1102 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1103 "%s(buffers[%u]=%u is not zero or the name "
1104 "of an existing buffer object)",
1105 caller
, index
, buffers
[index
]);
1113 * If *ptr points to obj, set ptr = the Null/default buffer object.
1114 * This is a helper for buffer object deletion.
1115 * The GL spec says that deleting a buffer object causes it to get
1116 * unbound from all arrays in the current context.
1119 unbind(struct gl_context
*ctx
,
1120 struct gl_buffer_object
**ptr
,
1121 struct gl_buffer_object
*obj
)
1124 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
1130 * Plug default/fallback buffer object functions into the device
1134 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1136 /* GL_ARB_vertex/pixel_buffer_object */
1137 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1138 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1139 driver
->BufferData
= buffer_data_fallback
;
1140 driver
->BufferSubData
= buffer_sub_data_fallback
;
1141 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1142 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1144 /* GL_ARB_clear_buffer_object */
1145 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1147 /* GL_ARB_map_buffer_range */
1148 driver
->MapBufferRange
= map_buffer_range_fallback
;
1149 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1151 /* GL_ARB_copy_buffer */
1152 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1157 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1158 struct gl_buffer_object
*bufObj
)
1162 for (i
= 0; i
< MAP_COUNT
; i
++) {
1163 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1164 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1165 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1166 bufObj
->Mappings
[i
].AccessFlags
= 0;
1172 /**********************************************************************/
1174 /**********************************************************************/
1177 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1179 GET_CURRENT_CONTEXT(ctx
);
1181 if (MESA_VERBOSE
& VERBOSE_API
)
1182 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1183 _mesa_enum_to_string(target
), buffer
);
1185 bind_buffer_object(ctx
, target
, buffer
);
1190 * Delete a set of buffer objects.
1192 * \param n Number of buffer objects to delete.
1193 * \param ids Array of \c n buffer object IDs.
1196 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1198 GET_CURRENT_CONTEXT(ctx
);
1200 FLUSH_VERTICES(ctx
, 0);
1203 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1207 mtx_lock(&ctx
->Shared
->Mutex
);
1209 for (i
= 0; i
< n
; i
++) {
1210 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
1212 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1215 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1217 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1219 /* unbind any vertex pointers bound to this buffer */
1220 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1221 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1224 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1225 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1227 if (vao
->IndexBufferObj
== bufObj
) {
1228 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1231 /* unbind ARB_draw_indirect binding point */
1232 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1233 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1236 /* unbind ARB_copy_buffer binding points */
1237 if (ctx
->CopyReadBuffer
== bufObj
) {
1238 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1240 if (ctx
->CopyWriteBuffer
== bufObj
) {
1241 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1244 /* unbind transform feedback binding points */
1245 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1246 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1248 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1249 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1250 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1254 /* unbind UBO binding points */
1255 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1256 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1257 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1261 if (ctx
->UniformBuffer
== bufObj
) {
1262 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1265 /* unbind SSBO binding points */
1266 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1267 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1268 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1272 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1273 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER
, 0);
1276 /* unbind Atomci Buffer binding points */
1277 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1278 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1279 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1283 if (ctx
->AtomicBuffer
== bufObj
) {
1284 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1287 /* unbind any pixel pack/unpack pointers bound to this buffer */
1288 if (ctx
->Pack
.BufferObj
== bufObj
) {
1289 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1291 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1292 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1295 if (ctx
->Texture
.BufferObject
== bufObj
) {
1296 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1299 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1300 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1303 /* The ID is immediately freed for re-use */
1304 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1305 /* Make sure we do not run into the classic ABA problem on bind.
1306 * We don't want to allow re-binding a buffer object that's been
1307 * "deleted" by glDeleteBuffers().
1309 * The explicit rebinding to the default object in the current context
1310 * prevents the above in the current context, but another context
1311 * sharing the same objects might suffer from this problem.
1312 * The alternative would be to do the hash lookup in any case on bind
1313 * which would introduce more runtime overhead than this.
1315 bufObj
->DeletePending
= GL_TRUE
;
1316 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1320 mtx_unlock(&ctx
->Shared
->Mutex
);
1325 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1326 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1330 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1332 GET_CURRENT_CONTEXT(ctx
);
1335 struct gl_buffer_object
*buf
;
1337 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1339 if (MESA_VERBOSE
& VERBOSE_API
)
1340 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1343 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1352 * This must be atomic (generation and allocation of buffer object IDs)
1354 mtx_lock(&ctx
->Shared
->Mutex
);
1356 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1358 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1359 * DummyBufferObject. Otherwise, create a new buffer object and insert
1362 for (i
= 0; i
< n
; i
++) {
1363 buffers
[i
] = first
+ i
;
1365 assert(ctx
->Driver
.NewBufferObject
);
1366 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1368 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1369 mtx_unlock(&ctx
->Shared
->Mutex
);
1374 buf
= &DummyBufferObject
;
1376 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1379 mtx_unlock(&ctx
->Shared
->Mutex
);
1383 * Generate a set of unique buffer object IDs and store them in \c buffers.
1385 * \param n Number of IDs to generate.
1386 * \param buffers Array of \c n locations to store the IDs.
1389 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1391 create_buffers(n
, buffers
, false);
1395 * Create a set of buffer objects and store their unique IDs in \c buffers.
1397 * \param n Number of IDs to generate.
1398 * \param buffers Array of \c n locations to store the IDs.
1401 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1403 create_buffers(n
, buffers
, true);
1408 * Determine if ID is the name of a buffer object.
1410 * \param id ID of the potential buffer object.
1411 * \return \c GL_TRUE if \c id is the name of a buffer object,
1412 * \c GL_FALSE otherwise.
1414 GLboolean GLAPIENTRY
1415 _mesa_IsBuffer(GLuint id
)
1417 struct gl_buffer_object
*bufObj
;
1418 GET_CURRENT_CONTEXT(ctx
);
1419 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1421 mtx_lock(&ctx
->Shared
->Mutex
);
1422 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1423 mtx_unlock(&ctx
->Shared
->Mutex
);
1425 return bufObj
&& bufObj
!= &DummyBufferObject
;
1430 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1431 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1432 GLbitfield flags
, const char *func
)
1435 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1439 if (flags
& ~(GL_MAP_READ_BIT
|
1441 GL_MAP_PERSISTENT_BIT
|
1442 GL_MAP_COHERENT_BIT
|
1443 GL_DYNAMIC_STORAGE_BIT
|
1444 GL_CLIENT_STORAGE_BIT
)) {
1445 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1449 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1450 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1451 _mesa_error(ctx
, GL_INVALID_VALUE
,
1452 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1456 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1457 _mesa_error(ctx
, GL_INVALID_VALUE
,
1458 "%s(COHERENT and flags!=PERSISTENT)", func
);
1462 if (bufObj
->Immutable
) {
1463 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1467 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1468 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1470 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1472 bufObj
->Written
= GL_TRUE
;
1473 bufObj
->Immutable
= GL_TRUE
;
1475 assert(ctx
->Driver
.BufferData
);
1476 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1478 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1479 /* Even though the interaction between AMD_pinned_memory and
1480 * glBufferStorage is not described in the spec, Graham Sellers
1481 * said that it should behave the same as glBufferData.
1483 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1486 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1492 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1495 GET_CURRENT_CONTEXT(ctx
);
1496 struct gl_buffer_object
*bufObj
;
1498 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1502 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1507 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1510 GET_CURRENT_CONTEXT(ctx
);
1511 struct gl_buffer_object
*bufObj
;
1513 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1518 * In direct state access, buffer objects have an unspecified target since
1519 * they are not required to be bound.
1521 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1522 "glNamedBufferStorage");
1527 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1528 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1529 GLenum usage
, const char *func
)
1533 if (MESA_VERBOSE
& VERBOSE_API
)
1534 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1536 _mesa_enum_to_string(target
),
1537 (long int) size
, data
,
1538 _mesa_enum_to_string(usage
));
1541 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1546 case GL_STREAM_DRAW_ARB
:
1547 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1550 case GL_STATIC_DRAW_ARB
:
1551 case GL_DYNAMIC_DRAW_ARB
:
1555 case GL_STREAM_READ_ARB
:
1556 case GL_STREAM_COPY_ARB
:
1557 case GL_STATIC_READ_ARB
:
1558 case GL_STATIC_COPY_ARB
:
1559 case GL_DYNAMIC_READ_ARB
:
1560 case GL_DYNAMIC_COPY_ARB
:
1561 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1565 valid_usage
= false;
1570 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1571 _mesa_enum_to_string(usage
));
1575 if (bufObj
->Immutable
) {
1576 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1580 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1581 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1583 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1585 bufObj
->Written
= GL_TRUE
;
1588 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1589 bufObj
->Name
, size
, data
, usage
);
1596 assert(ctx
->Driver
.BufferData
);
1597 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1600 GL_DYNAMIC_STORAGE_BIT
,
1602 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1603 /* From GL_AMD_pinned_memory:
1605 * INVALID_OPERATION is generated by BufferData if <target> is
1606 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1607 * mapped to the GPU address space.
1609 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1612 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1618 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1619 const GLvoid
*data
, GLenum usage
)
1621 GET_CURRENT_CONTEXT(ctx
);
1622 struct gl_buffer_object
*bufObj
;
1624 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1628 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1633 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1636 GET_CURRENT_CONTEXT(ctx
);
1637 struct gl_buffer_object
*bufObj
;
1639 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1643 /* In direct state access, buffer objects have an unspecified target since
1644 * they are not required to be bound.
1646 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1647 "glNamedBufferData");
1652 * Implementation for glBufferSubData and glNamedBufferSubData.
1654 * \param ctx GL context.
1655 * \param bufObj The buffer object.
1656 * \param offset Offset of the first byte of the subdata range.
1657 * \param size Size, in bytes, of the subdata range.
1658 * \param data The data store.
1659 * \param func Name of calling function for recording errors.
1663 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1664 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1667 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1669 /* error already recorded */
1673 if (bufObj
->Immutable
&&
1674 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1675 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1682 bufObj
->Written
= GL_TRUE
;
1684 assert(ctx
->Driver
.BufferSubData
);
1685 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1689 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1690 GLsizeiptr size
, const GLvoid
*data
)
1692 GET_CURRENT_CONTEXT(ctx
);
1693 struct gl_buffer_object
*bufObj
;
1695 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1699 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1703 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1704 GLsizeiptr size
, const GLvoid
*data
)
1706 GET_CURRENT_CONTEXT(ctx
);
1707 struct gl_buffer_object
*bufObj
;
1709 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1713 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1714 "glNamedBufferSubData");
1719 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1720 GLsizeiptr size
, GLvoid
*data
)
1722 GET_CURRENT_CONTEXT(ctx
);
1723 struct gl_buffer_object
*bufObj
;
1725 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1726 GL_INVALID_OPERATION
);
1730 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1731 "glGetBufferSubData")) {
1735 assert(ctx
->Driver
.GetBufferSubData
);
1736 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1740 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1741 GLsizeiptr size
, GLvoid
*data
)
1743 GET_CURRENT_CONTEXT(ctx
);
1744 struct gl_buffer_object
*bufObj
;
1746 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1747 "glGetNamedBufferSubData");
1751 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1752 "glGetNamedBufferSubData")) {
1756 assert(ctx
->Driver
.GetBufferSubData
);
1757 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1762 * \param subdata true if caller is *SubData, false if *Data
1765 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1766 struct gl_buffer_object
*bufObj
,
1767 GLenum internalformat
,
1768 GLintptr offset
, GLsizeiptr size
,
1769 GLenum format
, GLenum type
,
1771 const char *func
, bool subdata
)
1773 mesa_format mesaFormat
;
1774 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1775 GLsizeiptr clearValueSize
;
1777 /* This checks for disallowed mappings. */
1778 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1783 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1784 format
, type
, func
);
1786 if (mesaFormat
== MESA_FORMAT_NONE
) {
1790 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1791 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1792 _mesa_error(ctx
, GL_INVALID_VALUE
,
1793 "%s(offset or size is not a multiple of "
1794 "internalformat size)", func
);
1799 /* clear to zeros, per the spec */
1801 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1802 NULL
, clearValueSize
, bufObj
);
1807 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1808 format
, type
, data
, func
)) {
1813 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1814 clearValue
, clearValueSize
, bufObj
);
1819 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1820 GLenum type
, const GLvoid
*data
)
1822 GET_CURRENT_CONTEXT(ctx
);
1823 struct gl_buffer_object
*bufObj
;
1825 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1829 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1831 "glClearBufferData", false);
1835 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1836 GLenum format
, GLenum type
, const GLvoid
*data
)
1838 GET_CURRENT_CONTEXT(ctx
);
1839 struct gl_buffer_object
*bufObj
;
1841 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1845 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1847 "glClearNamedBufferData", false);
1852 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1853 GLintptr offset
, GLsizeiptr size
,
1854 GLenum format
, GLenum type
,
1857 GET_CURRENT_CONTEXT(ctx
);
1858 struct gl_buffer_object
*bufObj
;
1860 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1864 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1866 "glClearBufferSubData", true);
1870 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1871 GLintptr offset
, GLsizeiptr size
,
1872 GLenum format
, GLenum type
,
1875 GET_CURRENT_CONTEXT(ctx
);
1876 struct gl_buffer_object
*bufObj
;
1878 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1879 "glClearNamedBufferSubData");
1883 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1885 "glClearNamedBufferSubData", true);
1890 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1893 GLboolean status
= GL_TRUE
;
1894 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1896 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
1897 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1898 "%s(buffer is not mapped)", func
);
1903 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1904 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1906 /* check that last 100 bytes are still = magic value */
1907 for (i
= 0; i
< 100; i
++) {
1908 GLuint pos
= bufObj
->Size
- i
- 1;
1909 if (buf
[pos
] != 123) {
1910 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1911 " at position %d (value = %u)\n",
1919 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1920 GLuint i
, unchanged
= 0;
1921 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1923 /* check which bytes changed */
1924 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1925 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1932 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1933 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1938 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
1939 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
1940 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
1941 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
1942 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
1947 GLboolean GLAPIENTRY
1948 _mesa_UnmapBuffer(GLenum target
)
1950 GET_CURRENT_CONTEXT(ctx
);
1951 struct gl_buffer_object
*bufObj
;
1953 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
1957 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
1960 GLboolean GLAPIENTRY
1961 _mesa_UnmapNamedBuffer(GLuint buffer
)
1963 GET_CURRENT_CONTEXT(ctx
);
1964 struct gl_buffer_object
*bufObj
;
1966 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
1970 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
1975 get_buffer_parameter(struct gl_context
*ctx
,
1976 struct gl_buffer_object
*bufObj
, GLenum pname
,
1977 GLint64
*params
, const char *func
)
1980 case GL_BUFFER_SIZE_ARB
:
1981 *params
= bufObj
->Size
;
1983 case GL_BUFFER_USAGE_ARB
:
1984 *params
= bufObj
->Usage
;
1986 case GL_BUFFER_ACCESS_ARB
:
1987 *params
= simplified_access_mode(ctx
,
1988 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
1990 case GL_BUFFER_MAPPED_ARB
:
1991 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
1993 case GL_BUFFER_ACCESS_FLAGS
:
1994 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1996 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
1998 case GL_BUFFER_MAP_OFFSET
:
1999 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2001 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2003 case GL_BUFFER_MAP_LENGTH
:
2004 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2006 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2008 case GL_BUFFER_IMMUTABLE_STORAGE
:
2009 if (!ctx
->Extensions
.ARB_buffer_storage
)
2011 *params
= bufObj
->Immutable
;
2013 case GL_BUFFER_STORAGE_FLAGS
:
2014 if (!ctx
->Extensions
.ARB_buffer_storage
)
2016 *params
= bufObj
->StorageFlags
;
2025 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2026 _mesa_enum_to_string(pname
));
2031 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2033 GET_CURRENT_CONTEXT(ctx
);
2034 struct gl_buffer_object
*bufObj
;
2037 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2038 GL_INVALID_OPERATION
);
2042 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2043 "glGetBufferParameteriv"))
2044 return; /* Error already recorded. */
2046 *params
= (GLint
) parameter
;
2050 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2052 GET_CURRENT_CONTEXT(ctx
);
2053 struct gl_buffer_object
*bufObj
;
2056 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2057 GL_INVALID_OPERATION
);
2061 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2062 "glGetBufferParameteri64v"))
2063 return; /* Error already recorded. */
2065 *params
= parameter
;
2069 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2071 GET_CURRENT_CONTEXT(ctx
);
2072 struct gl_buffer_object
*bufObj
;
2075 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2076 "glGetNamedBufferParameteriv");
2080 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2081 "glGetNamedBufferParameteriv"))
2082 return; /* Error already recorded. */
2084 *params
= (GLint
) parameter
;
2088 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2091 GET_CURRENT_CONTEXT(ctx
);
2092 struct gl_buffer_object
*bufObj
;
2095 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2096 "glGetNamedBufferParameteri64v");
2100 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2101 "glGetNamedBufferParameteri64v"))
2102 return; /* Error already recorded. */
2104 *params
= parameter
;
2109 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2111 GET_CURRENT_CONTEXT(ctx
);
2112 struct gl_buffer_object
*bufObj
;
2114 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2115 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2116 "GL_BUFFER_MAP_POINTER)");
2120 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2121 GL_INVALID_OPERATION
);
2125 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2129 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2131 GET_CURRENT_CONTEXT(ctx
);
2132 struct gl_buffer_object
*bufObj
;
2134 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2135 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2136 "GL_BUFFER_MAP_POINTER)");
2140 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2141 "glGetNamedBufferPointerv");
2145 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2150 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2151 struct gl_buffer_object
*src
,
2152 struct gl_buffer_object
*dst
,
2153 GLintptr readOffset
, GLintptr writeOffset
,
2154 GLsizeiptr size
, const char *func
)
2156 if (_mesa_check_disallowed_mapping(src
)) {
2157 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2158 "%s(readBuffer is mapped)", func
);
2162 if (_mesa_check_disallowed_mapping(dst
)) {
2163 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2164 "%s(writeBuffer is mapped)", func
);
2168 if (readOffset
< 0) {
2169 _mesa_error(ctx
, GL_INVALID_VALUE
,
2170 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2174 if (writeOffset
< 0) {
2175 _mesa_error(ctx
, GL_INVALID_VALUE
,
2176 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2181 _mesa_error(ctx
, GL_INVALID_VALUE
,
2182 "%s(size %d < 0)", func
, (int) size
);
2186 if (readOffset
+ size
> src
->Size
) {
2187 _mesa_error(ctx
, GL_INVALID_VALUE
,
2188 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2189 (int) readOffset
, (int) size
, (int) src
->Size
);
2193 if (writeOffset
+ size
> dst
->Size
) {
2194 _mesa_error(ctx
, GL_INVALID_VALUE
,
2195 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2196 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2201 if (readOffset
+ size
<= writeOffset
) {
2204 else if (writeOffset
+ size
<= readOffset
) {
2208 /* overlapping src/dst is illegal */
2209 _mesa_error(ctx
, GL_INVALID_VALUE
,
2210 "%s(overlapping src/dst)", func
);
2215 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2219 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2220 GLintptr readOffset
, GLintptr writeOffset
,
2223 GET_CURRENT_CONTEXT(ctx
);
2224 struct gl_buffer_object
*src
, *dst
;
2226 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2227 GL_INVALID_OPERATION
);
2231 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2232 GL_INVALID_OPERATION
);
2236 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2237 "glCopyBufferSubData");
2241 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2242 GLintptr readOffset
, GLintptr writeOffset
,
2245 GET_CURRENT_CONTEXT(ctx
);
2246 struct gl_buffer_object
*src
, *dst
;
2248 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2249 "glCopyNamedBufferSubData");
2253 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2254 "glCopyNamedBufferSubData");
2258 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2259 "glCopyNamedBufferSubData");
2264 _mesa_map_buffer_range(struct gl_context
*ctx
,
2265 struct gl_buffer_object
*bufObj
,
2266 GLintptr offset
, GLsizeiptr length
,
2267 GLbitfield access
, const char *func
)
2270 GLbitfield allowed_access
;
2272 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2275 _mesa_error(ctx
, GL_INVALID_VALUE
,
2276 "%s(offset %ld < 0)", func
, (long) offset
);
2281 _mesa_error(ctx
, GL_INVALID_VALUE
,
2282 "%s(length %ld < 0)", func
, (long) length
);
2286 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2288 * "An INVALID_OPERATION error is generated for any of the following
2291 * * <length> is zero."
2293 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2294 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2298 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2302 allowed_access
= GL_MAP_READ_BIT
|
2304 GL_MAP_INVALIDATE_RANGE_BIT
|
2305 GL_MAP_INVALIDATE_BUFFER_BIT
|
2306 GL_MAP_FLUSH_EXPLICIT_BIT
|
2307 GL_MAP_UNSYNCHRONIZED_BIT
;
2309 if (ctx
->Extensions
.ARB_buffer_storage
) {
2310 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2311 GL_MAP_COHERENT_BIT
;
2314 if (access
& ~allowed_access
) {
2315 /* generate an error if any bits other than those allowed are set */
2316 _mesa_error(ctx
, GL_INVALID_VALUE
,
2317 "%s(access has undefined bits set)", func
);
2321 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2322 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2323 "%s(access indicates neither read or write)", func
);
2327 if ((access
& GL_MAP_READ_BIT
) &&
2328 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2329 GL_MAP_INVALIDATE_BUFFER_BIT
|
2330 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2331 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2332 "%s(read access with disallowed bits)", func
);
2336 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2337 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2338 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2339 "%s(access has flush explicit without write)", func
);
2343 if (access
& GL_MAP_READ_BIT
&&
2344 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2345 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2346 "%s(buffer does not allow read access)", func
);
2350 if (access
& GL_MAP_WRITE_BIT
&&
2351 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2352 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2353 "%s(buffer does not allow write access)", func
);
2357 if (access
& GL_MAP_COHERENT_BIT
&&
2358 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2359 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2360 "%s(buffer does not allow coherent access)", func
);
2364 if (access
& GL_MAP_PERSISTENT_BIT
&&
2365 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2366 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2367 "%s(buffer does not allow persistent access)", func
);
2371 if (offset
+ length
> bufObj
->Size
) {
2372 _mesa_error(ctx
, GL_INVALID_VALUE
,
2373 "%s(offset %td + length %td > buffer_size %td)", func
,
2374 offset
, length
, bufObj
->Size
);
2378 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2379 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2380 "%s(buffer already mapped)", func
);
2384 if (!bufObj
->Size
) {
2385 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2390 assert(ctx
->Driver
.MapBufferRange
);
2391 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2394 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2397 /* The driver callback should have set all these fields.
2398 * This is important because other modules (like VBO) might call
2399 * the driver function directly.
2401 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2402 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2403 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2404 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2407 if (access
& GL_MAP_WRITE_BIT
)
2408 bufObj
->Written
= GL_TRUE
;
2411 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2412 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2413 bufObj
->Name
, bufObj
->Size
, access
);
2414 /* Access must be write only */
2415 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2417 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2418 for (i
= 0; i
< bufObj
->Size
; i
++)
2425 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2426 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2428 /* buffer is 100 bytes larger than requested, fill with magic value */
2429 for (i
= 0; i
< 100; i
++) {
2430 buf
[bufObj
->Size
- i
- 1] = 123;
2439 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2442 GET_CURRENT_CONTEXT(ctx
);
2443 struct gl_buffer_object
*bufObj
;
2445 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2446 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2447 "glMapBufferRange(ARB_map_buffer_range not supported)");
2451 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2455 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2456 "glMapBufferRange");
2460 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2463 GET_CURRENT_CONTEXT(ctx
);
2464 struct gl_buffer_object
*bufObj
;
2466 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2467 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2468 "glMapNamedBufferRange("
2469 "ARB_map_buffer_range not supported)");
2473 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2477 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2478 "glMapNamedBufferRange");
2482 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2483 * flags for input to _mesa_map_buffer_range.
2485 * \return true if the type of requested access is permissible.
2488 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2492 case GL_READ_ONLY_ARB
:
2493 *flags
= GL_MAP_READ_BIT
;
2494 return _mesa_is_desktop_gl(ctx
);
2495 case GL_WRITE_ONLY_ARB
:
2496 *flags
= GL_MAP_WRITE_BIT
;
2498 case GL_READ_WRITE_ARB
:
2499 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2500 return _mesa_is_desktop_gl(ctx
);
2507 _mesa_MapBuffer(GLenum target
, GLenum access
)
2509 GET_CURRENT_CONTEXT(ctx
);
2510 struct gl_buffer_object
*bufObj
;
2511 GLbitfield accessFlags
;
2513 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2514 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2518 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2522 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2527 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2529 GET_CURRENT_CONTEXT(ctx
);
2530 struct gl_buffer_object
*bufObj
;
2531 GLbitfield accessFlags
;
2533 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2534 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2538 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2542 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2543 "glMapNamedBuffer");
2548 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2549 struct gl_buffer_object
*bufObj
,
2550 GLintptr offset
, GLsizeiptr length
,
2553 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2554 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2555 "%s(ARB_map_buffer_range not supported)", func
);
2560 _mesa_error(ctx
, GL_INVALID_VALUE
,
2561 "%s(offset %ld < 0)", func
, (long) offset
);
2566 _mesa_error(ctx
, GL_INVALID_VALUE
,
2567 "%s(length %ld < 0)", func
, (long) length
);
2571 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2572 /* buffer is not mapped */
2573 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2574 "%s(buffer is not mapped)", func
);
2578 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2579 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2580 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2581 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2585 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2586 _mesa_error(ctx
, GL_INVALID_VALUE
,
2587 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2588 (long) offset
, (long) length
,
2589 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2593 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2595 if (ctx
->Driver
.FlushMappedBufferRange
)
2596 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2601 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2604 GET_CURRENT_CONTEXT(ctx
);
2605 struct gl_buffer_object
*bufObj
;
2607 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2608 GL_INVALID_OPERATION
);
2612 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2613 "glFlushMappedBufferRange");
2617 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2620 GET_CURRENT_CONTEXT(ctx
);
2621 struct gl_buffer_object
*bufObj
;
2623 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2624 "glFlushMappedNamedBufferRange");
2628 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2629 "glFlushMappedNamedBufferRange");
2634 * Binds a buffer object to a uniform buffer binding point.
2636 * The caller is responsible for flushing vertices and updating
2640 set_ubo_binding(struct gl_context
*ctx
,
2641 struct gl_uniform_buffer_binding
*binding
,
2642 struct gl_buffer_object
*bufObj
,
2647 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2649 binding
->Offset
= offset
;
2650 binding
->Size
= size
;
2651 binding
->AutomaticSize
= autoSize
;
2653 /* If this is a real buffer object, mark it has having been used
2654 * at some point as a UBO.
2657 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2661 * Binds a buffer object to a shader storage buffer binding point.
2663 * The caller is responsible for flushing vertices and updating
2667 set_ssbo_binding(struct gl_context
*ctx
,
2668 struct gl_shader_storage_buffer_binding
*binding
,
2669 struct gl_buffer_object
*bufObj
,
2674 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2676 binding
->Offset
= offset
;
2677 binding
->Size
= size
;
2678 binding
->AutomaticSize
= autoSize
;
2680 /* If this is a real buffer object, mark it has having been used
2681 * at some point as a SSBO.
2684 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
2688 * Binds a buffer object to a uniform buffer binding point.
2690 * Unlike set_ubo_binding(), this function also flushes vertices
2691 * and updates NewDriverState. It also checks if the binding
2692 * has actually changed before updating it.
2695 bind_uniform_buffer(struct gl_context
*ctx
,
2697 struct gl_buffer_object
*bufObj
,
2702 struct gl_uniform_buffer_binding
*binding
=
2703 &ctx
->UniformBufferBindings
[index
];
2705 if (binding
->BufferObject
== bufObj
&&
2706 binding
->Offset
== offset
&&
2707 binding
->Size
== size
&&
2708 binding
->AutomaticSize
== autoSize
) {
2712 FLUSH_VERTICES(ctx
, 0);
2713 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2715 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2719 * Binds a buffer object to a shader storage buffer binding point.
2721 * Unlike set_ssbo_binding(), this function also flushes vertices
2722 * and updates NewDriverState. It also checks if the binding
2723 * has actually changed before updating it.
2726 bind_shader_storage_buffer(struct gl_context
*ctx
,
2728 struct gl_buffer_object
*bufObj
,
2733 struct gl_shader_storage_buffer_binding
*binding
=
2734 &ctx
->ShaderStorageBufferBindings
[index
];
2736 if (binding
->BufferObject
== bufObj
&&
2737 binding
->Offset
== offset
&&
2738 binding
->Size
== size
&&
2739 binding
->AutomaticSize
== autoSize
) {
2743 FLUSH_VERTICES(ctx
, 0);
2744 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
2746 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2750 * Bind a region of a buffer object to a uniform block binding point.
2751 * \param index the uniform buffer binding point index
2752 * \param bufObj the buffer object
2753 * \param offset offset to the start of buffer object region
2754 * \param size size of the buffer object region
2757 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2759 struct gl_buffer_object
*bufObj
,
2763 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2764 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2768 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2769 _mesa_error(ctx
, GL_INVALID_VALUE
,
2770 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2771 ctx
->Const
.UniformBufferOffsetAlignment
);
2775 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2780 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2781 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2785 * Bind a region of a buffer object to a shader storage block binding point.
2786 * \param index the shader storage buffer binding point index
2787 * \param bufObj the buffer object
2788 * \param offset offset to the start of buffer object region
2789 * \param size size of the buffer object region
2792 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
2794 struct gl_buffer_object
*bufObj
,
2798 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2799 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2803 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
2804 _mesa_error(ctx
, GL_INVALID_VALUE
,
2805 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2806 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
2810 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2815 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2816 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2820 * Bind a buffer object to a uniform block binding point.
2821 * As above, but offset = 0.
2824 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2826 struct gl_buffer_object
*bufObj
)
2828 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2829 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2833 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2835 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2836 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2838 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2842 * Bind a buffer object to a shader storage block binding point.
2843 * As above, but offset = 0.
2846 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
2848 struct gl_buffer_object
*bufObj
)
2850 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2851 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2855 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2857 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2858 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2860 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2864 * Binds a buffer object to an atomic buffer binding point.
2866 * The caller is responsible for validating the offset,
2867 * flushing the vertices and updating NewDriverState.
2870 set_atomic_buffer_binding(struct gl_context
*ctx
,
2871 struct gl_atomic_buffer_binding
*binding
,
2872 struct gl_buffer_object
*bufObj
,
2876 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2878 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2879 binding
->Offset
= -1;
2882 binding
->Offset
= offset
;
2883 binding
->Size
= size
;
2884 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
2889 * Binds a buffer object to an atomic buffer binding point.
2891 * Unlike set_atomic_buffer_binding(), this function also validates the
2892 * index and offset, flushes vertices, and updates NewDriverState.
2893 * It also checks if the binding has actually changing before
2897 bind_atomic_buffer(struct gl_context
*ctx
,
2899 struct gl_buffer_object
*bufObj
,
2904 struct gl_atomic_buffer_binding
*binding
;
2906 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
2907 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
2911 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
2912 _mesa_error(ctx
, GL_INVALID_VALUE
,
2913 "%s(offset misaligned %d/%d)", name
, (int) offset
,
2914 ATOMIC_COUNTER_SIZE
);
2918 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
2920 binding
= &ctx
->AtomicBufferBindings
[index
];
2921 if (binding
->BufferObject
== bufObj
&&
2922 binding
->Offset
== offset
&&
2923 binding
->Size
== size
) {
2927 FLUSH_VERTICES(ctx
, 0);
2928 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
2930 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
2934 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
2936 const GLintptr
*offsets
,
2937 const GLsizeiptr
*sizes
)
2939 if (offsets
[index
] < 0) {
2940 /* The ARB_multi_bind spec says:
2942 * "An INVALID_VALUE error is generated by BindBuffersRange if any
2943 * value in <offsets> is less than zero (per binding)."
2945 _mesa_error(ctx
, GL_INVALID_VALUE
,
2946 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
2947 index
, (int64_t) offsets
[index
]);
2951 if (sizes
[index
] <= 0) {
2952 /* The ARB_multi_bind spec says:
2954 * "An INVALID_VALUE error is generated by BindBuffersRange if any
2955 * value in <sizes> is less than or equal to zero (per binding)."
2957 _mesa_error(ctx
, GL_INVALID_VALUE
,
2958 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
2959 index
, (int64_t) sizes
[index
]);
2967 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
2968 GLuint first
, GLsizei count
,
2971 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
2972 _mesa_error(ctx
, GL_INVALID_ENUM
,
2973 "%s(target=GL_UNIFORM_BUFFER)", caller
);
2977 /* The ARB_multi_bind_spec says:
2979 * "An INVALID_OPERATION error is generated if <first> + <count> is
2980 * greater than the number of target-specific indexed binding points,
2981 * as described in section 6.7.1."
2983 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
2984 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2985 "%s(first=%u + count=%d > the value of "
2986 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
2987 caller
, first
, count
,
2988 ctx
->Const
.MaxUniformBufferBindings
);
2996 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
2997 GLuint first
, GLsizei count
,
3000 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3001 _mesa_error(ctx
, GL_INVALID_ENUM
,
3002 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3006 /* The ARB_multi_bind_spec says:
3008 * "An INVALID_OPERATION error is generated if <first> + <count> is
3009 * greater than the number of target-specific indexed binding points,
3010 * as described in section 6.7.1."
3012 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3013 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3014 "%s(first=%u + count=%d > the value of "
3015 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3016 caller
, first
, count
,
3017 ctx
->Const
.MaxShaderStorageBufferBindings
);
3025 * Unbind all uniform buffers in the range
3026 * <first> through <first>+<count>-1
3029 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3031 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3034 for (i
= 0; i
< count
; i
++)
3035 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3036 bufObj
, -1, -1, GL_TRUE
);
3040 * Unbind all shader storage buffers in the range
3041 * <first> through <first>+<count>-1
3044 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3047 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3050 for (i
= 0; i
< count
; i
++)
3051 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3052 bufObj
, -1, -1, GL_TRUE
);
3056 bind_uniform_buffers_base(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3057 const GLuint
*buffers
)
3061 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, "glBindBuffersBase"))
3064 /* Assume that at least one binding will be changed */
3065 FLUSH_VERTICES(ctx
, 0);
3066 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3069 /* The ARB_multi_bind spec says:
3071 * "If <buffers> is NULL, all bindings from <first> through
3072 * <first>+<count>-1 are reset to their unbound (zero) state."
3074 unbind_uniform_buffers(ctx
, first
, count
);
3078 /* Note that the error semantics for multi-bind commands differ from
3079 * those of other GL commands.
3081 * The Issues section in the ARB_multi_bind spec says:
3083 * "(11) Typically, OpenGL specifies that if an error is generated by a
3084 * command, that command has no effect. This is somewhat
3085 * unfortunate for multi-bind commands, because it would require a
3086 * first pass to scan the entire list of bound objects for errors
3087 * and then a second pass to actually perform the bindings.
3088 * Should we have different error semantics?
3090 * RESOLVED: Yes. In this specification, when the parameters for
3091 * one of the <count> binding points are invalid, that binding point
3092 * is not updated and an error will be generated. However, other
3093 * binding points in the same command will be updated if their
3094 * parameters are valid and no other error occurs."
3097 _mesa_begin_bufferobj_lookups(ctx
);
3099 for (i
= 0; i
< count
; i
++) {
3100 struct gl_uniform_buffer_binding
*binding
=
3101 &ctx
->UniformBufferBindings
[first
+ i
];
3102 struct gl_buffer_object
*bufObj
;
3104 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3105 bufObj
= binding
->BufferObject
;
3107 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3108 "glBindBuffersBase");
3111 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3112 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_TRUE
);
3114 set_ubo_binding(ctx
, binding
, bufObj
, 0, 0, GL_TRUE
);
3118 _mesa_end_bufferobj_lookups(ctx
);
3122 bind_shader_storage_buffers_base(struct gl_context
*ctx
, GLuint first
,
3123 GLsizei count
, const GLuint
*buffers
)
3127 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
,
3128 "glBindBuffersBase"))
3131 /* Assume that at least one binding will be changed */
3132 FLUSH_VERTICES(ctx
, 0);
3133 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3136 /* The ARB_multi_bind spec says:
3138 * "If <buffers> is NULL, all bindings from <first> through
3139 * <first>+<count>-1 are reset to their unbound (zero) state."
3141 unbind_shader_storage_buffers(ctx
, first
, count
);
3145 /* Note that the error semantics for multi-bind commands differ from
3146 * those of other GL commands.
3148 * The Issues section in the ARB_multi_bind spec says:
3150 * "(11) Typically, OpenGL specifies that if an error is generated by a
3151 * command, that command has no effect. This is somewhat
3152 * unfortunate for multi-bind commands, because it would require a
3153 * first pass to scan the entire list of bound objects for errors
3154 * and then a second pass to actually perform the bindings.
3155 * Should we have different error semantics?
3157 * RESOLVED: Yes. In this specification, when the parameters for
3158 * one of the <count> binding points are invalid, that binding point
3159 * is not updated and an error will be generated. However, other
3160 * binding points in the same command will be updated if their
3161 * parameters are valid and no other error occurs."
3164 _mesa_begin_bufferobj_lookups(ctx
);
3166 for (i
= 0; i
< count
; i
++) {
3167 struct gl_shader_storage_buffer_binding
*binding
=
3168 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3169 struct gl_buffer_object
*bufObj
;
3171 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3172 bufObj
= binding
->BufferObject
;
3174 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3175 "glBindBuffersBase");
3178 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3179 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, GL_TRUE
);
3181 set_ssbo_binding(ctx
, binding
, bufObj
, 0, 0, GL_TRUE
);
3185 _mesa_end_bufferobj_lookups(ctx
);
3189 bind_uniform_buffers_range(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3190 const GLuint
*buffers
,
3191 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3195 if (!error_check_bind_uniform_buffers(ctx
, first
, count
,
3196 "glBindBuffersRange"))
3199 /* Assume that at least one binding will be changed */
3200 FLUSH_VERTICES(ctx
, 0);
3201 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3204 /* The ARB_multi_bind spec says:
3206 * "If <buffers> is NULL, all bindings from <first> through
3207 * <first>+<count>-1 are reset to their unbound (zero) state.
3208 * In this case, the offsets and sizes associated with the
3209 * binding points are set to default values, ignoring
3210 * <offsets> and <sizes>."
3212 unbind_uniform_buffers(ctx
, first
, count
);
3216 /* Note that the error semantics for multi-bind commands differ from
3217 * those of other GL commands.
3219 * The Issues section in the ARB_multi_bind spec says:
3221 * "(11) Typically, OpenGL specifies that if an error is generated by a
3222 * command, that command has no effect. This is somewhat
3223 * unfortunate for multi-bind commands, because it would require a
3224 * first pass to scan the entire list of bound objects for errors
3225 * and then a second pass to actually perform the bindings.
3226 * Should we have different error semantics?
3228 * RESOLVED: Yes. In this specification, when the parameters for
3229 * one of the <count> binding points are invalid, that binding point
3230 * is not updated and an error will be generated. However, other
3231 * binding points in the same command will be updated if their
3232 * parameters are valid and no other error occurs."
3235 _mesa_begin_bufferobj_lookups(ctx
);
3237 for (i
= 0; i
< count
; i
++) {
3238 struct gl_uniform_buffer_binding
*binding
=
3239 &ctx
->UniformBufferBindings
[first
+ i
];
3240 struct gl_buffer_object
*bufObj
;
3242 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3245 /* The ARB_multi_bind spec says:
3247 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3248 * pair of values in <offsets> and <sizes> does not respectively
3249 * satisfy the constraints described for those parameters for the
3250 * specified target, as described in section 6.7.1 (per binding)."
3252 * Section 6.7.1 refers to table 6.5, which says:
3254 * "┌───────────────────────────────────────────────────────────────┐
3255 * │ Uniform buffer array bindings (see sec. 7.6) │
3256 * ├─────────────────────┬─────────────────────────────────────────┤
3258 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3259 * │ │ OFFSET_ALIGNMENT │
3261 * │ size restriction │ none │
3262 * └─────────────────────┴─────────────────────────────────────────┘"
3264 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3265 _mesa_error(ctx
, GL_INVALID_VALUE
,
3266 "glBindBuffersRange(offsets[%u]=%" PRId64
3267 " is misaligned; it must be a multiple of the value of "
3268 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3269 "target=GL_UNIFORM_BUFFER)",
3270 i
, (int64_t) offsets
[i
],
3271 ctx
->Const
.UniformBufferOffsetAlignment
);
3275 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3276 bufObj
= binding
->BufferObject
;
3278 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3279 "glBindBuffersRange");
3282 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3283 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_FALSE
);
3285 set_ubo_binding(ctx
, binding
, bufObj
,
3286 offsets
[i
], sizes
[i
], GL_FALSE
);
3290 _mesa_end_bufferobj_lookups(ctx
);
3294 bind_shader_storage_buffers_range(struct gl_context
*ctx
, GLuint first
,
3295 GLsizei count
, const GLuint
*buffers
,
3296 const GLintptr
*offsets
,
3297 const GLsizeiptr
*sizes
)
3301 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
,
3302 "glBindBuffersRange"))
3305 /* Assume that at least one binding will be changed */
3306 FLUSH_VERTICES(ctx
, 0);
3307 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3310 /* The ARB_multi_bind spec says:
3312 * "If <buffers> is NULL, all bindings from <first> through
3313 * <first>+<count>-1 are reset to their unbound (zero) state.
3314 * In this case, the offsets and sizes associated with the
3315 * binding points are set to default values, ignoring
3316 * <offsets> and <sizes>."
3318 unbind_shader_storage_buffers(ctx
, first
, count
);
3322 /* Note that the error semantics for multi-bind commands differ from
3323 * those of other GL commands.
3325 * The Issues section in the ARB_multi_bind spec says:
3327 * "(11) Typically, OpenGL specifies that if an error is generated by a
3328 * command, that command has no effect. This is somewhat
3329 * unfortunate for multi-bind commands, because it would require a
3330 * first pass to scan the entire list of bound objects for errors
3331 * and then a second pass to actually perform the bindings.
3332 * Should we have different error semantics?
3334 * RESOLVED: Yes. In this specification, when the parameters for
3335 * one of the <count> binding points are invalid, that binding point
3336 * is not updated and an error will be generated. However, other
3337 * binding points in the same command will be updated if their
3338 * parameters are valid and no other error occurs."
3341 _mesa_begin_bufferobj_lookups(ctx
);
3343 for (i
= 0; i
< count
; i
++) {
3344 struct gl_shader_storage_buffer_binding
*binding
=
3345 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3346 struct gl_buffer_object
*bufObj
;
3348 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3351 /* The ARB_multi_bind spec says:
3353 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3354 * pair of values in <offsets> and <sizes> does not respectively
3355 * satisfy the constraints described for those parameters for the
3356 * specified target, as described in section 6.7.1 (per binding)."
3358 * Section 6.7.1 refers to table 6.5, which says:
3360 * "┌───────────────────────────────────────────────────────────────┐
3361 * │ Shader storage buffer array bindings (see sec. 7.8) │
3362 * ├─────────────────────┬─────────────────────────────────────────┤
3364 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3365 * │ │ BUFFER_OFFSET_ALIGNMENT │
3367 * │ size restriction │ none │
3368 * └─────────────────────┴─────────────────────────────────────────┘"
3370 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3371 _mesa_error(ctx
, GL_INVALID_VALUE
,
3372 "glBindBuffersRange(offsets[%u]=%" PRId64
3373 " is misaligned; it must be a multiple of the value of "
3374 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3375 "target=GL_SHADER_STORAGE_BUFFER)",
3376 i
, (int64_t) offsets
[i
],
3377 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3381 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3382 bufObj
= binding
->BufferObject
;
3384 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3385 "glBindBuffersRange");
3388 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3389 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, GL_FALSE
);
3391 set_ssbo_binding(ctx
, binding
, bufObj
,
3392 offsets
[i
], sizes
[i
], GL_FALSE
);
3396 _mesa_end_bufferobj_lookups(ctx
);
3400 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3401 struct gl_transform_feedback_object
*tfObj
,
3402 GLuint first
, GLsizei count
, const char *caller
)
3404 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3405 _mesa_error(ctx
, GL_INVALID_ENUM
,
3406 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3410 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3412 * "An INVALID_OPERATION error is generated :
3415 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3416 * FEEDBACK_BUFFER and transform feedback is currently active."
3418 * We assume that this is also meant to apply to BindBuffersRange
3419 * and BindBuffersBase.
3421 if (tfObj
->Active
) {
3422 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3423 "%s(Changing transform feedback buffers while "
3424 "transform feedback is active)", caller
);
3428 /* The ARB_multi_bind_spec says:
3430 * "An INVALID_OPERATION error is generated if <first> + <count> is
3431 * greater than the number of target-specific indexed binding points,
3432 * as described in section 6.7.1."
3434 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3435 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3436 "%s(first=%u + count=%d > the value of "
3437 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3438 caller
, first
, count
,
3439 ctx
->Const
.MaxTransformFeedbackBuffers
);
3447 * Unbind all transform feedback buffers in the range
3448 * <first> through <first>+<count>-1
3451 unbind_xfb_buffers(struct gl_context
*ctx
,
3452 struct gl_transform_feedback_object
*tfObj
,
3453 GLuint first
, GLsizei count
)
3455 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3458 for (i
= 0; i
< count
; i
++)
3459 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3464 bind_xfb_buffers_base(struct gl_context
*ctx
,
3465 GLuint first
, GLsizei count
,
3466 const GLuint
*buffers
)
3468 struct gl_transform_feedback_object
*tfObj
=
3469 ctx
->TransformFeedback
.CurrentObject
;
3472 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3473 "glBindBuffersBase"))
3476 /* Assume that at least one binding will be changed */
3477 FLUSH_VERTICES(ctx
, 0);
3478 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3481 /* The ARB_multi_bind spec says:
3483 * "If <buffers> is NULL, all bindings from <first> through
3484 * <first>+<count>-1 are reset to their unbound (zero) state."
3486 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3490 /* Note that the error semantics for multi-bind commands differ from
3491 * those of other GL commands.
3493 * The Issues section in the ARB_multi_bind spec says:
3495 * "(11) Typically, OpenGL specifies that if an error is generated by a
3496 * command, that command has no effect. This is somewhat
3497 * unfortunate for multi-bind commands, because it would require a
3498 * first pass to scan the entire list of bound objects for errors
3499 * and then a second pass to actually perform the bindings.
3500 * Should we have different error semantics?
3502 * RESOLVED: Yes. In this specification, when the parameters for
3503 * one of the <count> binding points are invalid, that binding point
3504 * is not updated and an error will be generated. However, other
3505 * binding points in the same command will be updated if their
3506 * parameters are valid and no other error occurs."
3509 _mesa_begin_bufferobj_lookups(ctx
);
3511 for (i
= 0; i
< count
; i
++) {
3512 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[first
+ i
];
3513 struct gl_buffer_object
*bufObj
;
3515 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3516 bufObj
= boundBufObj
;
3518 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3519 "glBindBuffersBase");
3522 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3526 _mesa_end_bufferobj_lookups(ctx
);
3530 bind_xfb_buffers_range(struct gl_context
*ctx
,
3531 GLuint first
, GLsizei count
,
3532 const GLuint
*buffers
,
3533 const GLintptr
*offsets
,
3534 const GLsizeiptr
*sizes
)
3536 struct gl_transform_feedback_object
*tfObj
=
3537 ctx
->TransformFeedback
.CurrentObject
;
3540 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3541 "glBindBuffersRange"))
3544 /* Assume that at least one binding will be changed */
3545 FLUSH_VERTICES(ctx
, 0);
3546 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3549 /* The ARB_multi_bind spec says:
3551 * "If <buffers> is NULL, all bindings from <first> through
3552 * <first>+<count>-1 are reset to their unbound (zero) state.
3553 * In this case, the offsets and sizes associated with the
3554 * binding points are set to default values, ignoring
3555 * <offsets> and <sizes>."
3557 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3561 /* Note that the error semantics for multi-bind commands differ from
3562 * those of other GL commands.
3564 * The Issues section in the ARB_multi_bind spec says:
3566 * "(11) Typically, OpenGL specifies that if an error is generated by a
3567 * command, that command has no effect. This is somewhat
3568 * unfortunate for multi-bind commands, because it would require a
3569 * first pass to scan the entire list of bound objects for errors
3570 * and then a second pass to actually perform the bindings.
3571 * Should we have different error semantics?
3573 * RESOLVED: Yes. In this specification, when the parameters for
3574 * one of the <count> binding points are invalid, that binding point
3575 * is not updated and an error will be generated. However, other
3576 * binding points in the same command will be updated if their
3577 * parameters are valid and no other error occurs."
3580 _mesa_begin_bufferobj_lookups(ctx
);
3582 for (i
= 0; i
< count
; i
++) {
3583 const GLuint index
= first
+ i
;
3584 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3585 struct gl_buffer_object
*bufObj
;
3587 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3590 /* The ARB_multi_bind spec says:
3592 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3593 * pair of values in <offsets> and <sizes> does not respectively
3594 * satisfy the constraints described for those parameters for the
3595 * specified target, as described in section 6.7.1 (per binding)."
3597 * Section 6.7.1 refers to table 6.5, which says:
3599 * "┌───────────────────────────────────────────────────────────────┐
3600 * │ Transform feedback array bindings (see sec. 13.2.2) │
3601 * ├───────────────────────┬───────────────────────────────────────┤
3603 * │ offset restriction │ multiple of 4 │
3605 * │ size restriction │ multiple of 4 │
3606 * └───────────────────────┴───────────────────────────────────────┘"
3608 if (offsets
[i
] & 0x3) {
3609 _mesa_error(ctx
, GL_INVALID_VALUE
,
3610 "glBindBuffersRange(offsets[%u]=%" PRId64
3611 " is misaligned; it must be a multiple of 4 when "
3612 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3613 i
, (int64_t) offsets
[i
]);
3617 if (sizes
[i
] & 0x3) {
3618 _mesa_error(ctx
, GL_INVALID_VALUE
,
3619 "glBindBuffersRange(sizes[%u]=%" PRId64
3620 " is misaligned; it must be a multiple of 4 when "
3621 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3622 i
, (int64_t) sizes
[i
]);
3626 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3627 bufObj
= boundBufObj
;
3629 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3630 "glBindBuffersRange");
3633 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3634 offsets
[i
], sizes
[i
]);
3637 _mesa_end_bufferobj_lookups(ctx
);
3641 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3642 GLuint first
, GLsizei count
,
3645 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3646 _mesa_error(ctx
, GL_INVALID_ENUM
,
3647 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3651 /* The ARB_multi_bind_spec says:
3653 * "An INVALID_OPERATION error is generated if <first> + <count> is
3654 * greater than the number of target-specific indexed binding points,
3655 * as described in section 6.7.1."
3657 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3658 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3659 "%s(first=%u + count=%d > the value of "
3660 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3661 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3669 * Unbind all atomic counter buffers in the range
3670 * <first> through <first>+<count>-1
3673 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3675 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3678 for (i
= 0; i
< count
; i
++)
3679 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3684 bind_atomic_buffers_base(struct gl_context
*ctx
,
3687 const GLuint
*buffers
)
3691 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
3692 "glBindBuffersBase"))
3695 /* Assume that at least one binding will be changed */
3696 FLUSH_VERTICES(ctx
, 0);
3697 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3700 /* The ARB_multi_bind spec says:
3702 * "If <buffers> is NULL, all bindings from <first> through
3703 * <first>+<count>-1 are reset to their unbound (zero) state."
3705 unbind_atomic_buffers(ctx
, first
, count
);
3709 /* Note that the error semantics for multi-bind commands differ from
3710 * those of other GL commands.
3712 * The Issues section in the ARB_multi_bind spec says:
3714 * "(11) Typically, OpenGL specifies that if an error is generated by a
3715 * command, that command has no effect. This is somewhat
3716 * unfortunate for multi-bind commands, because it would require a
3717 * first pass to scan the entire list of bound objects for errors
3718 * and then a second pass to actually perform the bindings.
3719 * Should we have different error semantics?
3721 * RESOLVED: Yes. In this specification, when the parameters for
3722 * one of the <count> binding points are invalid, that binding point
3723 * is not updated and an error will be generated. However, other
3724 * binding points in the same command will be updated if their
3725 * parameters are valid and no other error occurs."
3728 _mesa_begin_bufferobj_lookups(ctx
);
3730 for (i
= 0; i
< count
; i
++) {
3731 struct gl_atomic_buffer_binding
*binding
=
3732 &ctx
->AtomicBufferBindings
[first
+ i
];
3733 struct gl_buffer_object
*bufObj
;
3735 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3736 bufObj
= binding
->BufferObject
;
3738 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3739 "glBindBuffersBase");
3742 set_atomic_buffer_binding(ctx
, binding
, bufObj
, 0, 0);
3745 _mesa_end_bufferobj_lookups(ctx
);
3749 bind_atomic_buffers_range(struct gl_context
*ctx
,
3752 const GLuint
*buffers
,
3753 const GLintptr
*offsets
,
3754 const GLsizeiptr
*sizes
)
3758 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
3759 "glBindBuffersRange"))
3762 /* Assume that at least one binding will be changed */
3763 FLUSH_VERTICES(ctx
, 0);
3764 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3767 /* The ARB_multi_bind spec says:
3769 * "If <buffers> is NULL, all bindings from <first> through
3770 * <first>+<count>-1 are reset to their unbound (zero) state.
3771 * In this case, the offsets and sizes associated with the
3772 * binding points are set to default values, ignoring
3773 * <offsets> and <sizes>."
3775 unbind_atomic_buffers(ctx
, first
, count
);
3779 /* Note that the error semantics for multi-bind commands differ from
3780 * those of other GL commands.
3782 * The Issues section in the ARB_multi_bind spec says:
3784 * "(11) Typically, OpenGL specifies that if an error is generated by a
3785 * command, that command has no effect. This is somewhat
3786 * unfortunate for multi-bind commands, because it would require a
3787 * first pass to scan the entire list of bound objects for errors
3788 * and then a second pass to actually perform the bindings.
3789 * Should we have different error semantics?
3791 * RESOLVED: Yes. In this specification, when the parameters for
3792 * one of the <count> binding points are invalid, that binding point
3793 * is not updated and an error will be generated. However, other
3794 * binding points in the same command will be updated if their
3795 * parameters are valid and no other error occurs."
3798 _mesa_begin_bufferobj_lookups(ctx
);
3800 for (i
= 0; i
< count
; i
++) {
3801 struct gl_atomic_buffer_binding
*binding
=
3802 &ctx
->AtomicBufferBindings
[first
+ i
];
3803 struct gl_buffer_object
*bufObj
;
3805 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3808 /* The ARB_multi_bind spec says:
3810 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3811 * pair of values in <offsets> and <sizes> does not respectively
3812 * satisfy the constraints described for those parameters for the
3813 * specified target, as described in section 6.7.1 (per binding)."
3815 * Section 6.7.1 refers to table 6.5, which says:
3817 * "┌───────────────────────────────────────────────────────────────┐
3818 * │ Atomic counter array bindings (see sec. 7.7.2) │
3819 * ├───────────────────────┬───────────────────────────────────────┤
3821 * │ offset restriction │ multiple of 4 │
3823 * │ size restriction │ none │
3824 * └───────────────────────┴───────────────────────────────────────┘"
3826 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3827 _mesa_error(ctx
, GL_INVALID_VALUE
,
3828 "glBindBuffersRange(offsets[%u]=%" PRId64
3829 " is misaligned; it must be a multiple of %d when "
3830 "target=GL_ATOMIC_COUNTER_BUFFER)",
3831 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3835 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3836 bufObj
= binding
->BufferObject
;
3838 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3839 "glBindBuffersRange");
3842 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offsets
[i
], sizes
[i
]);
3845 _mesa_end_bufferobj_lookups(ctx
);
3849 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3850 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3852 GET_CURRENT_CONTEXT(ctx
);
3853 struct gl_buffer_object
*bufObj
;
3856 bufObj
= ctx
->Shared
->NullBufferObj
;
3858 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3860 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3861 &bufObj
, "glBindBufferRange"))
3865 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3866 "glBindBufferRange(invalid buffer=%u)", buffer
);
3872 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3879 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3880 _mesa_bind_buffer_range_transform_feedback(ctx
,
3881 ctx
->TransformFeedback
.CurrentObject
,
3882 index
, bufObj
, offset
, size
,
3885 case GL_UNIFORM_BUFFER
:
3886 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3888 case GL_SHADER_STORAGE_BUFFER
:
3889 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3891 case GL_ATOMIC_COUNTER_BUFFER
:
3892 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3893 "glBindBufferRange");
3896 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3902 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3904 GET_CURRENT_CONTEXT(ctx
);
3905 struct gl_buffer_object
*bufObj
;
3908 bufObj
= ctx
->Shared
->NullBufferObj
;
3910 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3912 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3913 &bufObj
, "glBindBufferBase"))
3917 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3918 "glBindBufferBase(invalid buffer=%u)", buffer
);
3922 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3923 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3925 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3926 * zero and size equal to the size of buffer."
3928 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3930 * "If the parameter (starting offset or size) was not specified when the
3931 * buffer object was bound, zero is returned."
3933 * What happens if the size of the buffer changes? Does the size of the
3934 * buffer at the moment glBindBufferBase was called still play a role, like
3935 * the first quote would imply, or is the size meaningless in the
3936 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3937 * core spec page 45 says:
3939 * "It is equivalent to calling BindBufferRange with offset zero, while
3940 * size is determined by the size of the bound buffer at the time the
3943 * My interpretation is that the GL 4.1 spec was a clarification of the
3944 * behavior, not a change. In particular, this choice will only make
3945 * rendering work in cases where it would have had undefined results.
3949 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3950 _mesa_bind_buffer_base_transform_feedback(ctx
,
3951 ctx
->TransformFeedback
.CurrentObject
,
3952 index
, bufObj
, false);
3954 case GL_UNIFORM_BUFFER
:
3955 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
3957 case GL_SHADER_STORAGE_BUFFER
:
3958 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
3960 case GL_ATOMIC_COUNTER_BUFFER
:
3961 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
3962 "glBindBufferBase");
3965 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
3971 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
3972 const GLuint
*buffers
,
3973 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3975 GET_CURRENT_CONTEXT(ctx
);
3978 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3979 bind_xfb_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
3981 case GL_UNIFORM_BUFFER
:
3982 bind_uniform_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
3984 case GL_SHADER_STORAGE_BUFFER
:
3985 bind_shader_storage_buffers_range(ctx
, first
, count
, buffers
, offsets
,
3988 case GL_ATOMIC_COUNTER_BUFFER
:
3989 bind_atomic_buffers_range(ctx
, first
, count
, buffers
,
3993 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
3994 _mesa_enum_to_string(target
));
4000 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4001 const GLuint
*buffers
)
4003 GET_CURRENT_CONTEXT(ctx
);
4006 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4007 bind_xfb_buffers_base(ctx
, first
, count
, buffers
);
4009 case GL_UNIFORM_BUFFER
:
4010 bind_uniform_buffers_base(ctx
, first
, count
, buffers
);
4012 case GL_SHADER_STORAGE_BUFFER
:
4013 bind_shader_storage_buffers_base(ctx
, first
, count
, buffers
);
4015 case GL_ATOMIC_COUNTER_BUFFER
:
4016 bind_atomic_buffers_base(ctx
, first
, count
, buffers
);
4019 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4020 _mesa_enum_to_string(target
));
4026 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4029 GET_CURRENT_CONTEXT(ctx
);
4030 struct gl_buffer_object
*bufObj
;
4031 const GLintptr end
= offset
+ length
;
4033 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4035 _mesa_error(ctx
, GL_INVALID_VALUE
,
4036 "glInvalidateBufferSubData(name = 0x%x) invalid object",
4041 /* The GL_ARB_invalidate_subdata spec says:
4043 * "An INVALID_VALUE error is generated if <offset> or <length> is
4044 * negative, or if <offset> + <length> is greater than the value of
4047 if (end
< 0 || end
> bufObj
->Size
) {
4048 _mesa_error(ctx
, GL_INVALID_VALUE
,
4049 "glInvalidateBufferSubData(invalid offset or length)");
4053 /* The OpenGL 4.4 (Core Profile) spec says:
4055 * "An INVALID_OPERATION error is generated if buffer is currently
4056 * mapped by MapBuffer or if the invalidate range intersects the range
4057 * currently mapped by MapBufferRange, unless it was mapped
4058 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4060 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4061 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4062 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4063 "glInvalidateBufferSubData(intersection with mapped "
4068 /* We don't actually do anything for this yet. Just return after
4069 * validating the parameters and generating the required errors.
4075 _mesa_InvalidateBufferData(GLuint buffer
)
4077 GET_CURRENT_CONTEXT(ctx
);
4078 struct gl_buffer_object
*bufObj
;
4080 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4082 _mesa_error(ctx
, GL_INVALID_VALUE
,
4083 "glInvalidateBufferData(name = 0x%x) invalid object",
4088 /* The OpenGL 4.4 (Core Profile) spec says:
4090 * "An INVALID_OPERATION error is generated if buffer is currently
4091 * mapped by MapBuffer or if the invalidate range intersects the range
4092 * currently mapped by MapBufferRange, unless it was mapped
4093 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4095 if (_mesa_check_disallowed_mapping(bufObj
)) {
4096 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4097 "glInvalidateBufferData(intersection with mapped "
4102 /* We don't actually do anything for this yet. Just return after
4103 * validating the parameters and generating the required errors.