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 */
41 #include "bufferobj.h"
46 #include "glformats.h"
48 #include "transformfeedback.h"
54 /*#define BOUNDS_CHECK*/
58 * Used as a placeholder for buffer objects between glGenBuffers() and
59 * glBindBuffer() so that glIsBuffer() can work correctly.
61 static struct gl_buffer_object DummyBufferObject
;
65 * Return pointer to address of a buffer object target.
66 * \param ctx the GL context
67 * \param target the buffer object target to be retrieved.
68 * \return pointer to pointer to the buffer object bound to \c target in the
69 * specified context or \c NULL if \c target is invalid.
71 static inline struct gl_buffer_object
**
72 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
74 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
76 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
77 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
81 case GL_ARRAY_BUFFER_ARB
:
82 return &ctx
->Array
.ArrayBufferObj
;
83 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
84 return &ctx
->Array
.VAO
->IndexBufferObj
;
85 case GL_PIXEL_PACK_BUFFER_EXT
:
86 return &ctx
->Pack
.BufferObj
;
87 case GL_PIXEL_UNPACK_BUFFER_EXT
:
88 return &ctx
->Unpack
.BufferObj
;
89 case GL_COPY_READ_BUFFER
:
90 return &ctx
->CopyReadBuffer
;
91 case GL_COPY_WRITE_BUFFER
:
92 return &ctx
->CopyWriteBuffer
;
93 case GL_DRAW_INDIRECT_BUFFER
:
94 if ((ctx
->API
== API_OPENGL_CORE
&&
95 ctx
->Extensions
.ARB_draw_indirect
) ||
96 _mesa_is_gles31(ctx
)) {
97 return &ctx
->DrawIndirectBuffer
;
100 case GL_TRANSFORM_FEEDBACK_BUFFER
:
101 if (ctx
->Extensions
.EXT_transform_feedback
) {
102 return &ctx
->TransformFeedback
.CurrentBuffer
;
105 case GL_TEXTURE_BUFFER
:
106 if (ctx
->API
== API_OPENGL_CORE
&&
107 ctx
->Extensions
.ARB_texture_buffer_object
) {
108 return &ctx
->Texture
.BufferObject
;
111 case GL_UNIFORM_BUFFER
:
112 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
113 return &ctx
->UniformBuffer
;
116 case GL_SHADER_STORAGE_BUFFER
:
117 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
118 return &ctx
->ShaderStorageBuffer
;
121 case GL_ATOMIC_COUNTER_BUFFER
:
122 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
123 return &ctx
->AtomicBuffer
;
126 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
127 if (ctx
->Extensions
.AMD_pinned_memory
) {
128 return &ctx
->ExternalVirtualMemoryBuffer
;
139 * Get the buffer object bound to the specified target in a GL context.
140 * \param ctx the GL context
141 * \param target the buffer object target to be retrieved.
142 * \param error the GL error to record if target is illegal.
143 * \return pointer to the buffer object bound to \c target in the
144 * specified context or \c NULL if \c target is invalid.
146 static inline struct gl_buffer_object
*
147 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
150 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
153 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
157 if (!_mesa_is_bufferobj(*bufObj
)) {
158 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
167 * Convert a GLbitfield describing the mapped buffer access flags
168 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
171 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
173 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
174 if ((access
& rwFlags
) == rwFlags
)
175 return GL_READ_WRITE
;
176 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
178 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
179 return GL_WRITE_ONLY
;
181 /* Otherwise, AccessFlags is zero (the default state).
183 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
185 * Name Type Initial Value Legal Values
187 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
190 * However, table 6.8 in the GL_OES_mapbuffer extension says:
192 * Get Value Type Get Command Value Description
193 * --------- ---- ----------- ----- -----------
194 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
196 * The difference is because GL_OES_mapbuffer only supports mapping buffers
201 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
206 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
208 * The regions do not overlap if and only if the end of the given
209 * region is before the mapped region or the start of the given region
210 * is after the mapped region.
212 * \param obj Buffer object target on which to operate.
213 * \param offset Offset of the first byte of the subdata range.
214 * \param size Size, in bytes, of the subdata range.
215 * \return true if ranges overlap, false otherwise
219 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
220 GLintptr offset
, GLsizeiptr size
)
222 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
223 const GLintptr end
= offset
+ size
;
224 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
225 obj
->Mappings
[MAP_USER
].Length
;
227 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
236 * Tests the subdata range parameters and sets the GL error code for
237 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
238 * \c glClearBufferSubData.
240 * \param ctx GL context.
241 * \param bufObj The buffer object.
242 * \param offset Offset of the first byte of the subdata range.
243 * \param size Size, in bytes, of the subdata range.
244 * \param mappedRange If true, checks if an overlapping range is mapped.
245 * If false, checks if buffer is mapped.
246 * \param caller Name of calling function for recording errors.
247 * \return false if error, true otherwise
249 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
252 buffer_object_subdata_range_good(struct gl_context
*ctx
,
253 struct gl_buffer_object
*bufObj
,
254 GLintptr offset
, GLsizeiptr size
,
255 bool mappedRange
, const char *caller
)
258 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
263 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
267 if (offset
+ size
> bufObj
->Size
) {
268 _mesa_error(ctx
, GL_INVALID_VALUE
,
269 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
270 (unsigned long) offset
,
271 (unsigned long) size
,
272 (unsigned long) bufObj
->Size
);
276 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
280 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
281 _mesa_error(ctx
, GL_INVALID_OPERATION
,
282 "%s(range is mapped without persistent bit)",
288 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
289 _mesa_error(ctx
, GL_INVALID_OPERATION
,
290 "%s(buffer is mapped without persistent bit)",
301 * Test the format and type parameters and set the GL error code for
302 * \c glClearBufferData and \c glClearBufferSubData.
304 * \param ctx GL context.
305 * \param internalformat Format to which the data is to be converted.
306 * \param format Format of the supplied data.
307 * \param type Type of the supplied data.
308 * \param caller Name of calling function for recording errors.
309 * \return If internalformat, format and type are legal the mesa_format
310 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
312 * \sa glClearBufferData and glClearBufferSubData
315 validate_clear_buffer_format(struct gl_context
*ctx
,
316 GLenum internalformat
,
317 GLenum format
, GLenum type
,
320 mesa_format mesaFormat
;
321 GLenum errorFormatType
;
323 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
324 if (mesaFormat
== MESA_FORMAT_NONE
) {
325 _mesa_error(ctx
, GL_INVALID_ENUM
,
326 "%s(invalid internalformat)", caller
);
327 return MESA_FORMAT_NONE
;
330 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
331 * EXT_texture_integer there is no conversion between integer and
332 * non-integer formats
334 if (_mesa_is_enum_format_signed_int(format
) !=
335 _mesa_is_format_integer_color(mesaFormat
)) {
336 _mesa_error(ctx
, GL_INVALID_OPERATION
,
337 "%s(integer vs non-integer)", caller
);
338 return MESA_FORMAT_NONE
;
341 if (!_mesa_is_color_format(format
)) {
342 _mesa_error(ctx
, GL_INVALID_ENUM
,
343 "%s(format is not a color format)", caller
);
344 return MESA_FORMAT_NONE
;
347 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
348 if (errorFormatType
!= GL_NO_ERROR
) {
349 _mesa_error(ctx
, GL_INVALID_ENUM
,
350 "%s(invalid format or type)", caller
);
351 return MESA_FORMAT_NONE
;
359 * Convert user-specified clear value to the specified internal format.
361 * \param ctx GL context.
362 * \param internalformat Format to which the data is converted.
363 * \param clearValue Points to the converted clear value.
364 * \param format Format of the supplied data.
365 * \param type Type of the supplied data.
366 * \param data Data which is to be converted to internalformat.
367 * \param caller Name of calling function for recording errors.
368 * \return true if data could be converted, false otherwise.
370 * \sa glClearBufferData, glClearBufferSubData
373 convert_clear_buffer_data(struct gl_context
*ctx
,
374 mesa_format internalformat
,
375 GLubyte
*clearValue
, GLenum format
, GLenum type
,
376 const GLvoid
*data
, const char *caller
)
378 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
380 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
381 0, &clearValue
, 1, 1, 1,
382 format
, type
, data
, &ctx
->Unpack
)) {
386 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
393 * Allocate and initialize a new buffer object.
395 * Default callback for the \c dd_function_table::NewBufferObject() hook.
397 static struct gl_buffer_object
*
398 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
400 struct gl_buffer_object
*obj
;
404 obj
= MALLOC_STRUCT(gl_buffer_object
);
405 _mesa_initialize_buffer_object(ctx
, obj
, name
);
411 * Delete a buffer object.
413 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
416 _mesa_delete_buffer_object(struct gl_context
*ctx
,
417 struct gl_buffer_object
*bufObj
)
421 _mesa_align_free(bufObj
->Data
);
423 /* assign strange values here to help w/ debugging */
424 bufObj
->RefCount
= -1000;
427 mtx_destroy(&bufObj
->Mutex
);
435 * Set ptr to bufObj w/ reference counting.
436 * This is normally only called from the _mesa_reference_buffer_object() macro
437 * when there's a real pointer change.
440 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
441 struct gl_buffer_object
**ptr
,
442 struct gl_buffer_object
*bufObj
)
445 /* Unreference the old buffer */
446 GLboolean deleteFlag
= GL_FALSE
;
447 struct gl_buffer_object
*oldObj
= *ptr
;
449 mtx_lock(&oldObj
->Mutex
);
450 assert(oldObj
->RefCount
> 0);
453 printf("BufferObj %p %d DECR to %d\n",
454 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
456 deleteFlag
= (oldObj
->RefCount
== 0);
457 mtx_unlock(&oldObj
->Mutex
);
461 /* some sanity checking: don't delete a buffer still in use */
463 /* unfortunately, these tests are invalid during context tear-down */
464 assert(ctx
->Array
.ArrayBufferObj
!= bufObj
);
465 assert(ctx
->Array
.VAO
->IndexBufferObj
!= bufObj
);
466 assert(ctx
->Array
.VAO
->Vertex
.BufferObj
!= bufObj
);
469 assert(ctx
->Driver
.DeleteBuffer
);
470 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
478 /* reference new buffer */
479 mtx_lock(&bufObj
->Mutex
);
480 if (bufObj
->RefCount
== 0) {
481 /* this buffer's being deleted (look just above) */
482 /* Not sure this can every really happen. Warn if it does. */
483 _mesa_problem(NULL
, "referencing deleted buffer object");
489 printf("BufferObj %p %d INCR to %d\n",
490 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
494 mtx_unlock(&bufObj
->Mutex
);
500 * Initialize a buffer object to default values.
503 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
504 struct gl_buffer_object
*obj
,
507 memset(obj
, 0, sizeof(struct gl_buffer_object
));
508 mtx_init(&obj
->Mutex
, mtx_plain
);
511 obj
->Usage
= GL_STATIC_DRAW_ARB
;
517 * Callback called from _mesa_HashWalk()
520 count_buffer_size(GLuint key
, void *data
, void *userData
)
522 const struct gl_buffer_object
*bufObj
=
523 (const struct gl_buffer_object
*) data
;
524 GLuint
*total
= (GLuint
*) userData
;
527 *total
= *total
+ bufObj
->Size
;
532 * Compute total size (in bytes) of all buffer objects for the given context.
533 * For debugging purposes.
536 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
540 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
547 * Allocate space for and store data in a buffer object. Any data that was
548 * previously stored in the buffer object is lost. If \c data is \c NULL,
549 * memory will be allocated, but no copy will occur.
551 * This is the default callback for \c dd_function_table::BufferData()
552 * Note that all GL error checking will have been done already.
554 * \param ctx GL context.
555 * \param target Buffer object target on which to operate.
556 * \param size Size, in bytes, of the new data store.
557 * \param data Pointer to the data to store in the buffer object. This
558 * pointer may be \c NULL.
559 * \param usage Hints about how the data will be used.
560 * \param bufObj Object to be used.
562 * \return GL_TRUE for success, GL_FALSE for failure
563 * \sa glBufferDataARB, dd_function_table::BufferData.
566 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptr size
,
567 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
568 struct gl_buffer_object
*bufObj
)
574 _mesa_align_free( bufObj
->Data
);
576 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
578 bufObj
->Data
= (GLubyte
*) new_data
;
580 bufObj
->Usage
= usage
;
581 bufObj
->StorageFlags
= storageFlags
;
584 memcpy( bufObj
->Data
, data
, size
);
596 * Replace data in a subrange of buffer object. If the data range
597 * specified by \c size + \c offset extends beyond the end of the buffer or
598 * if \c data is \c NULL, no copy is performed.
600 * This is the default callback for \c dd_function_table::BufferSubData()
601 * Note that all GL error checking will have been done already.
603 * \param ctx GL context.
604 * \param offset Offset of the first byte to be modified.
605 * \param size Size, in bytes, of the data range.
606 * \param data Pointer to the data to store in the buffer object.
607 * \param bufObj Object to be used.
609 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
612 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptr offset
,
613 GLsizeiptr size
, const GLvoid
*data
,
614 struct gl_buffer_object
*bufObj
)
618 /* this should have been caught in _mesa_BufferSubData() */
619 assert(size
+ offset
<= bufObj
->Size
);
622 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
628 * Retrieve data from a subrange of buffer object. If the data range
629 * specified by \c size + \c offset extends beyond the end of the buffer or
630 * if \c data is \c NULL, no copy is performed.
632 * This is the default callback for \c dd_function_table::GetBufferSubData()
633 * Note that all GL error checking will have been done already.
635 * \param ctx GL context.
636 * \param target Buffer object target on which to operate.
637 * \param offset Offset of the first byte to be fetched.
638 * \param size Size, in bytes, of the data range.
639 * \param data Destination for data
640 * \param bufObj Object to be used.
642 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
645 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
646 GLsizeiptrARB size
, GLvoid
* data
,
647 struct gl_buffer_object
* bufObj
)
651 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
652 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
658 * Clear a subrange of the buffer object with copies of the supplied data.
659 * If data is NULL the buffer is filled with zeros.
661 * This is the default callback for \c dd_function_table::ClearBufferSubData()
662 * Note that all GL error checking will have been done already.
664 * \param ctx GL context.
665 * \param offset Offset of the first byte to be cleared.
666 * \param size Size, in bytes, of the to be cleared range.
667 * \param clearValue Source of the data.
668 * \param clearValueSize Size, in bytes, of the supplied data.
669 * \param bufObj Object to be cleared.
671 * \sa glClearBufferSubData, glClearBufferData and
672 * dd_function_table::ClearBufferSubData.
675 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
676 GLintptr offset
, GLsizeiptr size
,
677 const GLvoid
*clearValue
,
678 GLsizeiptr clearValueSize
,
679 struct gl_buffer_object
*bufObj
)
684 assert(ctx
->Driver
.MapBufferRange
);
685 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
687 GL_MAP_INVALIDATE_RANGE_BIT
,
688 bufObj
, MAP_INTERNAL
);
691 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
695 if (clearValue
== NULL
) {
696 /* Clear with zeros, per the spec */
697 memset(dest
, 0, size
);
698 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
702 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
703 memcpy(dest
, clearValue
, clearValueSize
);
704 dest
+= clearValueSize
;
707 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
712 * Default fallback for \c dd_function_table::MapBufferRange().
713 * Called via glMapBufferRange().
716 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
717 GLsizeiptr length
, GLbitfield access
,
718 struct gl_buffer_object
*bufObj
,
719 gl_map_buffer_index index
)
722 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
723 /* Just return a direct pointer to the data */
724 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
725 bufObj
->Mappings
[index
].Length
= length
;
726 bufObj
->Mappings
[index
].Offset
= offset
;
727 bufObj
->Mappings
[index
].AccessFlags
= access
;
728 return bufObj
->Mappings
[index
].Pointer
;
733 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
734 * Called via glFlushMappedBufferRange().
737 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
738 GLintptr offset
, GLsizeiptr length
,
739 struct gl_buffer_object
*obj
,
740 gl_map_buffer_index index
)
752 * Default callback for \c dd_function_table::UnmapBuffer().
754 * The input parameters will have been already tested for errors.
756 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
759 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
760 gl_map_buffer_index index
)
763 /* XXX we might assert here that bufObj->Pointer is non-null */
764 bufObj
->Mappings
[index
].Pointer
= NULL
;
765 bufObj
->Mappings
[index
].Length
= 0;
766 bufObj
->Mappings
[index
].Offset
= 0;
767 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
773 * Default fallback for \c dd_function_table::CopyBufferSubData().
774 * Called via glCopyBufferSubData().
777 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
778 struct gl_buffer_object
*src
,
779 struct gl_buffer_object
*dst
,
780 GLintptr readOffset
, GLintptr writeOffset
,
783 GLubyte
*srcPtr
, *dstPtr
;
786 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
788 GL_MAP_WRITE_BIT
, src
,
794 srcPtr
+= readOffset
;
795 dstPtr
+= writeOffset
;
797 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
798 GL_MAP_READ_BIT
, src
,
800 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
802 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
806 /* Note: the src and dst regions will never overlap. Trying to do so
807 * would generate GL_INVALID_VALUE earlier.
809 if (srcPtr
&& dstPtr
)
810 memcpy(dstPtr
, srcPtr
, size
);
812 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
814 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
820 * Initialize the state associated with buffer objects
823 _mesa_init_buffer_objects( struct gl_context
*ctx
)
827 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
828 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
829 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
831 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
832 ctx
->Shared
->NullBufferObj
);
834 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
835 ctx
->Shared
->NullBufferObj
);
836 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
837 ctx
->Shared
->NullBufferObj
);
839 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
840 ctx
->Shared
->NullBufferObj
);
842 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
843 ctx
->Shared
->NullBufferObj
);
845 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
846 ctx
->Shared
->NullBufferObj
);
848 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
849 ctx
->Shared
->NullBufferObj
);
851 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
852 _mesa_reference_buffer_object(ctx
,
853 &ctx
->UniformBufferBindings
[i
].BufferObject
,
854 ctx
->Shared
->NullBufferObj
);
855 ctx
->UniformBufferBindings
[i
].Offset
= -1;
856 ctx
->UniformBufferBindings
[i
].Size
= -1;
859 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
860 _mesa_reference_buffer_object(ctx
,
861 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
862 ctx
->Shared
->NullBufferObj
);
863 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
864 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
867 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
868 _mesa_reference_buffer_object(ctx
,
869 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
870 ctx
->Shared
->NullBufferObj
);
871 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
872 ctx
->AtomicBufferBindings
[i
].Size
= 0;
878 _mesa_free_buffer_objects( struct gl_context
*ctx
)
882 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
884 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
885 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
887 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
889 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
891 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
893 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
895 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
896 _mesa_reference_buffer_object(ctx
,
897 &ctx
->UniformBufferBindings
[i
].BufferObject
,
901 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
902 _mesa_reference_buffer_object(ctx
,
903 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
907 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
908 _mesa_reference_buffer_object(ctx
,
909 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
916 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
918 struct gl_buffer_object
**buf_handle
,
921 struct gl_buffer_object
*buf
= *buf_handle
;
923 if (!buf
&& ctx
->API
== API_OPENGL_CORE
) {
924 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
928 if (!buf
|| buf
== &DummyBufferObject
) {
929 /* If this is a new buffer object id, or one which was generated but
930 * never used before, allocate a buffer object now.
932 assert(ctx
->Driver
.NewBufferObject
);
933 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
935 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
938 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
946 * Bind the specified target to buffer for the specified context.
947 * Called by glBindBuffer() and other functions.
950 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
952 struct gl_buffer_object
*oldBufObj
;
953 struct gl_buffer_object
*newBufObj
= NULL
;
954 struct gl_buffer_object
**bindTarget
= NULL
;
956 bindTarget
= get_buffer_target(ctx
, target
);
958 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
962 /* Get pointer to old buffer object (to be unbound) */
963 oldBufObj
= *bindTarget
;
964 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
965 return; /* rebinding the same buffer object- no change */
968 * Get pointer to new buffer object (newBufObj)
971 /* The spec says there's not a buffer object named 0, but we use
972 * one internally because it simplifies things.
974 newBufObj
= ctx
->Shared
->NullBufferObj
;
977 /* non-default buffer object */
978 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
979 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
980 &newBufObj
, "glBindBuffer"))
984 /* bind new buffer */
985 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
990 * Update the default buffer objects in the given context to reference those
991 * specified in the shared state and release those referencing the old
995 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
997 /* Bind the NullBufferObj to remove references to those
998 * in the shared context hash table.
1000 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
1001 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
1002 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
1003 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1009 * Return the gl_buffer_object for the given ID.
1010 * Always return NULL for ID 0.
1012 struct gl_buffer_object
*
1013 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1018 return (struct gl_buffer_object
*)
1019 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1023 struct gl_buffer_object
*
1024 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1026 return (struct gl_buffer_object
*)
1027 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1031 * A convenience function for direct state access functions that throws
1032 * GL_INVALID_OPERATION if buffer is not the name of an existing
1035 struct gl_buffer_object
*
1036 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1039 struct gl_buffer_object
*bufObj
;
1041 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1042 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1043 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1044 "%s(non-existent buffer object %u)", caller
, buffer
);
1053 _mesa_begin_bufferobj_lookups(struct gl_context
*ctx
)
1055 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1060 _mesa_end_bufferobj_lookups(struct gl_context
*ctx
)
1062 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1067 * Look up a buffer object for a multi-bind function.
1069 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1070 * of generating an error if the buffer ID is not zero or the name
1071 * of an existing buffer object.
1073 * If the buffer ID refers to an existing buffer object, a pointer
1074 * to the buffer object is returned. If the ID is zero, a pointer
1075 * to the shared NullBufferObj is returned. If the ID is not zero
1076 * and does not refer to a valid buffer object, this function
1079 * This function assumes that the caller has already locked the
1080 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1082 struct gl_buffer_object
*
1083 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1084 const GLuint
*buffers
,
1085 GLuint index
, const char *caller
)
1087 struct gl_buffer_object
*bufObj
;
1089 if (buffers
[index
] != 0) {
1090 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1092 /* The multi-bind functions don't create the buffer objects
1093 when they don't exist. */
1094 if (bufObj
== &DummyBufferObject
)
1097 bufObj
= ctx
->Shared
->NullBufferObj
;
1100 /* The ARB_multi_bind spec says:
1102 * "An INVALID_OPERATION error is generated if any value
1103 * in <buffers> is not zero or the name of an existing
1104 * buffer object (per binding)."
1106 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1107 "%s(buffers[%u]=%u is not zero or the name "
1108 "of an existing buffer object)",
1109 caller
, index
, buffers
[index
]);
1117 * If *ptr points to obj, set ptr = the Null/default buffer object.
1118 * This is a helper for buffer object deletion.
1119 * The GL spec says that deleting a buffer object causes it to get
1120 * unbound from all arrays in the current context.
1123 unbind(struct gl_context
*ctx
,
1124 struct gl_buffer_object
**ptr
,
1125 struct gl_buffer_object
*obj
)
1128 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
1134 * Plug default/fallback buffer object functions into the device
1138 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1140 /* GL_ARB_vertex/pixel_buffer_object */
1141 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1142 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1143 driver
->BufferData
= buffer_data_fallback
;
1144 driver
->BufferSubData
= buffer_sub_data_fallback
;
1145 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1146 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1148 /* GL_ARB_clear_buffer_object */
1149 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1151 /* GL_ARB_map_buffer_range */
1152 driver
->MapBufferRange
= map_buffer_range_fallback
;
1153 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1155 /* GL_ARB_copy_buffer */
1156 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1161 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1162 struct gl_buffer_object
*bufObj
)
1166 for (i
= 0; i
< MAP_COUNT
; i
++) {
1167 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1168 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1169 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1170 bufObj
->Mappings
[i
].AccessFlags
= 0;
1176 /**********************************************************************/
1178 /**********************************************************************/
1181 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1183 GET_CURRENT_CONTEXT(ctx
);
1185 if (MESA_VERBOSE
& VERBOSE_API
)
1186 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1187 _mesa_enum_to_string(target
), buffer
);
1189 bind_buffer_object(ctx
, target
, buffer
);
1194 * Delete a set of buffer objects.
1196 * \param n Number of buffer objects to delete.
1197 * \param ids Array of \c n buffer object IDs.
1200 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1202 GET_CURRENT_CONTEXT(ctx
);
1204 FLUSH_VERTICES(ctx
, 0);
1207 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1211 mtx_lock(&ctx
->Shared
->Mutex
);
1213 for (i
= 0; i
< n
; i
++) {
1214 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
1216 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1219 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1221 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1223 /* unbind any vertex pointers bound to this buffer */
1224 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1225 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1228 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1229 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1231 if (vao
->IndexBufferObj
== bufObj
) {
1232 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1235 /* unbind ARB_draw_indirect binding point */
1236 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1237 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1240 /* unbind ARB_copy_buffer binding points */
1241 if (ctx
->CopyReadBuffer
== bufObj
) {
1242 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1244 if (ctx
->CopyWriteBuffer
== bufObj
) {
1245 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1248 /* unbind transform feedback binding points */
1249 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1250 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1252 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1253 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1254 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1258 /* unbind UBO binding points */
1259 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1260 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1261 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1265 if (ctx
->UniformBuffer
== bufObj
) {
1266 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1269 /* unbind SSBO binding points */
1270 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1271 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1272 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1276 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1277 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER
, 0);
1280 /* unbind Atomci Buffer binding points */
1281 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1282 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1283 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1287 if (ctx
->AtomicBuffer
== bufObj
) {
1288 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1291 /* unbind any pixel pack/unpack pointers bound to this buffer */
1292 if (ctx
->Pack
.BufferObj
== bufObj
) {
1293 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1295 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1296 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1299 if (ctx
->Texture
.BufferObject
== bufObj
) {
1300 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1303 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1304 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1307 /* The ID is immediately freed for re-use */
1308 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1309 /* Make sure we do not run into the classic ABA problem on bind.
1310 * We don't want to allow re-binding a buffer object that's been
1311 * "deleted" by glDeleteBuffers().
1313 * The explicit rebinding to the default object in the current context
1314 * prevents the above in the current context, but another context
1315 * sharing the same objects might suffer from this problem.
1316 * The alternative would be to do the hash lookup in any case on bind
1317 * which would introduce more runtime overhead than this.
1319 bufObj
->DeletePending
= GL_TRUE
;
1320 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1324 mtx_unlock(&ctx
->Shared
->Mutex
);
1329 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1330 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1334 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1336 GET_CURRENT_CONTEXT(ctx
);
1339 struct gl_buffer_object
*buf
;
1341 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1343 if (MESA_VERBOSE
& VERBOSE_API
)
1344 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1347 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1356 * This must be atomic (generation and allocation of buffer object IDs)
1358 mtx_lock(&ctx
->Shared
->Mutex
);
1360 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1362 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1363 * DummyBufferObject. Otherwise, create a new buffer object and insert
1366 for (i
= 0; i
< n
; i
++) {
1367 buffers
[i
] = first
+ i
;
1369 assert(ctx
->Driver
.NewBufferObject
);
1370 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1372 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1373 mtx_unlock(&ctx
->Shared
->Mutex
);
1378 buf
= &DummyBufferObject
;
1380 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1383 mtx_unlock(&ctx
->Shared
->Mutex
);
1387 * Generate a set of unique buffer object IDs and store them in \c buffers.
1389 * \param n Number of IDs to generate.
1390 * \param buffers Array of \c n locations to store the IDs.
1393 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1395 create_buffers(n
, buffers
, false);
1399 * Create a set of buffer objects and store their unique IDs in \c buffers.
1401 * \param n Number of IDs to generate.
1402 * \param buffers Array of \c n locations to store the IDs.
1405 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1407 create_buffers(n
, buffers
, true);
1412 * Determine if ID is the name of a buffer object.
1414 * \param id ID of the potential buffer object.
1415 * \return \c GL_TRUE if \c id is the name of a buffer object,
1416 * \c GL_FALSE otherwise.
1418 GLboolean GLAPIENTRY
1419 _mesa_IsBuffer(GLuint id
)
1421 struct gl_buffer_object
*bufObj
;
1422 GET_CURRENT_CONTEXT(ctx
);
1423 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1425 mtx_lock(&ctx
->Shared
->Mutex
);
1426 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1427 mtx_unlock(&ctx
->Shared
->Mutex
);
1429 return bufObj
&& bufObj
!= &DummyBufferObject
;
1434 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1435 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1436 GLbitfield flags
, const char *func
)
1439 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1443 if (flags
& ~(GL_MAP_READ_BIT
|
1445 GL_MAP_PERSISTENT_BIT
|
1446 GL_MAP_COHERENT_BIT
|
1447 GL_DYNAMIC_STORAGE_BIT
|
1448 GL_CLIENT_STORAGE_BIT
)) {
1449 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1453 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1454 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1455 _mesa_error(ctx
, GL_INVALID_VALUE
,
1456 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1460 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1461 _mesa_error(ctx
, GL_INVALID_VALUE
,
1462 "%s(COHERENT and flags!=PERSISTENT)", func
);
1466 if (bufObj
->Immutable
) {
1467 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1471 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1472 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1474 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1476 bufObj
->Written
= GL_TRUE
;
1477 bufObj
->Immutable
= GL_TRUE
;
1479 assert(ctx
->Driver
.BufferData
);
1480 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1482 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1483 /* Even though the interaction between AMD_pinned_memory and
1484 * glBufferStorage is not described in the spec, Graham Sellers
1485 * said that it should behave the same as glBufferData.
1487 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1490 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1496 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1499 GET_CURRENT_CONTEXT(ctx
);
1500 struct gl_buffer_object
*bufObj
;
1502 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1506 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1511 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1514 GET_CURRENT_CONTEXT(ctx
);
1515 struct gl_buffer_object
*bufObj
;
1517 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1522 * In direct state access, buffer objects have an unspecified target since
1523 * they are not required to be bound.
1525 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1526 "glNamedBufferStorage");
1531 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1532 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1533 GLenum usage
, const char *func
)
1537 if (MESA_VERBOSE
& VERBOSE_API
)
1538 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1540 _mesa_enum_to_string(target
),
1541 (long int) size
, data
,
1542 _mesa_enum_to_string(usage
));
1545 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1550 case GL_STREAM_DRAW_ARB
:
1551 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1554 case GL_STATIC_DRAW_ARB
:
1555 case GL_DYNAMIC_DRAW_ARB
:
1559 case GL_STREAM_READ_ARB
:
1560 case GL_STREAM_COPY_ARB
:
1561 case GL_STATIC_READ_ARB
:
1562 case GL_STATIC_COPY_ARB
:
1563 case GL_DYNAMIC_READ_ARB
:
1564 case GL_DYNAMIC_COPY_ARB
:
1565 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1569 valid_usage
= false;
1574 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1575 _mesa_enum_to_string(usage
));
1579 if (bufObj
->Immutable
) {
1580 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1584 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1585 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1587 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1589 bufObj
->Written
= GL_TRUE
;
1592 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1593 bufObj
->Name
, size
, data
, usage
);
1600 assert(ctx
->Driver
.BufferData
);
1601 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1604 GL_DYNAMIC_STORAGE_BIT
,
1606 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1607 /* From GL_AMD_pinned_memory:
1609 * INVALID_OPERATION is generated by BufferData if <target> is
1610 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1611 * mapped to the GPU address space.
1613 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1616 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1622 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1623 const GLvoid
*data
, GLenum usage
)
1625 GET_CURRENT_CONTEXT(ctx
);
1626 struct gl_buffer_object
*bufObj
;
1628 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1632 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1637 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1640 GET_CURRENT_CONTEXT(ctx
);
1641 struct gl_buffer_object
*bufObj
;
1643 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1647 /* In direct state access, buffer objects have an unspecified target since
1648 * they are not required to be bound.
1650 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1651 "glNamedBufferData");
1656 * Implementation for glBufferSubData and glNamedBufferSubData.
1658 * \param ctx GL context.
1659 * \param bufObj The buffer object.
1660 * \param offset Offset of the first byte of the subdata range.
1661 * \param size Size, in bytes, of the subdata range.
1662 * \param data The data store.
1663 * \param func Name of calling function for recording errors.
1667 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1668 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1671 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1673 /* error already recorded */
1677 if (bufObj
->Immutable
&&
1678 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1679 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1686 bufObj
->Written
= GL_TRUE
;
1688 assert(ctx
->Driver
.BufferSubData
);
1689 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1693 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1694 GLsizeiptr size
, const GLvoid
*data
)
1696 GET_CURRENT_CONTEXT(ctx
);
1697 struct gl_buffer_object
*bufObj
;
1699 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1703 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1707 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1708 GLsizeiptr size
, const GLvoid
*data
)
1710 GET_CURRENT_CONTEXT(ctx
);
1711 struct gl_buffer_object
*bufObj
;
1713 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1717 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1718 "glNamedBufferSubData");
1723 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1724 GLsizeiptr size
, GLvoid
*data
)
1726 GET_CURRENT_CONTEXT(ctx
);
1727 struct gl_buffer_object
*bufObj
;
1729 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1730 GL_INVALID_OPERATION
);
1734 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1735 "glGetBufferSubData")) {
1739 assert(ctx
->Driver
.GetBufferSubData
);
1740 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1744 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1745 GLsizeiptr size
, GLvoid
*data
)
1747 GET_CURRENT_CONTEXT(ctx
);
1748 struct gl_buffer_object
*bufObj
;
1750 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1751 "glGetNamedBufferSubData");
1755 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1756 "glGetNamedBufferSubData")) {
1760 assert(ctx
->Driver
.GetBufferSubData
);
1761 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1766 * \param subdata true if caller is *SubData, false if *Data
1769 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1770 struct gl_buffer_object
*bufObj
,
1771 GLenum internalformat
,
1772 GLintptr offset
, GLsizeiptr size
,
1773 GLenum format
, GLenum type
,
1775 const char *func
, bool subdata
)
1777 mesa_format mesaFormat
;
1778 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1779 GLsizeiptr clearValueSize
;
1781 /* This checks for disallowed mappings. */
1782 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1787 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1788 format
, type
, func
);
1790 if (mesaFormat
== MESA_FORMAT_NONE
) {
1794 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1795 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1796 _mesa_error(ctx
, GL_INVALID_VALUE
,
1797 "%s(offset or size is not a multiple of "
1798 "internalformat size)", func
);
1803 /* clear to zeros, per the spec */
1805 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1806 NULL
, clearValueSize
, bufObj
);
1811 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1812 format
, type
, data
, func
)) {
1817 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1818 clearValue
, clearValueSize
, bufObj
);
1823 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1824 GLenum type
, const GLvoid
*data
)
1826 GET_CURRENT_CONTEXT(ctx
);
1827 struct gl_buffer_object
*bufObj
;
1829 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1833 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1835 "glClearBufferData", false);
1839 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1840 GLenum format
, GLenum type
, const GLvoid
*data
)
1842 GET_CURRENT_CONTEXT(ctx
);
1843 struct gl_buffer_object
*bufObj
;
1845 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1849 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1851 "glClearNamedBufferData", false);
1856 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1857 GLintptr offset
, GLsizeiptr size
,
1858 GLenum format
, GLenum type
,
1861 GET_CURRENT_CONTEXT(ctx
);
1862 struct gl_buffer_object
*bufObj
;
1864 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1868 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1870 "glClearBufferSubData", true);
1874 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1875 GLintptr offset
, GLsizeiptr size
,
1876 GLenum format
, GLenum type
,
1879 GET_CURRENT_CONTEXT(ctx
);
1880 struct gl_buffer_object
*bufObj
;
1882 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1883 "glClearNamedBufferSubData");
1887 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1889 "glClearNamedBufferSubData", true);
1894 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1897 GLboolean status
= GL_TRUE
;
1898 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1900 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
1901 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1902 "%s(buffer is not mapped)", func
);
1907 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1908 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1910 /* check that last 100 bytes are still = magic value */
1911 for (i
= 0; i
< 100; i
++) {
1912 GLuint pos
= bufObj
->Size
- i
- 1;
1913 if (buf
[pos
] != 123) {
1914 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1915 " at position %d (value = %u)\n",
1923 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1924 GLuint i
, unchanged
= 0;
1925 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1927 /* check which bytes changed */
1928 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1929 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1936 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1937 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1942 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
1943 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
1944 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
1945 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
1946 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
1951 GLboolean GLAPIENTRY
1952 _mesa_UnmapBuffer(GLenum target
)
1954 GET_CURRENT_CONTEXT(ctx
);
1955 struct gl_buffer_object
*bufObj
;
1957 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
1961 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
1964 GLboolean GLAPIENTRY
1965 _mesa_UnmapNamedBuffer(GLuint buffer
)
1967 GET_CURRENT_CONTEXT(ctx
);
1968 struct gl_buffer_object
*bufObj
;
1970 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
1974 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
1979 get_buffer_parameter(struct gl_context
*ctx
,
1980 struct gl_buffer_object
*bufObj
, GLenum pname
,
1981 GLint64
*params
, const char *func
)
1984 case GL_BUFFER_SIZE_ARB
:
1985 *params
= bufObj
->Size
;
1987 case GL_BUFFER_USAGE_ARB
:
1988 *params
= bufObj
->Usage
;
1990 case GL_BUFFER_ACCESS_ARB
:
1991 *params
= simplified_access_mode(ctx
,
1992 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
1994 case GL_BUFFER_MAPPED_ARB
:
1995 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
1997 case GL_BUFFER_ACCESS_FLAGS
:
1998 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2000 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2002 case GL_BUFFER_MAP_OFFSET
:
2003 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2005 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2007 case GL_BUFFER_MAP_LENGTH
:
2008 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2010 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2012 case GL_BUFFER_IMMUTABLE_STORAGE
:
2013 if (!ctx
->Extensions
.ARB_buffer_storage
)
2015 *params
= bufObj
->Immutable
;
2017 case GL_BUFFER_STORAGE_FLAGS
:
2018 if (!ctx
->Extensions
.ARB_buffer_storage
)
2020 *params
= bufObj
->StorageFlags
;
2029 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2030 _mesa_enum_to_string(pname
));
2035 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2037 GET_CURRENT_CONTEXT(ctx
);
2038 struct gl_buffer_object
*bufObj
;
2041 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2042 GL_INVALID_OPERATION
);
2046 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2047 "glGetBufferParameteriv"))
2048 return; /* Error already recorded. */
2050 *params
= (GLint
) parameter
;
2054 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2056 GET_CURRENT_CONTEXT(ctx
);
2057 struct gl_buffer_object
*bufObj
;
2060 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2061 GL_INVALID_OPERATION
);
2065 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2066 "glGetBufferParameteri64v"))
2067 return; /* Error already recorded. */
2069 *params
= parameter
;
2073 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2075 GET_CURRENT_CONTEXT(ctx
);
2076 struct gl_buffer_object
*bufObj
;
2079 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2080 "glGetNamedBufferParameteriv");
2084 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2085 "glGetNamedBufferParameteriv"))
2086 return; /* Error already recorded. */
2088 *params
= (GLint
) parameter
;
2092 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2095 GET_CURRENT_CONTEXT(ctx
);
2096 struct gl_buffer_object
*bufObj
;
2099 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2100 "glGetNamedBufferParameteri64v");
2104 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2105 "glGetNamedBufferParameteri64v"))
2106 return; /* Error already recorded. */
2108 *params
= parameter
;
2113 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2115 GET_CURRENT_CONTEXT(ctx
);
2116 struct gl_buffer_object
*bufObj
;
2118 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2119 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2120 "GL_BUFFER_MAP_POINTER)");
2124 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2125 GL_INVALID_OPERATION
);
2129 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2133 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2135 GET_CURRENT_CONTEXT(ctx
);
2136 struct gl_buffer_object
*bufObj
;
2138 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2139 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2140 "GL_BUFFER_MAP_POINTER)");
2144 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2145 "glGetNamedBufferPointerv");
2149 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2154 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2155 struct gl_buffer_object
*src
,
2156 struct gl_buffer_object
*dst
,
2157 GLintptr readOffset
, GLintptr writeOffset
,
2158 GLsizeiptr size
, const char *func
)
2160 if (_mesa_check_disallowed_mapping(src
)) {
2161 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2162 "%s(readBuffer is mapped)", func
);
2166 if (_mesa_check_disallowed_mapping(dst
)) {
2167 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2168 "%s(writeBuffer is mapped)", func
);
2172 if (readOffset
< 0) {
2173 _mesa_error(ctx
, GL_INVALID_VALUE
,
2174 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2178 if (writeOffset
< 0) {
2179 _mesa_error(ctx
, GL_INVALID_VALUE
,
2180 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2185 _mesa_error(ctx
, GL_INVALID_VALUE
,
2186 "%s(size %d < 0)", func
, (int) size
);
2190 if (readOffset
+ size
> src
->Size
) {
2191 _mesa_error(ctx
, GL_INVALID_VALUE
,
2192 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2193 (int) readOffset
, (int) size
, (int) src
->Size
);
2197 if (writeOffset
+ size
> dst
->Size
) {
2198 _mesa_error(ctx
, GL_INVALID_VALUE
,
2199 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2200 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2205 if (readOffset
+ size
<= writeOffset
) {
2208 else if (writeOffset
+ size
<= readOffset
) {
2212 /* overlapping src/dst is illegal */
2213 _mesa_error(ctx
, GL_INVALID_VALUE
,
2214 "%s(overlapping src/dst)", func
);
2219 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2223 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2224 GLintptr readOffset
, GLintptr writeOffset
,
2227 GET_CURRENT_CONTEXT(ctx
);
2228 struct gl_buffer_object
*src
, *dst
;
2230 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2231 GL_INVALID_OPERATION
);
2235 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2236 GL_INVALID_OPERATION
);
2240 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2241 "glCopyBufferSubData");
2245 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2246 GLintptr readOffset
, GLintptr writeOffset
,
2249 GET_CURRENT_CONTEXT(ctx
);
2250 struct gl_buffer_object
*src
, *dst
;
2252 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2253 "glCopyNamedBufferSubData");
2257 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2258 "glCopyNamedBufferSubData");
2262 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2263 "glCopyNamedBufferSubData");
2268 _mesa_map_buffer_range(struct gl_context
*ctx
,
2269 struct gl_buffer_object
*bufObj
,
2270 GLintptr offset
, GLsizeiptr length
,
2271 GLbitfield access
, const char *func
)
2274 GLbitfield allowed_access
;
2276 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2279 _mesa_error(ctx
, GL_INVALID_VALUE
,
2280 "%s(offset %ld < 0)", func
, (long) offset
);
2285 _mesa_error(ctx
, GL_INVALID_VALUE
,
2286 "%s(length %ld < 0)", func
, (long) length
);
2290 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2292 * "An INVALID_OPERATION error is generated for any of the following
2295 * * <length> is zero."
2297 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2298 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2302 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2306 allowed_access
= GL_MAP_READ_BIT
|
2308 GL_MAP_INVALIDATE_RANGE_BIT
|
2309 GL_MAP_INVALIDATE_BUFFER_BIT
|
2310 GL_MAP_FLUSH_EXPLICIT_BIT
|
2311 GL_MAP_UNSYNCHRONIZED_BIT
;
2313 if (ctx
->Extensions
.ARB_buffer_storage
) {
2314 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2315 GL_MAP_COHERENT_BIT
;
2318 if (access
& ~allowed_access
) {
2319 /* generate an error if any bits other than those allowed are set */
2320 _mesa_error(ctx
, GL_INVALID_VALUE
,
2321 "%s(access has undefined bits set)", func
);
2325 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2326 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2327 "%s(access indicates neither read or write)", func
);
2331 if ((access
& GL_MAP_READ_BIT
) &&
2332 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2333 GL_MAP_INVALIDATE_BUFFER_BIT
|
2334 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2335 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2336 "%s(read access with disallowed bits)", func
);
2340 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2341 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2342 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2343 "%s(access has flush explicit without write)", func
);
2347 if (access
& GL_MAP_READ_BIT
&&
2348 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2349 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2350 "%s(buffer does not allow read access)", func
);
2354 if (access
& GL_MAP_WRITE_BIT
&&
2355 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2356 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2357 "%s(buffer does not allow write access)", func
);
2361 if (access
& GL_MAP_COHERENT_BIT
&&
2362 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2363 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2364 "%s(buffer does not allow coherent access)", func
);
2368 if (access
& GL_MAP_PERSISTENT_BIT
&&
2369 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2370 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2371 "%s(buffer does not allow persistent access)", func
);
2375 if (offset
+ length
> bufObj
->Size
) {
2376 _mesa_error(ctx
, GL_INVALID_VALUE
,
2377 "%s(offset %td + length %td > buffer_size %td)", func
,
2378 offset
, length
, bufObj
->Size
);
2382 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2383 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2384 "%s(buffer already mapped)", func
);
2388 if (!bufObj
->Size
) {
2389 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2394 assert(ctx
->Driver
.MapBufferRange
);
2395 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2398 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2401 /* The driver callback should have set all these fields.
2402 * This is important because other modules (like VBO) might call
2403 * the driver function directly.
2405 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2406 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2407 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2408 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2411 if (access
& GL_MAP_WRITE_BIT
)
2412 bufObj
->Written
= GL_TRUE
;
2415 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2416 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2417 bufObj
->Name
, bufObj
->Size
, access
);
2418 /* Access must be write only */
2419 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2421 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2422 for (i
= 0; i
< bufObj
->Size
; i
++)
2429 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2430 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2432 /* buffer is 100 bytes larger than requested, fill with magic value */
2433 for (i
= 0; i
< 100; i
++) {
2434 buf
[bufObj
->Size
- i
- 1] = 123;
2443 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2446 GET_CURRENT_CONTEXT(ctx
);
2447 struct gl_buffer_object
*bufObj
;
2449 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2450 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2451 "glMapBufferRange(ARB_map_buffer_range not supported)");
2455 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2459 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2460 "glMapBufferRange");
2464 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2467 GET_CURRENT_CONTEXT(ctx
);
2468 struct gl_buffer_object
*bufObj
;
2470 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2471 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2472 "glMapNamedBufferRange("
2473 "ARB_map_buffer_range not supported)");
2477 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2481 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2482 "glMapNamedBufferRange");
2486 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2487 * flags for input to _mesa_map_buffer_range.
2489 * \return true if the type of requested access is permissible.
2492 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2496 case GL_READ_ONLY_ARB
:
2497 *flags
= GL_MAP_READ_BIT
;
2498 return _mesa_is_desktop_gl(ctx
);
2499 case GL_WRITE_ONLY_ARB
:
2500 *flags
= GL_MAP_WRITE_BIT
;
2502 case GL_READ_WRITE_ARB
:
2503 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2504 return _mesa_is_desktop_gl(ctx
);
2511 _mesa_MapBuffer(GLenum target
, GLenum access
)
2513 GET_CURRENT_CONTEXT(ctx
);
2514 struct gl_buffer_object
*bufObj
;
2515 GLbitfield accessFlags
;
2517 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2518 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2522 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2526 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2531 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2533 GET_CURRENT_CONTEXT(ctx
);
2534 struct gl_buffer_object
*bufObj
;
2535 GLbitfield accessFlags
;
2537 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2538 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2542 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2546 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2547 "glMapNamedBuffer");
2552 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2553 struct gl_buffer_object
*bufObj
,
2554 GLintptr offset
, GLsizeiptr length
,
2557 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2558 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2559 "%s(ARB_map_buffer_range not supported)", func
);
2564 _mesa_error(ctx
, GL_INVALID_VALUE
,
2565 "%s(offset %ld < 0)", func
, (long) offset
);
2570 _mesa_error(ctx
, GL_INVALID_VALUE
,
2571 "%s(length %ld < 0)", func
, (long) length
);
2575 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2576 /* buffer is not mapped */
2577 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2578 "%s(buffer is not mapped)", func
);
2582 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2583 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2584 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2585 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2589 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2590 _mesa_error(ctx
, GL_INVALID_VALUE
,
2591 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2592 (long) offset
, (long) length
,
2593 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2597 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2599 if (ctx
->Driver
.FlushMappedBufferRange
)
2600 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2605 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2608 GET_CURRENT_CONTEXT(ctx
);
2609 struct gl_buffer_object
*bufObj
;
2611 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2612 GL_INVALID_OPERATION
);
2616 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2617 "glFlushMappedBufferRange");
2621 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2624 GET_CURRENT_CONTEXT(ctx
);
2625 struct gl_buffer_object
*bufObj
;
2627 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2628 "glFlushMappedNamedBufferRange");
2632 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2633 "glFlushMappedNamedBufferRange");
2638 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2640 struct gl_buffer_object
*bufObj
;
2643 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2645 _mesa_error(ctx
, GL_INVALID_VALUE
,
2646 "glObjectPurgeable(name = 0x%x)", name
);
2649 if (!_mesa_is_bufferobj(bufObj
)) {
2650 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
2654 if (bufObj
->Purgeable
) {
2655 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2656 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
2657 return GL_VOLATILE_APPLE
;
2660 bufObj
->Purgeable
= GL_TRUE
;
2662 retval
= GL_VOLATILE_APPLE
;
2663 if (ctx
->Driver
.BufferObjectPurgeable
)
2664 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
2671 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2673 struct gl_renderbuffer
*bufObj
;
2676 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
2678 _mesa_error(ctx
, GL_INVALID_VALUE
,
2679 "glObjectUnpurgeable(name = 0x%x)", name
);
2683 if (bufObj
->Purgeable
) {
2684 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2685 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
2686 return GL_VOLATILE_APPLE
;
2689 bufObj
->Purgeable
= GL_TRUE
;
2691 retval
= GL_VOLATILE_APPLE
;
2692 if (ctx
->Driver
.RenderObjectPurgeable
)
2693 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
2700 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2702 struct gl_texture_object
*bufObj
;
2705 bufObj
= _mesa_lookup_texture(ctx
, name
);
2707 _mesa_error(ctx
, GL_INVALID_VALUE
,
2708 "glObjectPurgeable(name = 0x%x)", name
);
2712 if (bufObj
->Purgeable
) {
2713 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2714 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
2715 return GL_VOLATILE_APPLE
;
2718 bufObj
->Purgeable
= GL_TRUE
;
2720 retval
= GL_VOLATILE_APPLE
;
2721 if (ctx
->Driver
.TextureObjectPurgeable
)
2722 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
2729 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
2733 GET_CURRENT_CONTEXT(ctx
);
2734 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2737 _mesa_error(ctx
, GL_INVALID_VALUE
,
2738 "glObjectPurgeable(name = 0x%x)", name
);
2743 case GL_VOLATILE_APPLE
:
2744 case GL_RELEASED_APPLE
:
2748 _mesa_error(ctx
, GL_INVALID_ENUM
,
2749 "glObjectPurgeable(name = 0x%x) invalid option: %d",
2754 switch (objectType
) {
2756 retval
= texture_object_purgeable(ctx
, name
, option
);
2758 case GL_RENDERBUFFER_EXT
:
2759 retval
= renderbuffer_purgeable(ctx
, name
, option
);
2761 case GL_BUFFER_OBJECT_APPLE
:
2762 retval
= buffer_object_purgeable(ctx
, name
, option
);
2765 _mesa_error(ctx
, GL_INVALID_ENUM
,
2766 "glObjectPurgeable(name = 0x%x) invalid type: %d",
2771 /* In strict conformance to the spec, we must only return VOLATILE when
2772 * when passed the VOLATILE option. Madness.
2774 * XXX First fix the spec, then fix me.
2776 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
2781 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2783 struct gl_buffer_object
*bufObj
;
2786 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2788 _mesa_error(ctx
, GL_INVALID_VALUE
,
2789 "glObjectUnpurgeable(name = 0x%x)", name
);
2793 if (! bufObj
->Purgeable
) {
2794 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2795 "glObjectUnpurgeable(name = 0x%x) object is "
2796 " already \"unpurged\"", name
);
2800 bufObj
->Purgeable
= GL_FALSE
;
2803 if (ctx
->Driver
.BufferObjectUnpurgeable
)
2804 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
2811 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2813 struct gl_renderbuffer
*bufObj
;
2816 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
2818 _mesa_error(ctx
, GL_INVALID_VALUE
,
2819 "glObjectUnpurgeable(name = 0x%x)", name
);
2823 if (! bufObj
->Purgeable
) {
2824 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2825 "glObjectUnpurgeable(name = 0x%x) object is "
2826 " already \"unpurged\"", name
);
2830 bufObj
->Purgeable
= GL_FALSE
;
2833 if (ctx
->Driver
.RenderObjectUnpurgeable
)
2834 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
2841 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2843 struct gl_texture_object
*bufObj
;
2846 bufObj
= _mesa_lookup_texture(ctx
, name
);
2848 _mesa_error(ctx
, GL_INVALID_VALUE
,
2849 "glObjectUnpurgeable(name = 0x%x)", name
);
2853 if (! bufObj
->Purgeable
) {
2854 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2855 "glObjectUnpurgeable(name = 0x%x) object is"
2856 " already \"unpurged\"", name
);
2860 bufObj
->Purgeable
= GL_FALSE
;
2863 if (ctx
->Driver
.TextureObjectUnpurgeable
)
2864 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
2871 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
2873 GET_CURRENT_CONTEXT(ctx
);
2874 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2877 _mesa_error(ctx
, GL_INVALID_VALUE
,
2878 "glObjectUnpurgeable(name = 0x%x)", name
);
2883 case GL_RETAINED_APPLE
:
2884 case GL_UNDEFINED_APPLE
:
2888 _mesa_error(ctx
, GL_INVALID_ENUM
,
2889 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2894 switch (objectType
) {
2895 case GL_BUFFER_OBJECT_APPLE
:
2896 return buffer_object_unpurgeable(ctx
, name
, option
);
2898 return texture_object_unpurgeable(ctx
, name
, option
);
2899 case GL_RENDERBUFFER_EXT
:
2900 return renderbuffer_unpurgeable(ctx
, name
, option
);
2902 _mesa_error(ctx
, GL_INVALID_ENUM
,
2903 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2911 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
2912 GLenum pname
, GLint
*params
)
2914 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2916 _mesa_error(ctx
, GL_INVALID_VALUE
,
2917 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
2922 case GL_PURGEABLE_APPLE
:
2923 *params
= bufObj
->Purgeable
;
2926 _mesa_error(ctx
, GL_INVALID_ENUM
,
2927 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2935 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
2936 GLenum pname
, GLint
*params
)
2938 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
2940 _mesa_error(ctx
, GL_INVALID_VALUE
,
2941 "glObjectUnpurgeable(name = 0x%x)", name
);
2946 case GL_PURGEABLE_APPLE
:
2947 *params
= rb
->Purgeable
;
2950 _mesa_error(ctx
, GL_INVALID_ENUM
,
2951 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2959 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
2960 GLenum pname
, GLint
*params
)
2962 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
2964 _mesa_error(ctx
, GL_INVALID_VALUE
,
2965 "glObjectUnpurgeable(name = 0x%x)", name
);
2970 case GL_PURGEABLE_APPLE
:
2971 *params
= texObj
->Purgeable
;
2974 _mesa_error(ctx
, GL_INVALID_ENUM
,
2975 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2983 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
2986 GET_CURRENT_CONTEXT(ctx
);
2989 _mesa_error(ctx
, GL_INVALID_VALUE
,
2990 "glGetObjectParameteriv(name = 0x%x)", name
);
2994 switch (objectType
) {
2996 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
2998 case GL_BUFFER_OBJECT_APPLE
:
2999 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
3001 case GL_RENDERBUFFER_EXT
:
3002 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
3005 _mesa_error(ctx
, GL_INVALID_ENUM
,
3006 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
3012 * Binds a buffer object to a uniform buffer binding point.
3014 * The caller is responsible for flushing vertices and updating
3018 set_ubo_binding(struct gl_context
*ctx
,
3019 struct gl_uniform_buffer_binding
*binding
,
3020 struct gl_buffer_object
*bufObj
,
3025 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
3027 binding
->Offset
= offset
;
3028 binding
->Size
= size
;
3029 binding
->AutomaticSize
= autoSize
;
3031 /* If this is a real buffer object, mark it has having been used
3032 * at some point as a UBO.
3035 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
3039 * Binds a buffer object to a shader storage buffer binding point.
3041 * The caller is responsible for flushing vertices and updating
3045 set_ssbo_binding(struct gl_context
*ctx
,
3046 struct gl_shader_storage_buffer_binding
*binding
,
3047 struct gl_buffer_object
*bufObj
,
3052 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
3054 binding
->Offset
= offset
;
3055 binding
->Size
= size
;
3056 binding
->AutomaticSize
= autoSize
;
3058 /* If this is a real buffer object, mark it has having been used
3059 * at some point as a SSBO.
3062 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
3066 * Binds a buffer object to a uniform buffer binding point.
3068 * Unlike set_ubo_binding(), this function also flushes vertices
3069 * and updates NewDriverState. It also checks if the binding
3070 * has actually changed before updating it.
3073 bind_uniform_buffer(struct gl_context
*ctx
,
3075 struct gl_buffer_object
*bufObj
,
3080 struct gl_uniform_buffer_binding
*binding
=
3081 &ctx
->UniformBufferBindings
[index
];
3083 if (binding
->BufferObject
== bufObj
&&
3084 binding
->Offset
== offset
&&
3085 binding
->Size
== size
&&
3086 binding
->AutomaticSize
== autoSize
) {
3090 FLUSH_VERTICES(ctx
, 0);
3091 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3093 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
3097 * Binds a buffer object to a shader storage buffer binding point.
3099 * Unlike set_ssbo_binding(), this function also flushes vertices
3100 * and updates NewDriverState. It also checks if the binding
3101 * has actually changed before updating it.
3104 bind_shader_storage_buffer(struct gl_context
*ctx
,
3106 struct gl_buffer_object
*bufObj
,
3111 struct gl_shader_storage_buffer_binding
*binding
=
3112 &ctx
->ShaderStorageBufferBindings
[index
];
3114 if (binding
->BufferObject
== bufObj
&&
3115 binding
->Offset
== offset
&&
3116 binding
->Size
== size
&&
3117 binding
->AutomaticSize
== autoSize
) {
3121 FLUSH_VERTICES(ctx
, 0);
3122 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3124 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
3128 * Bind a region of a buffer object to a uniform block binding point.
3129 * \param index the uniform buffer binding point index
3130 * \param bufObj the buffer object
3131 * \param offset offset to the start of buffer object region
3132 * \param size size of the buffer object region
3135 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
3137 struct gl_buffer_object
*bufObj
,
3141 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3142 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3146 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3147 _mesa_error(ctx
, GL_INVALID_VALUE
,
3148 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3149 ctx
->Const
.UniformBufferOffsetAlignment
);
3153 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3158 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3159 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3163 * Bind a region of a buffer object to a shader storage block binding point.
3164 * \param index the shader storage buffer binding point index
3165 * \param bufObj the buffer object
3166 * \param offset offset to the start of buffer object region
3167 * \param size size of the buffer object region
3170 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3172 struct gl_buffer_object
*bufObj
,
3176 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3177 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3181 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3182 _mesa_error(ctx
, GL_INVALID_VALUE
,
3183 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3184 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3188 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3193 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3194 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3198 * Bind a buffer object to a uniform block binding point.
3199 * As above, but offset = 0.
3202 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
3204 struct gl_buffer_object
*bufObj
)
3206 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3207 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
3211 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3213 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3214 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
3216 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
3220 * Bind a buffer object to a shader storage block binding point.
3221 * As above, but offset = 0.
3224 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
3226 struct gl_buffer_object
*bufObj
)
3228 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3229 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
3233 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3235 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3236 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
3238 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
3242 * Binds a buffer object to an atomic buffer binding point.
3244 * The caller is responsible for validating the offset,
3245 * flushing the vertices and updating NewDriverState.
3248 set_atomic_buffer_binding(struct gl_context
*ctx
,
3249 struct gl_atomic_buffer_binding
*binding
,
3250 struct gl_buffer_object
*bufObj
,
3254 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
3256 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3257 binding
->Offset
= -1;
3260 binding
->Offset
= offset
;
3261 binding
->Size
= size
;
3262 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
3267 * Binds a buffer object to an atomic buffer binding point.
3269 * Unlike set_atomic_buffer_binding(), this function also validates the
3270 * index and offset, flushes vertices, and updates NewDriverState.
3271 * It also checks if the binding has actually changing before
3275 bind_atomic_buffer(struct gl_context
*ctx
,
3277 struct gl_buffer_object
*bufObj
,
3282 struct gl_atomic_buffer_binding
*binding
;
3284 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3285 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3289 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3290 _mesa_error(ctx
, GL_INVALID_VALUE
,
3291 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3292 ATOMIC_COUNTER_SIZE
);
3296 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3298 binding
= &ctx
->AtomicBufferBindings
[index
];
3299 if (binding
->BufferObject
== bufObj
&&
3300 binding
->Offset
== offset
&&
3301 binding
->Size
== size
) {
3305 FLUSH_VERTICES(ctx
, 0);
3306 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3308 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3312 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3314 const GLintptr
*offsets
,
3315 const GLsizeiptr
*sizes
)
3317 if (offsets
[index
] < 0) {
3318 /* The ARB_multi_bind spec says:
3320 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3321 * value in <offsets> is less than zero (per binding)."
3323 _mesa_error(ctx
, GL_INVALID_VALUE
,
3324 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3325 index
, (int64_t) offsets
[index
]);
3329 if (sizes
[index
] <= 0) {
3330 /* The ARB_multi_bind spec says:
3332 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3333 * value in <sizes> is less than or equal to zero (per binding)."
3335 _mesa_error(ctx
, GL_INVALID_VALUE
,
3336 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3337 index
, (int64_t) sizes
[index
]);
3345 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3346 GLuint first
, GLsizei count
,
3349 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3350 _mesa_error(ctx
, GL_INVALID_ENUM
,
3351 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3355 /* The ARB_multi_bind_spec says:
3357 * "An INVALID_OPERATION error is generated if <first> + <count> is
3358 * greater than the number of target-specific indexed binding points,
3359 * as described in section 6.7.1."
3361 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3362 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3363 "%s(first=%u + count=%d > the value of "
3364 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3365 caller
, first
, count
,
3366 ctx
->Const
.MaxUniformBufferBindings
);
3374 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3375 GLuint first
, GLsizei count
,
3378 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3379 _mesa_error(ctx
, GL_INVALID_ENUM
,
3380 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3384 /* The ARB_multi_bind_spec says:
3386 * "An INVALID_OPERATION error is generated if <first> + <count> is
3387 * greater than the number of target-specific indexed binding points,
3388 * as described in section 6.7.1."
3390 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3391 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3392 "%s(first=%u + count=%d > the value of "
3393 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3394 caller
, first
, count
,
3395 ctx
->Const
.MaxShaderStorageBufferBindings
);
3403 * Unbind all uniform buffers in the range
3404 * <first> through <first>+<count>-1
3407 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3409 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3412 for (i
= 0; i
< count
; i
++)
3413 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3414 bufObj
, -1, -1, GL_TRUE
);
3418 * Unbind all shader storage buffers in the range
3419 * <first> through <first>+<count>-1
3422 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3425 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3428 for (i
= 0; i
< count
; i
++)
3429 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3430 bufObj
, -1, -1, GL_TRUE
);
3434 bind_uniform_buffers_base(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3435 const GLuint
*buffers
)
3439 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, "glBindBuffersBase"))
3442 /* Assume that at least one binding will be changed */
3443 FLUSH_VERTICES(ctx
, 0);
3444 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3447 /* The ARB_multi_bind spec says:
3449 * "If <buffers> is NULL, all bindings from <first> through
3450 * <first>+<count>-1 are reset to their unbound (zero) state."
3452 unbind_uniform_buffers(ctx
, first
, count
);
3456 /* Note that the error semantics for multi-bind commands differ from
3457 * those of other GL commands.
3459 * The Issues section in the ARB_multi_bind spec says:
3461 * "(11) Typically, OpenGL specifies that if an error is generated by a
3462 * command, that command has no effect. This is somewhat
3463 * unfortunate for multi-bind commands, because it would require a
3464 * first pass to scan the entire list of bound objects for errors
3465 * and then a second pass to actually perform the bindings.
3466 * Should we have different error semantics?
3468 * RESOLVED: Yes. In this specification, when the parameters for
3469 * one of the <count> binding points are invalid, that binding point
3470 * is not updated and an error will be generated. However, other
3471 * binding points in the same command will be updated if their
3472 * parameters are valid and no other error occurs."
3475 _mesa_begin_bufferobj_lookups(ctx
);
3477 for (i
= 0; i
< count
; i
++) {
3478 struct gl_uniform_buffer_binding
*binding
=
3479 &ctx
->UniformBufferBindings
[first
+ i
];
3480 struct gl_buffer_object
*bufObj
;
3482 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3483 bufObj
= binding
->BufferObject
;
3485 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3486 "glBindBuffersBase");
3489 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3490 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_TRUE
);
3492 set_ubo_binding(ctx
, binding
, bufObj
, 0, 0, GL_TRUE
);
3496 _mesa_end_bufferobj_lookups(ctx
);
3500 bind_shader_storage_buffers_base(struct gl_context
*ctx
, GLuint first
,
3501 GLsizei count
, const GLuint
*buffers
)
3505 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
,
3506 "glBindBuffersBase"))
3509 /* Assume that at least one binding will be changed */
3510 FLUSH_VERTICES(ctx
, 0);
3511 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3514 /* The ARB_multi_bind spec says:
3516 * "If <buffers> is NULL, all bindings from <first> through
3517 * <first>+<count>-1 are reset to their unbound (zero) state."
3519 unbind_shader_storage_buffers(ctx
, first
, count
);
3523 /* Note that the error semantics for multi-bind commands differ from
3524 * those of other GL commands.
3526 * The Issues section in the ARB_multi_bind spec says:
3528 * "(11) Typically, OpenGL specifies that if an error is generated by a
3529 * command, that command has no effect. This is somewhat
3530 * unfortunate for multi-bind commands, because it would require a
3531 * first pass to scan the entire list of bound objects for errors
3532 * and then a second pass to actually perform the bindings.
3533 * Should we have different error semantics?
3535 * RESOLVED: Yes. In this specification, when the parameters for
3536 * one of the <count> binding points are invalid, that binding point
3537 * is not updated and an error will be generated. However, other
3538 * binding points in the same command will be updated if their
3539 * parameters are valid and no other error occurs."
3542 _mesa_begin_bufferobj_lookups(ctx
);
3544 for (i
= 0; i
< count
; i
++) {
3545 struct gl_shader_storage_buffer_binding
*binding
=
3546 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3547 struct gl_buffer_object
*bufObj
;
3549 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3550 bufObj
= binding
->BufferObject
;
3552 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3553 "glBindBuffersBase");
3556 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3557 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, GL_TRUE
);
3559 set_ssbo_binding(ctx
, binding
, bufObj
, 0, 0, GL_TRUE
);
3563 _mesa_end_bufferobj_lookups(ctx
);
3567 bind_uniform_buffers_range(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3568 const GLuint
*buffers
,
3569 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3573 if (!error_check_bind_uniform_buffers(ctx
, first
, count
,
3574 "glBindBuffersRange"))
3577 /* Assume that at least one binding will be changed */
3578 FLUSH_VERTICES(ctx
, 0);
3579 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3582 /* The ARB_multi_bind spec says:
3584 * "If <buffers> is NULL, all bindings from <first> through
3585 * <first>+<count>-1 are reset to their unbound (zero) state.
3586 * In this case, the offsets and sizes associated with the
3587 * binding points are set to default values, ignoring
3588 * <offsets> and <sizes>."
3590 unbind_uniform_buffers(ctx
, first
, count
);
3594 /* Note that the error semantics for multi-bind commands differ from
3595 * those of other GL commands.
3597 * The Issues section in the ARB_multi_bind spec says:
3599 * "(11) Typically, OpenGL specifies that if an error is generated by a
3600 * command, that command has no effect. This is somewhat
3601 * unfortunate for multi-bind commands, because it would require a
3602 * first pass to scan the entire list of bound objects for errors
3603 * and then a second pass to actually perform the bindings.
3604 * Should we have different error semantics?
3606 * RESOLVED: Yes. In this specification, when the parameters for
3607 * one of the <count> binding points are invalid, that binding point
3608 * is not updated and an error will be generated. However, other
3609 * binding points in the same command will be updated if their
3610 * parameters are valid and no other error occurs."
3613 _mesa_begin_bufferobj_lookups(ctx
);
3615 for (i
= 0; i
< count
; i
++) {
3616 struct gl_uniform_buffer_binding
*binding
=
3617 &ctx
->UniformBufferBindings
[first
+ i
];
3618 struct gl_buffer_object
*bufObj
;
3620 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3623 /* The ARB_multi_bind spec says:
3625 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3626 * pair of values in <offsets> and <sizes> does not respectively
3627 * satisfy the constraints described for those parameters for the
3628 * specified target, as described in section 6.7.1 (per binding)."
3630 * Section 6.7.1 refers to table 6.5, which says:
3632 * "┌───────────────────────────────────────────────────────────────┐
3633 * │ Uniform buffer array bindings (see sec. 7.6) │
3634 * ├─────────────────────┬─────────────────────────────────────────┤
3636 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3637 * │ │ OFFSET_ALIGNMENT │
3639 * │ size restriction │ none │
3640 * └─────────────────────┴─────────────────────────────────────────┘"
3642 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3643 _mesa_error(ctx
, GL_INVALID_VALUE
,
3644 "glBindBuffersRange(offsets[%u]=%" PRId64
3645 " is misaligned; it must be a multiple of the value of "
3646 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3647 "target=GL_UNIFORM_BUFFER)",
3648 i
, (int64_t) offsets
[i
],
3649 ctx
->Const
.UniformBufferOffsetAlignment
);
3653 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3654 bufObj
= binding
->BufferObject
;
3656 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3657 "glBindBuffersRange");
3660 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3661 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_FALSE
);
3663 set_ubo_binding(ctx
, binding
, bufObj
,
3664 offsets
[i
], sizes
[i
], GL_FALSE
);
3668 _mesa_end_bufferobj_lookups(ctx
);
3672 bind_shader_storage_buffers_range(struct gl_context
*ctx
, GLuint first
,
3673 GLsizei count
, const GLuint
*buffers
,
3674 const GLintptr
*offsets
,
3675 const GLsizeiptr
*sizes
)
3679 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
,
3680 "glBindBuffersRange"))
3683 /* Assume that at least one binding will be changed */
3684 FLUSH_VERTICES(ctx
, 0);
3685 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3688 /* The ARB_multi_bind spec says:
3690 * "If <buffers> is NULL, all bindings from <first> through
3691 * <first>+<count>-1 are reset to their unbound (zero) state.
3692 * In this case, the offsets and sizes associated with the
3693 * binding points are set to default values, ignoring
3694 * <offsets> and <sizes>."
3696 unbind_shader_storage_buffers(ctx
, first
, count
);
3700 /* Note that the error semantics for multi-bind commands differ from
3701 * those of other GL commands.
3703 * The Issues section in the ARB_multi_bind spec says:
3705 * "(11) Typically, OpenGL specifies that if an error is generated by a
3706 * command, that command has no effect. This is somewhat
3707 * unfortunate for multi-bind commands, because it would require a
3708 * first pass to scan the entire list of bound objects for errors
3709 * and then a second pass to actually perform the bindings.
3710 * Should we have different error semantics?
3712 * RESOLVED: Yes. In this specification, when the parameters for
3713 * one of the <count> binding points are invalid, that binding point
3714 * is not updated and an error will be generated. However, other
3715 * binding points in the same command will be updated if their
3716 * parameters are valid and no other error occurs."
3719 _mesa_begin_bufferobj_lookups(ctx
);
3721 for (i
= 0; i
< count
; i
++) {
3722 struct gl_shader_storage_buffer_binding
*binding
=
3723 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3724 struct gl_buffer_object
*bufObj
;
3726 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3729 /* The ARB_multi_bind spec says:
3731 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3732 * pair of values in <offsets> and <sizes> does not respectively
3733 * satisfy the constraints described for those parameters for the
3734 * specified target, as described in section 6.7.1 (per binding)."
3736 * Section 6.7.1 refers to table 6.5, which says:
3738 * "┌───────────────────────────────────────────────────────────────┐
3739 * │ Shader storage buffer array bindings (see sec. 7.8) │
3740 * ├─────────────────────┬─────────────────────────────────────────┤
3742 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3743 * │ │ BUFFER_OFFSET_ALIGNMENT │
3745 * │ size restriction │ none │
3746 * └─────────────────────┴─────────────────────────────────────────┘"
3748 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3749 _mesa_error(ctx
, GL_INVALID_VALUE
,
3750 "glBindBuffersRange(offsets[%u]=%" PRId64
3751 " is misaligned; it must be a multiple of the value of "
3752 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3753 "target=GL_SHADER_STORAGE_BUFFER)",
3754 i
, (int64_t) offsets
[i
],
3755 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3759 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3760 bufObj
= binding
->BufferObject
;
3762 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3763 "glBindBuffersRange");
3766 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3767 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, GL_FALSE
);
3769 set_ssbo_binding(ctx
, binding
, bufObj
,
3770 offsets
[i
], sizes
[i
], GL_FALSE
);
3774 _mesa_end_bufferobj_lookups(ctx
);
3778 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3779 struct gl_transform_feedback_object
*tfObj
,
3780 GLuint first
, GLsizei count
, const char *caller
)
3782 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3783 _mesa_error(ctx
, GL_INVALID_ENUM
,
3784 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3788 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3790 * "An INVALID_OPERATION error is generated :
3793 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3794 * FEEDBACK_BUFFER and transform feedback is currently active."
3796 * We assume that this is also meant to apply to BindBuffersRange
3797 * and BindBuffersBase.
3799 if (tfObj
->Active
) {
3800 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3801 "%s(Changing transform feedback buffers while "
3802 "transform feedback is active)", caller
);
3806 /* The ARB_multi_bind_spec says:
3808 * "An INVALID_OPERATION error is generated if <first> + <count> is
3809 * greater than the number of target-specific indexed binding points,
3810 * as described in section 6.7.1."
3812 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3813 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3814 "%s(first=%u + count=%d > the value of "
3815 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3816 caller
, first
, count
,
3817 ctx
->Const
.MaxTransformFeedbackBuffers
);
3825 * Unbind all transform feedback buffers in the range
3826 * <first> through <first>+<count>-1
3829 unbind_xfb_buffers(struct gl_context
*ctx
,
3830 struct gl_transform_feedback_object
*tfObj
,
3831 GLuint first
, GLsizei count
)
3833 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3836 for (i
= 0; i
< count
; i
++)
3837 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3842 bind_xfb_buffers_base(struct gl_context
*ctx
,
3843 GLuint first
, GLsizei count
,
3844 const GLuint
*buffers
)
3846 struct gl_transform_feedback_object
*tfObj
=
3847 ctx
->TransformFeedback
.CurrentObject
;
3850 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3851 "glBindBuffersBase"))
3854 /* Assume that at least one binding will be changed */
3855 FLUSH_VERTICES(ctx
, 0);
3856 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3859 /* The ARB_multi_bind spec says:
3861 * "If <buffers> is NULL, all bindings from <first> through
3862 * <first>+<count>-1 are reset to their unbound (zero) state."
3864 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3868 /* Note that the error semantics for multi-bind commands differ from
3869 * those of other GL commands.
3871 * The Issues section in the ARB_multi_bind spec says:
3873 * "(11) Typically, OpenGL specifies that if an error is generated by a
3874 * command, that command has no effect. This is somewhat
3875 * unfortunate for multi-bind commands, because it would require a
3876 * first pass to scan the entire list of bound objects for errors
3877 * and then a second pass to actually perform the bindings.
3878 * Should we have different error semantics?
3880 * RESOLVED: Yes. In this specification, when the parameters for
3881 * one of the <count> binding points are invalid, that binding point
3882 * is not updated and an error will be generated. However, other
3883 * binding points in the same command will be updated if their
3884 * parameters are valid and no other error occurs."
3887 _mesa_begin_bufferobj_lookups(ctx
);
3889 for (i
= 0; i
< count
; i
++) {
3890 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[first
+ i
];
3891 struct gl_buffer_object
*bufObj
;
3893 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3894 bufObj
= boundBufObj
;
3896 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3897 "glBindBuffersBase");
3900 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3904 _mesa_end_bufferobj_lookups(ctx
);
3908 bind_xfb_buffers_range(struct gl_context
*ctx
,
3909 GLuint first
, GLsizei count
,
3910 const GLuint
*buffers
,
3911 const GLintptr
*offsets
,
3912 const GLsizeiptr
*sizes
)
3914 struct gl_transform_feedback_object
*tfObj
=
3915 ctx
->TransformFeedback
.CurrentObject
;
3918 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3919 "glBindBuffersRange"))
3922 /* Assume that at least one binding will be changed */
3923 FLUSH_VERTICES(ctx
, 0);
3924 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3927 /* The ARB_multi_bind spec says:
3929 * "If <buffers> is NULL, all bindings from <first> through
3930 * <first>+<count>-1 are reset to their unbound (zero) state.
3931 * In this case, the offsets and sizes associated with the
3932 * binding points are set to default values, ignoring
3933 * <offsets> and <sizes>."
3935 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3939 /* Note that the error semantics for multi-bind commands differ from
3940 * those of other GL commands.
3942 * The Issues section in the ARB_multi_bind spec says:
3944 * "(11) Typically, OpenGL specifies that if an error is generated by a
3945 * command, that command has no effect. This is somewhat
3946 * unfortunate for multi-bind commands, because it would require a
3947 * first pass to scan the entire list of bound objects for errors
3948 * and then a second pass to actually perform the bindings.
3949 * Should we have different error semantics?
3951 * RESOLVED: Yes. In this specification, when the parameters for
3952 * one of the <count> binding points are invalid, that binding point
3953 * is not updated and an error will be generated. However, other
3954 * binding points in the same command will be updated if their
3955 * parameters are valid and no other error occurs."
3958 _mesa_begin_bufferobj_lookups(ctx
);
3960 for (i
= 0; i
< count
; i
++) {
3961 const GLuint index
= first
+ i
;
3962 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3963 struct gl_buffer_object
*bufObj
;
3965 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3968 /* The ARB_multi_bind spec says:
3970 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3971 * pair of values in <offsets> and <sizes> does not respectively
3972 * satisfy the constraints described for those parameters for the
3973 * specified target, as described in section 6.7.1 (per binding)."
3975 * Section 6.7.1 refers to table 6.5, which says:
3977 * "┌───────────────────────────────────────────────────────────────┐
3978 * │ Transform feedback array bindings (see sec. 13.2.2) │
3979 * ├───────────────────────┬───────────────────────────────────────┤
3981 * │ offset restriction │ multiple of 4 │
3983 * │ size restriction │ multiple of 4 │
3984 * └───────────────────────┴───────────────────────────────────────┘"
3986 if (offsets
[i
] & 0x3) {
3987 _mesa_error(ctx
, GL_INVALID_VALUE
,
3988 "glBindBuffersRange(offsets[%u]=%" PRId64
3989 " is misaligned; it must be a multiple of 4 when "
3990 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3991 i
, (int64_t) offsets
[i
]);
3995 if (sizes
[i
] & 0x3) {
3996 _mesa_error(ctx
, GL_INVALID_VALUE
,
3997 "glBindBuffersRange(sizes[%u]=%" PRId64
3998 " is misaligned; it must be a multiple of 4 when "
3999 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4000 i
, (int64_t) sizes
[i
]);
4004 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
4005 bufObj
= boundBufObj
;
4007 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
4008 "glBindBuffersRange");
4011 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
4012 offsets
[i
], sizes
[i
]);
4015 _mesa_end_bufferobj_lookups(ctx
);
4019 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
4020 GLuint first
, GLsizei count
,
4023 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
4024 _mesa_error(ctx
, GL_INVALID_ENUM
,
4025 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
4029 /* The ARB_multi_bind_spec says:
4031 * "An INVALID_OPERATION error is generated if <first> + <count> is
4032 * greater than the number of target-specific indexed binding points,
4033 * as described in section 6.7.1."
4035 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
4036 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4037 "%s(first=%u + count=%d > the value of "
4038 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4039 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
4047 * Unbind all atomic counter buffers in the range
4048 * <first> through <first>+<count>-1
4051 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
4053 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
4056 for (i
= 0; i
< count
; i
++)
4057 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
4062 bind_atomic_buffers_base(struct gl_context
*ctx
,
4065 const GLuint
*buffers
)
4069 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
4070 "glBindBuffersBase"))
4073 /* Assume that at least one binding will be changed */
4074 FLUSH_VERTICES(ctx
, 0);
4075 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
4078 /* The ARB_multi_bind spec says:
4080 * "If <buffers> is NULL, all bindings from <first> through
4081 * <first>+<count>-1 are reset to their unbound (zero) state."
4083 unbind_atomic_buffers(ctx
, first
, count
);
4087 /* Note that the error semantics for multi-bind commands differ from
4088 * those of other GL commands.
4090 * The Issues section in the ARB_multi_bind spec says:
4092 * "(11) Typically, OpenGL specifies that if an error is generated by a
4093 * command, that command has no effect. This is somewhat
4094 * unfortunate for multi-bind commands, because it would require a
4095 * first pass to scan the entire list of bound objects for errors
4096 * and then a second pass to actually perform the bindings.
4097 * Should we have different error semantics?
4099 * RESOLVED: Yes. In this specification, when the parameters for
4100 * one of the <count> binding points are invalid, that binding point
4101 * is not updated and an error will be generated. However, other
4102 * binding points in the same command will be updated if their
4103 * parameters are valid and no other error occurs."
4106 _mesa_begin_bufferobj_lookups(ctx
);
4108 for (i
= 0; i
< count
; i
++) {
4109 struct gl_atomic_buffer_binding
*binding
=
4110 &ctx
->AtomicBufferBindings
[first
+ i
];
4111 struct gl_buffer_object
*bufObj
;
4113 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
4114 bufObj
= binding
->BufferObject
;
4116 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
4117 "glBindBuffersBase");
4120 set_atomic_buffer_binding(ctx
, binding
, bufObj
, 0, 0);
4123 _mesa_end_bufferobj_lookups(ctx
);
4127 bind_atomic_buffers_range(struct gl_context
*ctx
,
4130 const GLuint
*buffers
,
4131 const GLintptr
*offsets
,
4132 const GLsizeiptr
*sizes
)
4136 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
4137 "glBindBuffersRange"))
4140 /* Assume that at least one binding will be changed */
4141 FLUSH_VERTICES(ctx
, 0);
4142 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
4145 /* The ARB_multi_bind spec says:
4147 * "If <buffers> is NULL, all bindings from <first> through
4148 * <first>+<count>-1 are reset to their unbound (zero) state.
4149 * In this case, the offsets and sizes associated with the
4150 * binding points are set to default values, ignoring
4151 * <offsets> and <sizes>."
4153 unbind_atomic_buffers(ctx
, first
, count
);
4157 /* Note that the error semantics for multi-bind commands differ from
4158 * those of other GL commands.
4160 * The Issues section in the ARB_multi_bind spec says:
4162 * "(11) Typically, OpenGL specifies that if an error is generated by a
4163 * command, that command has no effect. This is somewhat
4164 * unfortunate for multi-bind commands, because it would require a
4165 * first pass to scan the entire list of bound objects for errors
4166 * and then a second pass to actually perform the bindings.
4167 * Should we have different error semantics?
4169 * RESOLVED: Yes. In this specification, when the parameters for
4170 * one of the <count> binding points are invalid, that binding point
4171 * is not updated and an error will be generated. However, other
4172 * binding points in the same command will be updated if their
4173 * parameters are valid and no other error occurs."
4176 _mesa_begin_bufferobj_lookups(ctx
);
4178 for (i
= 0; i
< count
; i
++) {
4179 struct gl_atomic_buffer_binding
*binding
=
4180 &ctx
->AtomicBufferBindings
[first
+ i
];
4181 struct gl_buffer_object
*bufObj
;
4183 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4186 /* The ARB_multi_bind spec says:
4188 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4189 * pair of values in <offsets> and <sizes> does not respectively
4190 * satisfy the constraints described for those parameters for the
4191 * specified target, as described in section 6.7.1 (per binding)."
4193 * Section 6.7.1 refers to table 6.5, which says:
4195 * "┌───────────────────────────────────────────────────────────────┐
4196 * │ Atomic counter array bindings (see sec. 7.7.2) │
4197 * ├───────────────────────┬───────────────────────────────────────┤
4199 * │ offset restriction │ multiple of 4 │
4201 * │ size restriction │ none │
4202 * └───────────────────────┴───────────────────────────────────────┘"
4204 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
4205 _mesa_error(ctx
, GL_INVALID_VALUE
,
4206 "glBindBuffersRange(offsets[%u]=%" PRId64
4207 " is misaligned; it must be a multiple of %d when "
4208 "target=GL_ATOMIC_COUNTER_BUFFER)",
4209 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
4213 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
4214 bufObj
= binding
->BufferObject
;
4216 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
4217 "glBindBuffersRange");
4220 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offsets
[i
], sizes
[i
]);
4223 _mesa_end_bufferobj_lookups(ctx
);
4227 _mesa_BindBufferRange(GLenum target
, GLuint index
,
4228 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
4230 GET_CURRENT_CONTEXT(ctx
);
4231 struct gl_buffer_object
*bufObj
;
4234 bufObj
= ctx
->Shared
->NullBufferObj
;
4236 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4238 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4239 &bufObj
, "glBindBufferRange"))
4243 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4244 "glBindBufferRange(invalid buffer=%u)", buffer
);
4250 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
4257 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4258 _mesa_bind_buffer_range_transform_feedback(ctx
,
4259 ctx
->TransformFeedback
.CurrentObject
,
4260 index
, bufObj
, offset
, size
,
4263 case GL_UNIFORM_BUFFER
:
4264 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
4266 case GL_SHADER_STORAGE_BUFFER
:
4267 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
4269 case GL_ATOMIC_COUNTER_BUFFER
:
4270 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
4271 "glBindBufferRange");
4274 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
4280 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
4282 GET_CURRENT_CONTEXT(ctx
);
4283 struct gl_buffer_object
*bufObj
;
4286 bufObj
= ctx
->Shared
->NullBufferObj
;
4288 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4290 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4291 &bufObj
, "glBindBufferBase"))
4295 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4296 "glBindBufferBase(invalid buffer=%u)", buffer
);
4300 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4301 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4303 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4304 * zero and size equal to the size of buffer."
4306 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4308 * "If the parameter (starting offset or size) was not specified when the
4309 * buffer object was bound, zero is returned."
4311 * What happens if the size of the buffer changes? Does the size of the
4312 * buffer at the moment glBindBufferBase was called still play a role, like
4313 * the first quote would imply, or is the size meaningless in the
4314 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4315 * core spec page 45 says:
4317 * "It is equivalent to calling BindBufferRange with offset zero, while
4318 * size is determined by the size of the bound buffer at the time the
4321 * My interpretation is that the GL 4.1 spec was a clarification of the
4322 * behavior, not a change. In particular, this choice will only make
4323 * rendering work in cases where it would have had undefined results.
4327 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4328 _mesa_bind_buffer_base_transform_feedback(ctx
,
4329 ctx
->TransformFeedback
.CurrentObject
,
4330 index
, bufObj
, false);
4332 case GL_UNIFORM_BUFFER
:
4333 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4335 case GL_SHADER_STORAGE_BUFFER
:
4336 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4338 case GL_ATOMIC_COUNTER_BUFFER
:
4339 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
4340 "glBindBufferBase");
4343 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4349 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4350 const GLuint
*buffers
,
4351 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4353 GET_CURRENT_CONTEXT(ctx
);
4356 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4357 bind_xfb_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
4359 case GL_UNIFORM_BUFFER
:
4360 bind_uniform_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
4362 case GL_SHADER_STORAGE_BUFFER
:
4363 bind_shader_storage_buffers_range(ctx
, first
, count
, buffers
, offsets
,
4366 case GL_ATOMIC_COUNTER_BUFFER
:
4367 bind_atomic_buffers_range(ctx
, first
, count
, buffers
,
4371 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4372 _mesa_enum_to_string(target
));
4378 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4379 const GLuint
*buffers
)
4381 GET_CURRENT_CONTEXT(ctx
);
4384 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4385 bind_xfb_buffers_base(ctx
, first
, count
, buffers
);
4387 case GL_UNIFORM_BUFFER
:
4388 bind_uniform_buffers_base(ctx
, first
, count
, buffers
);
4390 case GL_SHADER_STORAGE_BUFFER
:
4391 bind_shader_storage_buffers_base(ctx
, first
, count
, buffers
);
4393 case GL_ATOMIC_COUNTER_BUFFER
:
4394 bind_atomic_buffers_base(ctx
, first
, count
, buffers
);
4397 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4398 _mesa_enum_to_string(target
));
4404 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4407 GET_CURRENT_CONTEXT(ctx
);
4408 struct gl_buffer_object
*bufObj
;
4409 const GLintptr end
= offset
+ length
;
4411 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4413 _mesa_error(ctx
, GL_INVALID_VALUE
,
4414 "glInvalidateBufferSubData(name = 0x%x) invalid object",
4419 /* The GL_ARB_invalidate_subdata spec says:
4421 * "An INVALID_VALUE error is generated if <offset> or <length> is
4422 * negative, or if <offset> + <length> is greater than the value of
4425 if (end
< 0 || end
> bufObj
->Size
) {
4426 _mesa_error(ctx
, GL_INVALID_VALUE
,
4427 "glInvalidateBufferSubData(invalid offset or length)");
4431 /* The OpenGL 4.4 (Core Profile) spec says:
4433 * "An INVALID_OPERATION error is generated if buffer is currently
4434 * mapped by MapBuffer or if the invalidate range intersects the range
4435 * currently mapped by MapBufferRange, unless it was mapped
4436 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4438 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4439 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4440 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4441 "glInvalidateBufferSubData(intersection with mapped "
4446 /* We don't actually do anything for this yet. Just return after
4447 * validating the parameters and generating the required errors.
4453 _mesa_InvalidateBufferData(GLuint buffer
)
4455 GET_CURRENT_CONTEXT(ctx
);
4456 struct gl_buffer_object
*bufObj
;
4458 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4460 _mesa_error(ctx
, GL_INVALID_VALUE
,
4461 "glInvalidateBufferData(name = 0x%x) invalid object",
4466 /* The OpenGL 4.4 (Core Profile) spec says:
4468 * "An INVALID_OPERATION error is generated if buffer is currently
4469 * mapped by MapBuffer or if the invalidate range intersects the range
4470 * currently mapped by MapBufferRange, unless it was mapped
4471 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4473 if (_mesa_check_disallowed_mapping(bufObj
)) {
4474 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4475 "glInvalidateBufferData(intersection with mapped "
4480 /* We don't actually do anything for this yet. Just return after
4481 * validating the parameters and generating the required errors.