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 return &ctx
->DrawIndirectBuffer
;
99 case GL_TRANSFORM_FEEDBACK_BUFFER
:
100 if (ctx
->Extensions
.EXT_transform_feedback
) {
101 return &ctx
->TransformFeedback
.CurrentBuffer
;
104 case GL_TEXTURE_BUFFER
:
105 if (ctx
->API
== API_OPENGL_CORE
&&
106 ctx
->Extensions
.ARB_texture_buffer_object
) {
107 return &ctx
->Texture
.BufferObject
;
110 case GL_UNIFORM_BUFFER
:
111 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
112 return &ctx
->UniformBuffer
;
115 case GL_ATOMIC_COUNTER_BUFFER
:
116 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
117 return &ctx
->AtomicBuffer
;
120 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
121 if (ctx
->Extensions
.AMD_pinned_memory
) {
122 return &ctx
->ExternalVirtualMemoryBuffer
;
133 * Get the buffer object bound to the specified target in a GL context.
134 * \param ctx the GL context
135 * \param target the buffer object target to be retrieved.
136 * \param error the GL error to record if target is illegal.
137 * \return pointer to the buffer object bound to \c target in the
138 * specified context or \c NULL if \c target is invalid.
140 static inline struct gl_buffer_object
*
141 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
144 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
147 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
151 if (!_mesa_is_bufferobj(*bufObj
)) {
152 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
161 * Convert a GLbitfield describing the mapped buffer access flags
162 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
165 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
167 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
168 if ((access
& rwFlags
) == rwFlags
)
169 return GL_READ_WRITE
;
170 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
172 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
173 return GL_WRITE_ONLY
;
175 /* Otherwise, AccessFlags is zero (the default state).
177 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
179 * Name Type Initial Value Legal Values
181 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
184 * However, table 6.8 in the GL_OES_mapbuffer extension says:
186 * Get Value Type Get Command Value Description
187 * --------- ---- ----------- ----- -----------
188 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
190 * The difference is because GL_OES_mapbuffer only supports mapping buffers
195 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
200 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
202 * The regions do not overlap if and only if the end of the given
203 * region is before the mapped region or the start of the given region
204 * is after the mapped region.
206 * \param obj Buffer object target on which to operate.
207 * \param offset Offset of the first byte of the subdata range.
208 * \param size Size, in bytes, of the subdata range.
209 * \return true if ranges overlap, false otherwise
213 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
214 GLintptr offset
, GLsizeiptr size
)
216 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
217 const GLintptr end
= offset
+ size
;
218 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
219 obj
->Mappings
[MAP_USER
].Length
;
221 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
230 * Tests the subdata range parameters and sets the GL error code for
231 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
232 * \c glClearBufferSubData.
234 * \param ctx GL context.
235 * \param bufObj The buffer object.
236 * \param offset Offset of the first byte of the subdata range.
237 * \param size Size, in bytes, of the subdata range.
238 * \param mappedRange If true, checks if an overlapping range is mapped.
239 * If false, checks if buffer is mapped.
240 * \param caller Name of calling function for recording errors.
241 * \return false if error, true otherwise
243 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
246 buffer_object_subdata_range_good(struct gl_context
*ctx
,
247 struct gl_buffer_object
*bufObj
,
248 GLintptr offset
, GLsizeiptr size
,
249 bool mappedRange
, const char *caller
)
252 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
257 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
261 if (offset
+ size
> bufObj
->Size
) {
262 _mesa_error(ctx
, GL_INVALID_VALUE
,
263 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
264 (unsigned long) offset
,
265 (unsigned long) size
,
266 (unsigned long) bufObj
->Size
);
270 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
274 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
275 _mesa_error(ctx
, GL_INVALID_OPERATION
,
276 "%s(range is mapped without persistent bit)",
282 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
283 _mesa_error(ctx
, GL_INVALID_OPERATION
,
284 "%s(buffer is mapped without persistent bit)",
295 * Test the format and type parameters and set the GL error code for
296 * \c glClearBufferData and \c glClearBufferSubData.
298 * \param ctx GL context.
299 * \param internalformat Format to which the data is to be converted.
300 * \param format Format of the supplied data.
301 * \param type Type of the supplied data.
302 * \param caller Name of calling function for recording errors.
303 * \return If internalformat, format and type are legal the mesa_format
304 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
306 * \sa glClearBufferData and glClearBufferSubData
309 validate_clear_buffer_format(struct gl_context
*ctx
,
310 GLenum internalformat
,
311 GLenum format
, GLenum type
,
314 mesa_format mesaFormat
;
315 GLenum errorFormatType
;
317 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
318 if (mesaFormat
== MESA_FORMAT_NONE
) {
319 _mesa_error(ctx
, GL_INVALID_ENUM
,
320 "%s(invalid internalformat)", caller
);
321 return MESA_FORMAT_NONE
;
324 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
325 * EXT_texture_integer there is no conversion between integer and
326 * non-integer formats
328 if (_mesa_is_enum_format_signed_int(format
) !=
329 _mesa_is_format_integer_color(mesaFormat
)) {
330 _mesa_error(ctx
, GL_INVALID_OPERATION
,
331 "%s(integer vs non-integer)", caller
);
332 return MESA_FORMAT_NONE
;
335 if (!_mesa_is_color_format(format
)) {
336 _mesa_error(ctx
, GL_INVALID_ENUM
,
337 "%s(format is not a color format)", caller
);
338 return MESA_FORMAT_NONE
;
341 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
342 if (errorFormatType
!= GL_NO_ERROR
) {
343 _mesa_error(ctx
, GL_INVALID_ENUM
,
344 "%s(invalid format or type)", caller
);
345 return MESA_FORMAT_NONE
;
353 * Convert user-specified clear value to the specified internal format.
355 * \param ctx GL context.
356 * \param internalformat Format to which the data is converted.
357 * \param clearValue Points to the converted clear value.
358 * \param format Format of the supplied data.
359 * \param type Type of the supplied data.
360 * \param data Data which is to be converted to internalformat.
361 * \param caller Name of calling function for recording errors.
362 * \return true if data could be converted, false otherwise.
364 * \sa glClearBufferData, glClearBufferSubData
367 convert_clear_buffer_data(struct gl_context
*ctx
,
368 mesa_format internalformat
,
369 GLubyte
*clearValue
, GLenum format
, GLenum type
,
370 const GLvoid
*data
, const char *caller
)
372 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
374 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
375 0, &clearValue
, 1, 1, 1,
376 format
, type
, data
, &ctx
->Unpack
)) {
380 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
387 * Allocate and initialize a new buffer object.
389 * Default callback for the \c dd_function_table::NewBufferObject() hook.
391 static struct gl_buffer_object
*
392 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
394 struct gl_buffer_object
*obj
;
398 obj
= MALLOC_STRUCT(gl_buffer_object
);
399 _mesa_initialize_buffer_object(ctx
, obj
, name
);
405 * Delete a buffer object.
407 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
410 _mesa_delete_buffer_object(struct gl_context
*ctx
,
411 struct gl_buffer_object
*bufObj
)
415 _mesa_align_free(bufObj
->Data
);
417 /* assign strange values here to help w/ debugging */
418 bufObj
->RefCount
= -1000;
421 mtx_destroy(&bufObj
->Mutex
);
429 * Set ptr to bufObj w/ reference counting.
430 * This is normally only called from the _mesa_reference_buffer_object() macro
431 * when there's a real pointer change.
434 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
435 struct gl_buffer_object
**ptr
,
436 struct gl_buffer_object
*bufObj
)
439 /* Unreference the old buffer */
440 GLboolean deleteFlag
= GL_FALSE
;
441 struct gl_buffer_object
*oldObj
= *ptr
;
443 mtx_lock(&oldObj
->Mutex
);
444 assert(oldObj
->RefCount
> 0);
447 printf("BufferObj %p %d DECR to %d\n",
448 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
450 deleteFlag
= (oldObj
->RefCount
== 0);
451 mtx_unlock(&oldObj
->Mutex
);
455 /* some sanity checking: don't delete a buffer still in use */
457 /* unfortunately, these tests are invalid during context tear-down */
458 assert(ctx
->Array
.ArrayBufferObj
!= bufObj
);
459 assert(ctx
->Array
.VAO
->IndexBufferObj
!= bufObj
);
460 assert(ctx
->Array
.VAO
->Vertex
.BufferObj
!= bufObj
);
463 assert(ctx
->Driver
.DeleteBuffer
);
464 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
472 /* reference new buffer */
473 mtx_lock(&bufObj
->Mutex
);
474 if (bufObj
->RefCount
== 0) {
475 /* this buffer's being deleted (look just above) */
476 /* Not sure this can every really happen. Warn if it does. */
477 _mesa_problem(NULL
, "referencing deleted buffer object");
483 printf("BufferObj %p %d INCR to %d\n",
484 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
488 mtx_unlock(&bufObj
->Mutex
);
494 * Initialize a buffer object to default values.
497 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
498 struct gl_buffer_object
*obj
,
501 memset(obj
, 0, sizeof(struct gl_buffer_object
));
502 mtx_init(&obj
->Mutex
, mtx_plain
);
505 obj
->Usage
= GL_STATIC_DRAW_ARB
;
511 * Callback called from _mesa_HashWalk()
514 count_buffer_size(GLuint key
, void *data
, void *userData
)
516 const struct gl_buffer_object
*bufObj
=
517 (const struct gl_buffer_object
*) data
;
518 GLuint
*total
= (GLuint
*) userData
;
520 *total
= *total
+ bufObj
->Size
;
525 * Compute total size (in bytes) of all buffer objects for the given context.
526 * For debugging purposes.
529 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
533 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
540 * Allocate space for and store data in a buffer object. Any data that was
541 * previously stored in the buffer object is lost. If \c data is \c NULL,
542 * memory will be allocated, but no copy will occur.
544 * This is the default callback for \c dd_function_table::BufferData()
545 * Note that all GL error checking will have been done already.
547 * \param ctx GL context.
548 * \param target Buffer object target on which to operate.
549 * \param size Size, in bytes, of the new data store.
550 * \param data Pointer to the data to store in the buffer object. This
551 * pointer may be \c NULL.
552 * \param usage Hints about how the data will be used.
553 * \param bufObj Object to be used.
555 * \return GL_TRUE for success, GL_FALSE for failure
556 * \sa glBufferDataARB, dd_function_table::BufferData.
559 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptr size
,
560 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
561 struct gl_buffer_object
*bufObj
)
567 _mesa_align_free( bufObj
->Data
);
569 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
571 bufObj
->Data
= (GLubyte
*) new_data
;
573 bufObj
->Usage
= usage
;
574 bufObj
->StorageFlags
= storageFlags
;
577 memcpy( bufObj
->Data
, data
, size
);
589 * Replace data in a subrange of buffer object. If the data range
590 * specified by \c size + \c offset extends beyond the end of the buffer or
591 * if \c data is \c NULL, no copy is performed.
593 * This is the default callback for \c dd_function_table::BufferSubData()
594 * Note that all GL error checking will have been done already.
596 * \param ctx GL context.
597 * \param offset Offset of the first byte to be modified.
598 * \param size Size, in bytes, of the data range.
599 * \param data Pointer to the data to store in the buffer object.
600 * \param bufObj Object to be used.
602 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
605 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptr offset
,
606 GLsizeiptr size
, const GLvoid
*data
,
607 struct gl_buffer_object
*bufObj
)
611 /* this should have been caught in _mesa_BufferSubData() */
612 assert(size
+ offset
<= bufObj
->Size
);
615 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
621 * Retrieve data from a subrange of buffer object. If the data range
622 * specified by \c size + \c offset extends beyond the end of the buffer or
623 * if \c data is \c NULL, no copy is performed.
625 * This is the default callback for \c dd_function_table::GetBufferSubData()
626 * Note that all GL error checking will have been done already.
628 * \param ctx GL context.
629 * \param target Buffer object target on which to operate.
630 * \param offset Offset of the first byte to be fetched.
631 * \param size Size, in bytes, of the data range.
632 * \param data Destination for data
633 * \param bufObj Object to be used.
635 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
638 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
639 GLsizeiptrARB size
, GLvoid
* data
,
640 struct gl_buffer_object
* bufObj
)
644 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
645 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
651 * Clear a subrange of the buffer object with copies of the supplied data.
652 * If data is NULL the buffer is filled with zeros.
654 * This is the default callback for \c dd_function_table::ClearBufferSubData()
655 * Note that all GL error checking will have been done already.
657 * \param ctx GL context.
658 * \param offset Offset of the first byte to be cleared.
659 * \param size Size, in bytes, of the to be cleared range.
660 * \param clearValue Source of the data.
661 * \param clearValueSize Size, in bytes, of the supplied data.
662 * \param bufObj Object to be cleared.
664 * \sa glClearBufferSubData, glClearBufferData and
665 * dd_function_table::ClearBufferSubData.
668 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
669 GLintptr offset
, GLsizeiptr size
,
670 const GLvoid
*clearValue
,
671 GLsizeiptr clearValueSize
,
672 struct gl_buffer_object
*bufObj
)
677 assert(ctx
->Driver
.MapBufferRange
);
678 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
680 GL_MAP_INVALIDATE_RANGE_BIT
,
681 bufObj
, MAP_INTERNAL
);
684 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
688 if (clearValue
== NULL
) {
689 /* Clear with zeros, per the spec */
690 memset(dest
, 0, size
);
691 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
695 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
696 memcpy(dest
, clearValue
, clearValueSize
);
697 dest
+= clearValueSize
;
700 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
705 * Default fallback for \c dd_function_table::MapBufferRange().
706 * Called via glMapBufferRange().
709 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
710 GLsizeiptr length
, GLbitfield access
,
711 struct gl_buffer_object
*bufObj
,
712 gl_map_buffer_index index
)
715 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
716 /* Just return a direct pointer to the data */
717 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
718 bufObj
->Mappings
[index
].Length
= length
;
719 bufObj
->Mappings
[index
].Offset
= offset
;
720 bufObj
->Mappings
[index
].AccessFlags
= access
;
721 return bufObj
->Mappings
[index
].Pointer
;
726 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
727 * Called via glFlushMappedBufferRange().
730 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
731 GLintptr offset
, GLsizeiptr length
,
732 struct gl_buffer_object
*obj
,
733 gl_map_buffer_index index
)
744 * Default callback for \c dd_function_table::UnmapBuffer().
746 * The input parameters will have been already tested for errors.
748 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
751 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
752 gl_map_buffer_index index
)
755 /* XXX we might assert here that bufObj->Pointer is non-null */
756 bufObj
->Mappings
[index
].Pointer
= NULL
;
757 bufObj
->Mappings
[index
].Length
= 0;
758 bufObj
->Mappings
[index
].Offset
= 0;
759 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
765 * Default fallback for \c dd_function_table::CopyBufferSubData().
766 * Called via glCopyBufferSubData().
769 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
770 struct gl_buffer_object
*src
,
771 struct gl_buffer_object
*dst
,
772 GLintptr readOffset
, GLintptr writeOffset
,
775 GLubyte
*srcPtr
, *dstPtr
;
778 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
780 GL_MAP_WRITE_BIT
, src
,
786 srcPtr
+= readOffset
;
787 dstPtr
+= writeOffset
;
789 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
790 GL_MAP_READ_BIT
, src
,
792 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
794 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
798 /* Note: the src and dst regions will never overlap. Trying to do so
799 * would generate GL_INVALID_VALUE earlier.
801 if (srcPtr
&& dstPtr
)
802 memcpy(dstPtr
, srcPtr
, size
);
804 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
806 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
812 * Initialize the state associated with buffer objects
815 _mesa_init_buffer_objects( struct gl_context
*ctx
)
819 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
820 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
821 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
823 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
824 ctx
->Shared
->NullBufferObj
);
826 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
827 ctx
->Shared
->NullBufferObj
);
828 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
829 ctx
->Shared
->NullBufferObj
);
831 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
832 ctx
->Shared
->NullBufferObj
);
834 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
835 ctx
->Shared
->NullBufferObj
);
837 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
838 ctx
->Shared
->NullBufferObj
);
840 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
841 _mesa_reference_buffer_object(ctx
,
842 &ctx
->UniformBufferBindings
[i
].BufferObject
,
843 ctx
->Shared
->NullBufferObj
);
844 ctx
->UniformBufferBindings
[i
].Offset
= -1;
845 ctx
->UniformBufferBindings
[i
].Size
= -1;
848 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
849 _mesa_reference_buffer_object(ctx
,
850 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
851 ctx
->Shared
->NullBufferObj
);
852 ctx
->AtomicBufferBindings
[i
].Offset
= -1;
853 ctx
->AtomicBufferBindings
[i
].Size
= -1;
859 _mesa_free_buffer_objects( struct gl_context
*ctx
)
863 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
865 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
866 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
868 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
870 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
872 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
874 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
875 _mesa_reference_buffer_object(ctx
,
876 &ctx
->UniformBufferBindings
[i
].BufferObject
,
880 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
881 _mesa_reference_buffer_object(ctx
,
882 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
889 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
892 struct gl_buffer_object
**buf_handle
,
895 struct gl_buffer_object
*buf
= *buf_handle
;
897 if (!buf
&& ctx
->API
== API_OPENGL_CORE
) {
898 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
902 if (!buf
|| buf
== &DummyBufferObject
) {
903 /* If this is a new buffer object id, or one which was generated but
904 * never used before, allocate a buffer object now.
906 assert(ctx
->Driver
.NewBufferObject
);
907 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
909 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
912 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
920 * Bind the specified target to buffer for the specified context.
921 * Called by glBindBuffer() and other functions.
924 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
926 struct gl_buffer_object
*oldBufObj
;
927 struct gl_buffer_object
*newBufObj
= NULL
;
928 struct gl_buffer_object
**bindTarget
= NULL
;
930 bindTarget
= get_buffer_target(ctx
, target
);
932 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
936 /* Get pointer to old buffer object (to be unbound) */
937 oldBufObj
= *bindTarget
;
938 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
939 return; /* rebinding the same buffer object- no change */
942 * Get pointer to new buffer object (newBufObj)
945 /* The spec says there's not a buffer object named 0, but we use
946 * one internally because it simplifies things.
948 newBufObj
= ctx
->Shared
->NullBufferObj
;
951 /* non-default buffer object */
952 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
953 if (!_mesa_handle_bind_buffer_gen(ctx
, target
, buffer
,
954 &newBufObj
, "glBindBuffer"))
958 /* bind new buffer */
959 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
964 * Update the default buffer objects in the given context to reference those
965 * specified in the shared state and release those referencing the old
969 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
971 /* Bind the NullBufferObj to remove references to those
972 * in the shared context hash table.
974 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
975 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
976 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
977 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
983 * Return the gl_buffer_object for the given ID.
984 * Always return NULL for ID 0.
986 struct gl_buffer_object
*
987 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
992 return (struct gl_buffer_object
*)
993 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
997 struct gl_buffer_object
*
998 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1000 return (struct gl_buffer_object
*)
1001 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1005 * A convenience function for direct state access functions that throws
1006 * GL_INVALID_OPERATION if buffer is not the name of a buffer object in the
1009 struct gl_buffer_object
*
1010 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1013 struct gl_buffer_object
*bufObj
;
1015 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1017 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1018 "%s(non-generated buffer name %u)", caller
, buffer
);
1025 _mesa_begin_bufferobj_lookups(struct gl_context
*ctx
)
1027 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1032 _mesa_end_bufferobj_lookups(struct gl_context
*ctx
)
1034 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1039 * Look up a buffer object for a multi-bind function.
1041 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1042 * of generating an error if the buffer ID is not zero or the name
1043 * of an existing buffer object.
1045 * If the buffer ID refers to an existing buffer object, a pointer
1046 * to the buffer object is returned. If the ID is zero, a pointer
1047 * to the shared NullBufferObj is returned. If the ID is not zero
1048 * and does not refer to a valid buffer object, this function
1051 * This function assumes that the caller has already locked the
1052 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1054 struct gl_buffer_object
*
1055 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1056 const GLuint
*buffers
,
1057 GLuint index
, const char *caller
)
1059 struct gl_buffer_object
*bufObj
;
1061 if (buffers
[index
] != 0) {
1062 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1064 /* The multi-bind functions don't create the buffer objects
1065 when they don't exist. */
1066 if (bufObj
== &DummyBufferObject
)
1069 bufObj
= ctx
->Shared
->NullBufferObj
;
1072 /* The ARB_multi_bind spec says:
1074 * "An INVALID_OPERATION error is generated if any value
1075 * in <buffers> is not zero or the name of an existing
1076 * buffer object (per binding)."
1078 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1079 "%s(buffers[%u]=%u is not zero or the name "
1080 "of an existing buffer object)",
1081 caller
, index
, buffers
[index
]);
1089 * If *ptr points to obj, set ptr = the Null/default buffer object.
1090 * This is a helper for buffer object deletion.
1091 * The GL spec says that deleting a buffer object causes it to get
1092 * unbound from all arrays in the current context.
1095 unbind(struct gl_context
*ctx
,
1096 struct gl_buffer_object
**ptr
,
1097 struct gl_buffer_object
*obj
)
1100 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
1106 * Plug default/fallback buffer object functions into the device
1110 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1112 /* GL_ARB_vertex/pixel_buffer_object */
1113 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1114 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1115 driver
->BufferData
= buffer_data_fallback
;
1116 driver
->BufferSubData
= buffer_sub_data_fallback
;
1117 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1118 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1120 /* GL_ARB_clear_buffer_object */
1121 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1123 /* GL_ARB_map_buffer_range */
1124 driver
->MapBufferRange
= map_buffer_range_fallback
;
1125 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1127 /* GL_ARB_copy_buffer */
1128 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1133 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1134 struct gl_buffer_object
*bufObj
)
1138 for (i
= 0; i
< MAP_COUNT
; i
++) {
1139 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1140 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1141 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1142 bufObj
->Mappings
[i
].AccessFlags
= 0;
1148 /**********************************************************************/
1150 /**********************************************************************/
1153 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1155 GET_CURRENT_CONTEXT(ctx
);
1157 if (MESA_VERBOSE
& VERBOSE_API
)
1158 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1159 _mesa_lookup_enum_by_nr(target
), buffer
);
1161 bind_buffer_object(ctx
, target
, buffer
);
1166 * Delete a set of buffer objects.
1168 * \param n Number of buffer objects to delete.
1169 * \param ids Array of \c n buffer object IDs.
1172 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1174 GET_CURRENT_CONTEXT(ctx
);
1176 FLUSH_VERTICES(ctx
, 0);
1179 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1183 mtx_lock(&ctx
->Shared
->Mutex
);
1185 for (i
= 0; i
< n
; i
++) {
1186 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
1188 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1191 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1193 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1195 /* unbind any vertex pointers bound to this buffer */
1196 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1197 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1200 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1201 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1203 if (vao
->IndexBufferObj
== bufObj
) {
1204 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1207 /* unbind ARB_draw_indirect binding point */
1208 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1209 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1212 /* unbind ARB_copy_buffer binding points */
1213 if (ctx
->CopyReadBuffer
== bufObj
) {
1214 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1216 if (ctx
->CopyWriteBuffer
== bufObj
) {
1217 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1220 /* unbind transform feedback binding points */
1221 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1222 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1224 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1225 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1226 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1230 /* unbind UBO binding points */
1231 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1232 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1233 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1237 if (ctx
->UniformBuffer
== bufObj
) {
1238 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1241 /* unbind Atomci Buffer binding points */
1242 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1243 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1244 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1248 if (ctx
->AtomicBuffer
== bufObj
) {
1249 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1252 /* unbind any pixel pack/unpack pointers bound to this buffer */
1253 if (ctx
->Pack
.BufferObj
== bufObj
) {
1254 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1256 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1257 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1260 if (ctx
->Texture
.BufferObject
== bufObj
) {
1261 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1264 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1265 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1268 /* The ID is immediately freed for re-use */
1269 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1270 /* Make sure we do not run into the classic ABA problem on bind.
1271 * We don't want to allow re-binding a buffer object that's been
1272 * "deleted" by glDeleteBuffers().
1274 * The explicit rebinding to the default object in the current context
1275 * prevents the above in the current context, but another context
1276 * sharing the same objects might suffer from this problem.
1277 * The alternative would be to do the hash lookup in any case on bind
1278 * which would introduce more runtime overhead than this.
1280 bufObj
->DeletePending
= GL_TRUE
;
1281 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1285 mtx_unlock(&ctx
->Shared
->Mutex
);
1290 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1291 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1295 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1297 GET_CURRENT_CONTEXT(ctx
);
1300 struct gl_buffer_object
*buf
;
1302 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1304 if (MESA_VERBOSE
& VERBOSE_API
)
1305 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1308 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1317 * This must be atomic (generation and allocation of buffer object IDs)
1319 mtx_lock(&ctx
->Shared
->Mutex
);
1321 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1323 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1324 * DummyBufferObject. Otherwise, create a new buffer object and insert
1327 for (i
= 0; i
< n
; i
++) {
1328 buffers
[i
] = first
+ i
;
1330 assert(ctx
->Driver
.NewBufferObject
);
1331 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1333 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1338 buf
= &DummyBufferObject
;
1340 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1343 mtx_unlock(&ctx
->Shared
->Mutex
);
1347 * Generate a set of unique buffer object IDs and store them in \c buffers.
1349 * \param n Number of IDs to generate.
1350 * \param buffers Array of \c n locations to store the IDs.
1353 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1355 create_buffers(n
, buffers
, false);
1359 * Create a set of buffer objects and store their unique IDs in \c buffers.
1361 * \param n Number of IDs to generate.
1362 * \param buffers Array of \c n locations to store the IDs.
1365 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1367 create_buffers(n
, buffers
, true);
1372 * Determine if ID is the name of a buffer object.
1374 * \param id ID of the potential buffer object.
1375 * \return \c GL_TRUE if \c id is the name of a buffer object,
1376 * \c GL_FALSE otherwise.
1378 GLboolean GLAPIENTRY
1379 _mesa_IsBuffer(GLuint id
)
1381 struct gl_buffer_object
*bufObj
;
1382 GET_CURRENT_CONTEXT(ctx
);
1383 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1385 mtx_lock(&ctx
->Shared
->Mutex
);
1386 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1387 mtx_unlock(&ctx
->Shared
->Mutex
);
1389 return bufObj
&& bufObj
!= &DummyBufferObject
;
1394 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1395 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1396 GLbitfield flags
, const char *func
)
1399 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1403 if (flags
& ~(GL_MAP_READ_BIT
|
1405 GL_MAP_PERSISTENT_BIT
|
1406 GL_MAP_COHERENT_BIT
|
1407 GL_DYNAMIC_STORAGE_BIT
|
1408 GL_CLIENT_STORAGE_BIT
)) {
1409 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1413 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1414 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1415 _mesa_error(ctx
, GL_INVALID_VALUE
,
1416 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1420 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1421 _mesa_error(ctx
, GL_INVALID_VALUE
,
1422 "%s(COHERENT and flags!=PERSISTENT)", func
);
1426 if (bufObj
->Immutable
) {
1427 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1431 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1432 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1434 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1436 bufObj
->Written
= GL_TRUE
;
1437 bufObj
->Immutable
= GL_TRUE
;
1439 assert(ctx
->Driver
.BufferData
);
1440 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1442 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1443 /* Even though the interaction between AMD_pinned_memory and
1444 * glBufferStorage is not described in the spec, Graham Sellers
1445 * said that it should behave the same as glBufferData.
1447 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1450 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1456 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1459 GET_CURRENT_CONTEXT(ctx
);
1460 struct gl_buffer_object
*bufObj
;
1462 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1466 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1471 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1474 GET_CURRENT_CONTEXT(ctx
);
1475 struct gl_buffer_object
*bufObj
;
1477 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1482 * In direct state access, buffer objects have an unspecified target since
1483 * they are not required to be bound.
1485 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1486 "glNamedBufferStorage");
1491 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1492 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1493 GLenum usage
, const char *func
)
1497 if (MESA_VERBOSE
& VERBOSE_API
)
1498 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1500 _mesa_lookup_enum_by_nr(target
),
1501 (long int) size
, data
,
1502 _mesa_lookup_enum_by_nr(usage
));
1505 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1510 case GL_STREAM_DRAW_ARB
:
1511 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1514 case GL_STATIC_DRAW_ARB
:
1515 case GL_DYNAMIC_DRAW_ARB
:
1519 case GL_STREAM_READ_ARB
:
1520 case GL_STREAM_COPY_ARB
:
1521 case GL_STATIC_READ_ARB
:
1522 case GL_STATIC_COPY_ARB
:
1523 case GL_DYNAMIC_READ_ARB
:
1524 case GL_DYNAMIC_COPY_ARB
:
1525 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1529 valid_usage
= false;
1534 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1535 _mesa_lookup_enum_by_nr(usage
));
1539 if (bufObj
->Immutable
) {
1540 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1544 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1545 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1547 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1549 bufObj
->Written
= GL_TRUE
;
1552 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1553 bufObj
->Name
, size
, data
, usage
);
1560 assert(ctx
->Driver
.BufferData
);
1561 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1564 GL_DYNAMIC_STORAGE_BIT
,
1566 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1567 /* From GL_AMD_pinned_memory:
1569 * INVALID_OPERATION is generated by BufferData if <target> is
1570 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1571 * mapped to the GPU address space.
1573 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1576 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1582 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1583 const GLvoid
*data
, GLenum usage
)
1585 GET_CURRENT_CONTEXT(ctx
);
1586 struct gl_buffer_object
*bufObj
;
1588 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1592 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1597 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1600 GET_CURRENT_CONTEXT(ctx
);
1601 struct gl_buffer_object
*bufObj
;
1603 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1607 /* In direct state access, buffer objects have an unspecified target since
1608 * they are not required to be bound.
1610 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1611 "glNamedBufferData");
1616 * Implementation for glBufferSubData and glNamedBufferSubData.
1618 * \param ctx GL context.
1619 * \param bufObj The buffer object.
1620 * \param offset Offset of the first byte of the subdata range.
1621 * \param size Size, in bytes, of the subdata range.
1622 * \param data The data store.
1623 * \param func Name of calling function for recording errors.
1627 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1628 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1631 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1633 /* error already recorded */
1637 if (bufObj
->Immutable
&&
1638 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1639 _mesa_error(ctx
, GL_INVALID_OPERATION
, func
);
1646 bufObj
->Written
= GL_TRUE
;
1648 assert(ctx
->Driver
.BufferSubData
);
1649 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1653 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1654 GLsizeiptr size
, const GLvoid
*data
)
1656 GET_CURRENT_CONTEXT(ctx
);
1657 struct gl_buffer_object
*bufObj
;
1659 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1663 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1667 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1668 GLsizeiptr size
, const GLvoid
*data
)
1670 GET_CURRENT_CONTEXT(ctx
);
1671 struct gl_buffer_object
*bufObj
;
1673 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1677 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1678 "glNamedBufferSubData");
1683 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1684 GLsizeiptr size
, GLvoid
*data
)
1686 GET_CURRENT_CONTEXT(ctx
);
1687 struct gl_buffer_object
*bufObj
;
1689 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1690 GL_INVALID_OPERATION
);
1694 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1695 "glGetBufferSubData")) {
1699 assert(ctx
->Driver
.GetBufferSubData
);
1700 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1704 * \param subdata true if caller is *SubData, false if *Data
1707 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1708 struct gl_buffer_object
*bufObj
,
1709 GLenum internalformat
,
1710 GLintptr offset
, GLsizeiptr size
,
1711 GLenum format
, GLenum type
,
1713 const char *func
, bool subdata
)
1715 mesa_format mesaFormat
;
1716 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1717 GLsizeiptr clearValueSize
;
1719 /* This checks for disallowed mappings. */
1720 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1725 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1726 format
, type
, func
);
1728 if (mesaFormat
== MESA_FORMAT_NONE
) {
1732 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1733 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1734 _mesa_error(ctx
, GL_INVALID_VALUE
,
1735 "%s(offset or size is not a multiple of "
1736 "internalformat size)", func
);
1741 /* clear to zeros, per the spec */
1743 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1744 NULL
, clearValueSize
, bufObj
);
1749 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1750 format
, type
, data
, func
)) {
1755 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1756 clearValue
, clearValueSize
, bufObj
);
1761 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1762 GLenum type
, const GLvoid
*data
)
1764 GET_CURRENT_CONTEXT(ctx
);
1765 struct gl_buffer_object
*bufObj
;
1767 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1771 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1773 "glClearBufferData", false);
1777 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1778 GLenum format
, GLenum type
, const GLvoid
*data
)
1780 GET_CURRENT_CONTEXT(ctx
);
1781 struct gl_buffer_object
*bufObj
;
1783 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1787 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1789 "glClearNamedBufferData", false);
1794 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1795 GLintptr offset
, GLsizeiptr size
,
1796 GLenum format
, GLenum type
,
1799 GET_CURRENT_CONTEXT(ctx
);
1800 struct gl_buffer_object
*bufObj
;
1802 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1806 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1808 "glClearBufferSubData", true);
1812 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1813 GLintptr offset
, GLsizeiptr size
,
1814 GLenum format
, GLenum type
,
1817 GET_CURRENT_CONTEXT(ctx
);
1818 struct gl_buffer_object
*bufObj
;
1820 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1821 "glClearNamedBufferSubData");
1825 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1827 "glClearNamedBufferSubData", true);
1832 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1835 GLboolean status
= GL_TRUE
;
1836 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1838 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
1839 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1840 "%s(buffer is not mapped)", func
);
1845 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1846 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1848 /* check that last 100 bytes are still = magic value */
1849 for (i
= 0; i
< 100; i
++) {
1850 GLuint pos
= bufObj
->Size
- i
- 1;
1851 if (buf
[pos
] != 123) {
1852 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1853 " at position %d (value = %u)\n",
1861 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1862 GLuint i
, unchanged
= 0;
1863 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1865 /* check which bytes changed */
1866 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1867 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1874 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1875 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1880 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
1881 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
1882 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
1883 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
1884 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
1889 GLboolean GLAPIENTRY
1890 _mesa_UnmapBuffer(GLenum target
)
1892 GET_CURRENT_CONTEXT(ctx
);
1893 struct gl_buffer_object
*bufObj
;
1895 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
1899 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
1902 GLboolean GLAPIENTRY
1903 _mesa_UnmapNamedBuffer(GLuint buffer
)
1905 GET_CURRENT_CONTEXT(ctx
);
1906 struct gl_buffer_object
*bufObj
;
1908 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
1912 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
1917 get_buffer_parameter(struct gl_context
*ctx
,
1918 struct gl_buffer_object
*bufObj
, GLenum pname
,
1919 GLint64
*params
, const char *func
)
1922 case GL_BUFFER_SIZE_ARB
:
1923 *params
= bufObj
->Size
;
1925 case GL_BUFFER_USAGE_ARB
:
1926 *params
= bufObj
->Usage
;
1928 case GL_BUFFER_ACCESS_ARB
:
1929 *params
= simplified_access_mode(ctx
,
1930 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
1932 case GL_BUFFER_MAPPED_ARB
:
1933 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
1935 case GL_BUFFER_ACCESS_FLAGS
:
1936 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1938 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
1940 case GL_BUFFER_MAP_OFFSET
:
1941 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1943 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
1945 case GL_BUFFER_MAP_LENGTH
:
1946 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1948 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
1950 case GL_BUFFER_IMMUTABLE_STORAGE
:
1951 if (!ctx
->Extensions
.ARB_buffer_storage
)
1953 *params
= bufObj
->Immutable
;
1955 case GL_BUFFER_STORAGE_FLAGS
:
1956 if (!ctx
->Extensions
.ARB_buffer_storage
)
1958 *params
= bufObj
->StorageFlags
;
1967 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
1968 _mesa_lookup_enum_by_nr(pname
));
1973 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1975 GET_CURRENT_CONTEXT(ctx
);
1976 struct gl_buffer_object
*bufObj
;
1979 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
1980 GL_INVALID_OPERATION
);
1984 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
1985 "glGetBufferParameteriv"))
1986 return; /* Error already recorded. */
1988 *params
= (GLint
) parameter
;
1992 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1994 GET_CURRENT_CONTEXT(ctx
);
1995 struct gl_buffer_object
*bufObj
;
1998 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
1999 GL_INVALID_OPERATION
);
2003 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2004 "glGetBufferParameteri64v"))
2005 return; /* Error already recorded. */
2007 *params
= parameter
;
2011 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2013 GET_CURRENT_CONTEXT(ctx
);
2014 struct gl_buffer_object
*bufObj
;
2017 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2018 "glGetNamedBufferParameteriv");
2022 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2023 "glGetNamedBufferParameteriv"))
2024 return; /* Error already recorded. */
2026 *params
= (GLint
) parameter
;
2030 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2033 GET_CURRENT_CONTEXT(ctx
);
2034 struct gl_buffer_object
*bufObj
;
2037 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2038 "glGetNamedBufferParameteri64v");
2042 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2043 "glGetNamedBufferParameteri64v"))
2044 return; /* Error already recorded. */
2046 *params
= parameter
;
2051 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2053 GET_CURRENT_CONTEXT(ctx
);
2054 struct gl_buffer_object
*bufObj
;
2056 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2057 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2058 "GL_BUFFER_MAP_POINTER)");
2062 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2063 GL_INVALID_OPERATION
);
2067 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2071 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2073 GET_CURRENT_CONTEXT(ctx
);
2074 struct gl_buffer_object
*bufObj
;
2076 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2077 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2078 "GL_BUFFER_MAP_POINTER)");
2082 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2083 "glGetNamedBufferPointerv");
2087 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2092 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2093 struct gl_buffer_object
*src
,
2094 struct gl_buffer_object
*dst
,
2095 GLintptr readOffset
, GLintptr writeOffset
,
2096 GLsizeiptr size
, const char *func
)
2098 if (_mesa_check_disallowed_mapping(src
)) {
2099 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2100 "%s(readBuffer is mapped)", func
);
2104 if (_mesa_check_disallowed_mapping(dst
)) {
2105 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2106 "%s(writeBuffer is mapped)", func
);
2110 if (readOffset
< 0) {
2111 _mesa_error(ctx
, GL_INVALID_VALUE
,
2112 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2116 if (writeOffset
< 0) {
2117 _mesa_error(ctx
, GL_INVALID_VALUE
,
2118 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2123 _mesa_error(ctx
, GL_INVALID_VALUE
,
2124 "%s(size %d < 0)", func
, (int) size
);
2128 if (readOffset
+ size
> src
->Size
) {
2129 _mesa_error(ctx
, GL_INVALID_VALUE
,
2130 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2131 (int) readOffset
, (int) size
, (int) src
->Size
);
2135 if (writeOffset
+ size
> dst
->Size
) {
2136 _mesa_error(ctx
, GL_INVALID_VALUE
,
2137 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2138 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2143 if (readOffset
+ size
<= writeOffset
) {
2146 else if (writeOffset
+ size
<= readOffset
) {
2150 /* overlapping src/dst is illegal */
2151 _mesa_error(ctx
, GL_INVALID_VALUE
,
2152 "%s(overlapping src/dst)", func
);
2157 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2161 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2162 GLintptr readOffset
, GLintptr writeOffset
,
2165 GET_CURRENT_CONTEXT(ctx
);
2166 struct gl_buffer_object
*src
, *dst
;
2168 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2169 GL_INVALID_OPERATION
);
2173 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2174 GL_INVALID_OPERATION
);
2178 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2179 "glCopyBufferSubData");
2183 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2184 GLintptr readOffset
, GLintptr writeOffset
,
2187 GET_CURRENT_CONTEXT(ctx
);
2188 struct gl_buffer_object
*src
, *dst
;
2190 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2191 "glCopyNamedBufferSubData");
2195 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2196 "glCopyNamedBufferSubData");
2200 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2201 "glCopyNamedBufferSubData");
2206 _mesa_map_buffer_range(struct gl_context
*ctx
,
2207 struct gl_buffer_object
*bufObj
,
2208 GLintptr offset
, GLsizeiptr length
,
2209 GLbitfield access
, const char *func
)
2212 GLbitfield allowed_access
;
2214 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2217 _mesa_error(ctx
, GL_INVALID_VALUE
,
2218 "%s(offset %ld < 0)", func
, (long) offset
);
2223 _mesa_error(ctx
, GL_INVALID_VALUE
,
2224 "%s(length %ld < 0)", func
, (long) length
);
2228 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2230 * "An INVALID_OPERATION error is generated for any of the following
2233 * * <length> is zero."
2235 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2236 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2240 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2244 allowed_access
= GL_MAP_READ_BIT
|
2246 GL_MAP_INVALIDATE_RANGE_BIT
|
2247 GL_MAP_INVALIDATE_BUFFER_BIT
|
2248 GL_MAP_FLUSH_EXPLICIT_BIT
|
2249 GL_MAP_UNSYNCHRONIZED_BIT
;
2251 if (ctx
->Extensions
.ARB_buffer_storage
) {
2252 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2253 GL_MAP_COHERENT_BIT
;
2256 if (access
& ~allowed_access
) {
2257 /* generate an error if any bits other than those allowed are set */
2258 _mesa_error(ctx
, GL_INVALID_VALUE
,
2259 "%s(access has undefined bits set)", func
);
2263 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2264 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2265 "%s(access indicates neither read or write)", func
);
2269 if ((access
& GL_MAP_READ_BIT
) &&
2270 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2271 GL_MAP_INVALIDATE_BUFFER_BIT
|
2272 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2273 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2274 "%s(read access with disallowed bits)", func
);
2278 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2279 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2280 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2281 "%s(access has flush explicit without write)", func
);
2285 if (access
& GL_MAP_READ_BIT
&&
2286 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2287 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2288 "%s(buffer does not allow read access)", func
);
2292 if (access
& GL_MAP_WRITE_BIT
&&
2293 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2294 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2295 "%s(buffer does not allow write access)", func
);
2299 if (access
& GL_MAP_COHERENT_BIT
&&
2300 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2301 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2302 "%s(buffer does not allow coherent access)", func
);
2306 if (access
& GL_MAP_PERSISTENT_BIT
&&
2307 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2308 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2309 "%s(buffer does not allow persistent access)", func
);
2313 if (offset
+ length
> bufObj
->Size
) {
2314 _mesa_error(ctx
, GL_INVALID_VALUE
,
2315 "%s(offset %ld + length %ld > buffer_size %ld)", func
,
2316 offset
, length
, bufObj
->Size
);
2320 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2321 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2322 "%s(buffer already mapped)", func
);
2326 if (!bufObj
->Size
) {
2327 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2332 assert(ctx
->Driver
.MapBufferRange
);
2333 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2336 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2339 /* The driver callback should have set all these fields.
2340 * This is important because other modules (like VBO) might call
2341 * the driver function directly.
2343 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2344 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2345 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2346 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2349 if (access
& GL_MAP_WRITE_BIT
)
2350 bufObj
->Written
= GL_TRUE
;
2353 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2354 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2355 bufObj
->Name
, bufObj
->Size
, access
);
2356 /* Access must be write only */
2357 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2359 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2360 for (i
= 0; i
< bufObj
->Size
; i
++)
2367 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2368 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2370 /* buffer is 100 bytes larger than requested, fill with magic value */
2371 for (i
= 0; i
< 100; i
++) {
2372 buf
[bufObj
->Size
- i
- 1] = 123;
2381 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2384 GET_CURRENT_CONTEXT(ctx
);
2385 struct gl_buffer_object
*bufObj
;
2387 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2388 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2389 "glMapBufferRange(ARB_map_buffer_range not supported)");
2393 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2397 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2398 "glMapBufferRange");
2402 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2405 GET_CURRENT_CONTEXT(ctx
);
2406 struct gl_buffer_object
*bufObj
;
2408 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2409 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2410 "glMapNamedBufferRange("
2411 "ARB_map_buffer_range not supported)");
2415 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2419 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2420 "glMapNamedBufferRange");
2424 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2425 * flags for input to _mesa_map_buffer_range.
2427 * \return true if the type of requested access is permissible.
2430 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2434 case GL_READ_ONLY_ARB
:
2435 *flags
= GL_MAP_READ_BIT
;
2436 return _mesa_is_desktop_gl(ctx
);
2437 case GL_WRITE_ONLY_ARB
:
2438 *flags
= GL_MAP_WRITE_BIT
;
2440 case GL_READ_WRITE_ARB
:
2441 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2442 return _mesa_is_desktop_gl(ctx
);
2449 _mesa_MapBuffer(GLenum target
, GLenum access
)
2451 GET_CURRENT_CONTEXT(ctx
);
2452 struct gl_buffer_object
*bufObj
;
2453 GLbitfield accessFlags
;
2455 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2456 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2460 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2464 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2469 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2471 GET_CURRENT_CONTEXT(ctx
);
2472 struct gl_buffer_object
*bufObj
;
2473 GLbitfield accessFlags
;
2475 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2476 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2480 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2484 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2485 "glMapNamedBuffer");
2490 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2491 struct gl_buffer_object
*bufObj
,
2492 GLintptr offset
, GLsizeiptr length
,
2495 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2496 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2497 "%s(ARB_map_buffer_range not supported)", func
);
2502 _mesa_error(ctx
, GL_INVALID_VALUE
,
2503 "%s(offset %ld < 0)", func
, (long) offset
);
2508 _mesa_error(ctx
, GL_INVALID_VALUE
,
2509 "%s(length %ld < 0)", func
, (long) length
);
2513 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2514 /* buffer is not mapped */
2515 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2516 "%s(buffer is not mapped)", func
);
2520 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2521 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2522 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2523 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2527 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2528 _mesa_error(ctx
, GL_INVALID_VALUE
,
2529 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2530 (long) offset
, (long) length
,
2531 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2535 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2537 if (ctx
->Driver
.FlushMappedBufferRange
)
2538 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2543 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2546 GET_CURRENT_CONTEXT(ctx
);
2547 struct gl_buffer_object
*bufObj
;
2549 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2550 GL_INVALID_OPERATION
);
2554 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2555 "glFlushMappedBufferRange");
2559 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2562 GET_CURRENT_CONTEXT(ctx
);
2563 struct gl_buffer_object
*bufObj
;
2565 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2566 "glFlushMappedNamedBufferRange");
2570 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2571 "glFlushMappedNamedBufferRange");
2576 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2578 struct gl_buffer_object
*bufObj
;
2581 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2583 _mesa_error(ctx
, GL_INVALID_VALUE
,
2584 "glObjectPurgeable(name = 0x%x)", name
);
2587 if (!_mesa_is_bufferobj(bufObj
)) {
2588 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
2592 if (bufObj
->Purgeable
) {
2593 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2594 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
2595 return GL_VOLATILE_APPLE
;
2598 bufObj
->Purgeable
= GL_TRUE
;
2600 retval
= GL_VOLATILE_APPLE
;
2601 if (ctx
->Driver
.BufferObjectPurgeable
)
2602 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
2609 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2611 struct gl_renderbuffer
*bufObj
;
2614 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
2616 _mesa_error(ctx
, GL_INVALID_VALUE
,
2617 "glObjectUnpurgeable(name = 0x%x)", name
);
2621 if (bufObj
->Purgeable
) {
2622 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2623 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
2624 return GL_VOLATILE_APPLE
;
2627 bufObj
->Purgeable
= GL_TRUE
;
2629 retval
= GL_VOLATILE_APPLE
;
2630 if (ctx
->Driver
.RenderObjectPurgeable
)
2631 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
2638 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2640 struct gl_texture_object
*bufObj
;
2643 bufObj
= _mesa_lookup_texture(ctx
, name
);
2645 _mesa_error(ctx
, GL_INVALID_VALUE
,
2646 "glObjectPurgeable(name = 0x%x)", name
);
2650 if (bufObj
->Purgeable
) {
2651 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2652 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
2653 return GL_VOLATILE_APPLE
;
2656 bufObj
->Purgeable
= GL_TRUE
;
2658 retval
= GL_VOLATILE_APPLE
;
2659 if (ctx
->Driver
.TextureObjectPurgeable
)
2660 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
2667 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
2671 GET_CURRENT_CONTEXT(ctx
);
2672 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2675 _mesa_error(ctx
, GL_INVALID_VALUE
,
2676 "glObjectPurgeable(name = 0x%x)", name
);
2681 case GL_VOLATILE_APPLE
:
2682 case GL_RELEASED_APPLE
:
2686 _mesa_error(ctx
, GL_INVALID_ENUM
,
2687 "glObjectPurgeable(name = 0x%x) invalid option: %d",
2692 switch (objectType
) {
2694 retval
= texture_object_purgeable(ctx
, name
, option
);
2696 case GL_RENDERBUFFER_EXT
:
2697 retval
= renderbuffer_purgeable(ctx
, name
, option
);
2699 case GL_BUFFER_OBJECT_APPLE
:
2700 retval
= buffer_object_purgeable(ctx
, name
, option
);
2703 _mesa_error(ctx
, GL_INVALID_ENUM
,
2704 "glObjectPurgeable(name = 0x%x) invalid type: %d",
2709 /* In strict conformance to the spec, we must only return VOLATILE when
2710 * when passed the VOLATILE option. Madness.
2712 * XXX First fix the spec, then fix me.
2714 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
2719 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2721 struct gl_buffer_object
*bufObj
;
2724 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2726 _mesa_error(ctx
, GL_INVALID_VALUE
,
2727 "glObjectUnpurgeable(name = 0x%x)", name
);
2731 if (! bufObj
->Purgeable
) {
2732 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2733 "glObjectUnpurgeable(name = 0x%x) object is "
2734 " already \"unpurged\"", name
);
2738 bufObj
->Purgeable
= GL_FALSE
;
2741 if (ctx
->Driver
.BufferObjectUnpurgeable
)
2742 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
2749 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2751 struct gl_renderbuffer
*bufObj
;
2754 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
2756 _mesa_error(ctx
, GL_INVALID_VALUE
,
2757 "glObjectUnpurgeable(name = 0x%x)", name
);
2761 if (! bufObj
->Purgeable
) {
2762 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2763 "glObjectUnpurgeable(name = 0x%x) object is "
2764 " already \"unpurged\"", name
);
2768 bufObj
->Purgeable
= GL_FALSE
;
2771 if (ctx
->Driver
.RenderObjectUnpurgeable
)
2772 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
2779 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2781 struct gl_texture_object
*bufObj
;
2784 bufObj
= _mesa_lookup_texture(ctx
, name
);
2786 _mesa_error(ctx
, GL_INVALID_VALUE
,
2787 "glObjectUnpurgeable(name = 0x%x)", name
);
2791 if (! bufObj
->Purgeable
) {
2792 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2793 "glObjectUnpurgeable(name = 0x%x) object is"
2794 " already \"unpurged\"", name
);
2798 bufObj
->Purgeable
= GL_FALSE
;
2801 if (ctx
->Driver
.TextureObjectUnpurgeable
)
2802 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
2809 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
2811 GET_CURRENT_CONTEXT(ctx
);
2812 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2815 _mesa_error(ctx
, GL_INVALID_VALUE
,
2816 "glObjectUnpurgeable(name = 0x%x)", name
);
2821 case GL_RETAINED_APPLE
:
2822 case GL_UNDEFINED_APPLE
:
2826 _mesa_error(ctx
, GL_INVALID_ENUM
,
2827 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2832 switch (objectType
) {
2833 case GL_BUFFER_OBJECT_APPLE
:
2834 return buffer_object_unpurgeable(ctx
, name
, option
);
2836 return texture_object_unpurgeable(ctx
, name
, option
);
2837 case GL_RENDERBUFFER_EXT
:
2838 return renderbuffer_unpurgeable(ctx
, name
, option
);
2840 _mesa_error(ctx
, GL_INVALID_ENUM
,
2841 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2849 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
2850 GLenum pname
, GLint
*params
)
2852 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2854 _mesa_error(ctx
, GL_INVALID_VALUE
,
2855 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
2860 case GL_PURGEABLE_APPLE
:
2861 *params
= bufObj
->Purgeable
;
2864 _mesa_error(ctx
, GL_INVALID_ENUM
,
2865 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2873 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
2874 GLenum pname
, GLint
*params
)
2876 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
2878 _mesa_error(ctx
, GL_INVALID_VALUE
,
2879 "glObjectUnpurgeable(name = 0x%x)", name
);
2884 case GL_PURGEABLE_APPLE
:
2885 *params
= rb
->Purgeable
;
2888 _mesa_error(ctx
, GL_INVALID_ENUM
,
2889 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2897 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
2898 GLenum pname
, GLint
*params
)
2900 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
2902 _mesa_error(ctx
, GL_INVALID_VALUE
,
2903 "glObjectUnpurgeable(name = 0x%x)", name
);
2908 case GL_PURGEABLE_APPLE
:
2909 *params
= texObj
->Purgeable
;
2912 _mesa_error(ctx
, GL_INVALID_ENUM
,
2913 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2921 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
2924 GET_CURRENT_CONTEXT(ctx
);
2927 _mesa_error(ctx
, GL_INVALID_VALUE
,
2928 "glGetObjectParameteriv(name = 0x%x)", name
);
2932 switch (objectType
) {
2934 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
2936 case GL_BUFFER_OBJECT_APPLE
:
2937 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
2939 case GL_RENDERBUFFER_EXT
:
2940 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
2943 _mesa_error(ctx
, GL_INVALID_ENUM
,
2944 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2950 * Binds a buffer object to a uniform buffer binding point.
2952 * The caller is responsible for flushing vertices and updating
2956 set_ubo_binding(struct gl_context
*ctx
,
2957 struct gl_uniform_buffer_binding
*binding
,
2958 struct gl_buffer_object
*bufObj
,
2963 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2965 binding
->Offset
= offset
;
2966 binding
->Size
= size
;
2967 binding
->AutomaticSize
= autoSize
;
2969 /* If this is a real buffer object, mark it has having been used
2970 * at some point as a UBO.
2973 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2977 * Binds a buffer object to a uniform buffer binding point.
2979 * Unlike set_ubo_binding(), this function also flushes vertices
2980 * and updates NewDriverState. It also checks if the binding
2981 * has actually changed before updating it.
2984 bind_uniform_buffer(struct gl_context
*ctx
,
2986 struct gl_buffer_object
*bufObj
,
2991 struct gl_uniform_buffer_binding
*binding
=
2992 &ctx
->UniformBufferBindings
[index
];
2994 if (binding
->BufferObject
== bufObj
&&
2995 binding
->Offset
== offset
&&
2996 binding
->Size
== size
&&
2997 binding
->AutomaticSize
== autoSize
) {
3001 FLUSH_VERTICES(ctx
, 0);
3002 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3004 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
3008 * Bind a region of a buffer object to a uniform block binding point.
3009 * \param index the uniform buffer binding point index
3010 * \param bufObj the buffer object
3011 * \param offset offset to the start of buffer object region
3012 * \param size size of the buffer object region
3015 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
3017 struct gl_buffer_object
*bufObj
,
3021 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3022 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3026 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3027 _mesa_error(ctx
, GL_INVALID_VALUE
,
3028 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3029 ctx
->Const
.UniformBufferOffsetAlignment
);
3033 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3038 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3039 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3044 * Bind a buffer object to a uniform block binding point.
3045 * As above, but offset = 0.
3048 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
3050 struct gl_buffer_object
*bufObj
)
3052 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3053 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
3057 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3059 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3060 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
3062 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
3066 * Binds a buffer object to an atomic buffer binding point.
3068 * The caller is responsible for validating the offset,
3069 * flushing the vertices and updating NewDriverState.
3072 set_atomic_buffer_binding(struct gl_context
*ctx
,
3073 struct gl_atomic_buffer_binding
*binding
,
3074 struct gl_buffer_object
*bufObj
,
3078 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
3080 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3081 binding
->Offset
= -1;
3084 binding
->Offset
= offset
;
3085 binding
->Size
= size
;
3086 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
3091 * Binds a buffer object to an atomic buffer binding point.
3093 * Unlike set_atomic_buffer_binding(), this function also validates the
3094 * index and offset, flushes vertices, and updates NewDriverState.
3095 * It also checks if the binding has actually changing before
3099 bind_atomic_buffer(struct gl_context
*ctx
,
3101 struct gl_buffer_object
*bufObj
,
3106 struct gl_atomic_buffer_binding
*binding
;
3108 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3109 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3113 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3114 _mesa_error(ctx
, GL_INVALID_VALUE
,
3115 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3116 ATOMIC_COUNTER_SIZE
);
3120 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3122 binding
= &ctx
->AtomicBufferBindings
[index
];
3123 if (binding
->BufferObject
== bufObj
&&
3124 binding
->Offset
== offset
&&
3125 binding
->Size
== size
) {
3129 FLUSH_VERTICES(ctx
, 0);
3130 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3132 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3136 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3138 const GLintptr
*offsets
,
3139 const GLsizeiptr
*sizes
)
3141 if (offsets
[index
] < 0) {
3142 /* The ARB_multi_bind spec says:
3144 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3145 * value in <offsets> is less than zero (per binding)."
3147 _mesa_error(ctx
, GL_INVALID_VALUE
,
3148 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3149 index
, (int64_t) offsets
[index
]);
3153 if (sizes
[index
] <= 0) {
3154 /* The ARB_multi_bind spec says:
3156 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3157 * value in <sizes> is less than or equal to zero (per binding)."
3159 _mesa_error(ctx
, GL_INVALID_VALUE
,
3160 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3161 index
, (int64_t) sizes
[index
]);
3169 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3170 GLuint first
, GLsizei count
,
3173 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3174 _mesa_error(ctx
, GL_INVALID_ENUM
,
3175 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3179 /* The ARB_multi_bind_spec says:
3181 * "An INVALID_OPERATION error is generated if <first> + <count> is
3182 * greater than the number of target-specific indexed binding points,
3183 * as described in section 6.7.1."
3185 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3186 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3187 "%s(first=%u + count=%d > the value of "
3188 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3189 caller
, first
, count
,
3190 ctx
->Const
.MaxUniformBufferBindings
);
3198 * Unbind all uniform buffers in the range
3199 * <first> through <first>+<count>-1
3202 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3204 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3207 for (i
= 0; i
< count
; i
++)
3208 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3209 bufObj
, -1, -1, GL_TRUE
);
3213 bind_uniform_buffers_base(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3214 const GLuint
*buffers
)
3218 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, "glBindBuffersBase"))
3221 /* Assume that at least one binding will be changed */
3222 FLUSH_VERTICES(ctx
, 0);
3223 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3226 /* The ARB_multi_bind spec says:
3228 * "If <buffers> is NULL, all bindings from <first> through
3229 * <first>+<count>-1 are reset to their unbound (zero) state."
3231 unbind_uniform_buffers(ctx
, first
, count
);
3235 /* Note that the error semantics for multi-bind commands differ from
3236 * those of other GL commands.
3238 * The Issues section in the ARB_multi_bind spec says:
3240 * "(11) Typically, OpenGL specifies that if an error is generated by a
3241 * command, that command has no effect. This is somewhat
3242 * unfortunate for multi-bind commands, because it would require a
3243 * first pass to scan the entire list of bound objects for errors
3244 * and then a second pass to actually perform the bindings.
3245 * Should we have different error semantics?
3247 * RESOLVED: Yes. In this specification, when the parameters for
3248 * one of the <count> binding points are invalid, that binding point
3249 * is not updated and an error will be generated. However, other
3250 * binding points in the same command will be updated if their
3251 * parameters are valid and no other error occurs."
3254 _mesa_begin_bufferobj_lookups(ctx
);
3256 for (i
= 0; i
< count
; i
++) {
3257 struct gl_uniform_buffer_binding
*binding
=
3258 &ctx
->UniformBufferBindings
[first
+ i
];
3259 struct gl_buffer_object
*bufObj
;
3261 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3262 bufObj
= binding
->BufferObject
;
3264 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3265 "glBindBuffersBase");
3268 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3269 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_TRUE
);
3271 set_ubo_binding(ctx
, binding
, bufObj
, 0, 0, GL_TRUE
);
3275 _mesa_end_bufferobj_lookups(ctx
);
3279 bind_uniform_buffers_range(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3280 const GLuint
*buffers
,
3281 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3285 if (!error_check_bind_uniform_buffers(ctx
, first
, count
,
3286 "glBindBuffersRange"))
3289 /* Assume that at least one binding will be changed */
3290 FLUSH_VERTICES(ctx
, 0);
3291 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3294 /* The ARB_multi_bind spec says:
3296 * "If <buffers> is NULL, all bindings from <first> through
3297 * <first>+<count>-1 are reset to their unbound (zero) state.
3298 * In this case, the offsets and sizes associated with the
3299 * binding points are set to default values, ignoring
3300 * <offsets> and <sizes>."
3302 unbind_uniform_buffers(ctx
, first
, count
);
3306 /* Note that the error semantics for multi-bind commands differ from
3307 * those of other GL commands.
3309 * The Issues section in the ARB_multi_bind spec says:
3311 * "(11) Typically, OpenGL specifies that if an error is generated by a
3312 * command, that command has no effect. This is somewhat
3313 * unfortunate for multi-bind commands, because it would require a
3314 * first pass to scan the entire list of bound objects for errors
3315 * and then a second pass to actually perform the bindings.
3316 * Should we have different error semantics?
3318 * RESOLVED: Yes. In this specification, when the parameters for
3319 * one of the <count> binding points are invalid, that binding point
3320 * is not updated and an error will be generated. However, other
3321 * binding points in the same command will be updated if their
3322 * parameters are valid and no other error occurs."
3325 _mesa_begin_bufferobj_lookups(ctx
);
3327 for (i
= 0; i
< count
; i
++) {
3328 struct gl_uniform_buffer_binding
*binding
=
3329 &ctx
->UniformBufferBindings
[first
+ i
];
3330 struct gl_buffer_object
*bufObj
;
3332 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3335 /* The ARB_multi_bind spec says:
3337 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3338 * pair of values in <offsets> and <sizes> does not respectively
3339 * satisfy the constraints described for those parameters for the
3340 * specified target, as described in section 6.7.1 (per binding)."
3342 * Section 6.7.1 refers to table 6.5, which says:
3344 * "┌───────────────────────────────────────────────────────────────┐
3345 * │ Uniform buffer array bindings (see sec. 7.6) │
3346 * ├─────────────────────┬─────────────────────────────────────────┤
3348 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3349 * │ │ OFFSET_ALIGNMENT │
3351 * │ size restriction │ none │
3352 * └─────────────────────┴─────────────────────────────────────────┘"
3354 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3355 _mesa_error(ctx
, GL_INVALID_VALUE
,
3356 "glBindBuffersRange(offsets[%u]=%" PRId64
3357 " is misaligned; it must be a multiple of the value of "
3358 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3359 "target=GL_UNIFORM_BUFFER)",
3360 i
, (int64_t) offsets
[i
],
3361 ctx
->Const
.UniformBufferOffsetAlignment
);
3365 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3366 bufObj
= binding
->BufferObject
;
3368 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3369 "glBindBuffersRange");
3372 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3373 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_FALSE
);
3375 set_ubo_binding(ctx
, binding
, bufObj
,
3376 offsets
[i
], sizes
[i
], GL_FALSE
);
3380 _mesa_end_bufferobj_lookups(ctx
);
3384 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3385 struct gl_transform_feedback_object
*tfObj
,
3386 GLuint first
, GLsizei count
, const char *caller
)
3388 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3389 _mesa_error(ctx
, GL_INVALID_ENUM
,
3390 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3394 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3396 * "An INVALID_OPERATION error is generated :
3399 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3400 * FEEDBACK_BUFFER and transform feedback is currently active."
3402 * We assume that this is also meant to apply to BindBuffersRange
3403 * and BindBuffersBase.
3405 if (tfObj
->Active
) {
3406 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3407 "%s(Changing transform feedback buffers while "
3408 "transform feedback is active)", caller
);
3412 /* The ARB_multi_bind_spec says:
3414 * "An INVALID_OPERATION error is generated if <first> + <count> is
3415 * greater than the number of target-specific indexed binding points,
3416 * as described in section 6.7.1."
3418 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3419 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3420 "%s(first=%u + count=%d > the value of "
3421 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3422 caller
, first
, count
,
3423 ctx
->Const
.MaxTransformFeedbackBuffers
);
3431 * Unbind all transform feedback buffers in the range
3432 * <first> through <first>+<count>-1
3435 unbind_xfb_buffers(struct gl_context
*ctx
,
3436 struct gl_transform_feedback_object
*tfObj
,
3437 GLuint first
, GLsizei count
)
3439 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3442 for (i
= 0; i
< count
; i
++)
3443 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3448 bind_xfb_buffers_base(struct gl_context
*ctx
,
3449 GLuint first
, GLsizei count
,
3450 const GLuint
*buffers
)
3452 struct gl_transform_feedback_object
*tfObj
=
3453 ctx
->TransformFeedback
.CurrentObject
;
3456 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3457 "glBindBuffersBase"))
3460 /* Assume that at least one binding will be changed */
3461 FLUSH_VERTICES(ctx
, 0);
3462 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3465 /* The ARB_multi_bind spec says:
3467 * "If <buffers> is NULL, all bindings from <first> through
3468 * <first>+<count>-1 are reset to their unbound (zero) state."
3470 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3474 /* Note that the error semantics for multi-bind commands differ from
3475 * those of other GL commands.
3477 * The Issues section in the ARB_multi_bind spec says:
3479 * "(11) Typically, OpenGL specifies that if an error is generated by a
3480 * command, that command has no effect. This is somewhat
3481 * unfortunate for multi-bind commands, because it would require a
3482 * first pass to scan the entire list of bound objects for errors
3483 * and then a second pass to actually perform the bindings.
3484 * Should we have different error semantics?
3486 * RESOLVED: Yes. In this specification, when the parameters for
3487 * one of the <count> binding points are invalid, that binding point
3488 * is not updated and an error will be generated. However, other
3489 * binding points in the same command will be updated if their
3490 * parameters are valid and no other error occurs."
3493 _mesa_begin_bufferobj_lookups(ctx
);
3495 for (i
= 0; i
< count
; i
++) {
3496 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[first
+ i
];
3497 struct gl_buffer_object
*bufObj
;
3499 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3500 bufObj
= boundBufObj
;
3502 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3503 "glBindBuffersBase");
3506 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3510 _mesa_end_bufferobj_lookups(ctx
);
3514 bind_xfb_buffers_range(struct gl_context
*ctx
,
3515 GLuint first
, GLsizei count
,
3516 const GLuint
*buffers
,
3517 const GLintptr
*offsets
,
3518 const GLsizeiptr
*sizes
)
3520 struct gl_transform_feedback_object
*tfObj
=
3521 ctx
->TransformFeedback
.CurrentObject
;
3524 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3525 "glBindBuffersRange"))
3528 /* Assume that at least one binding will be changed */
3529 FLUSH_VERTICES(ctx
, 0);
3530 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3533 /* The ARB_multi_bind spec says:
3535 * "If <buffers> is NULL, all bindings from <first> through
3536 * <first>+<count>-1 are reset to their unbound (zero) state.
3537 * In this case, the offsets and sizes associated with the
3538 * binding points are set to default values, ignoring
3539 * <offsets> and <sizes>."
3541 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3545 /* Note that the error semantics for multi-bind commands differ from
3546 * those of other GL commands.
3548 * The Issues section in the ARB_multi_bind spec says:
3550 * "(11) Typically, OpenGL specifies that if an error is generated by a
3551 * command, that command has no effect. This is somewhat
3552 * unfortunate for multi-bind commands, because it would require a
3553 * first pass to scan the entire list of bound objects for errors
3554 * and then a second pass to actually perform the bindings.
3555 * Should we have different error semantics?
3557 * RESOLVED: Yes. In this specification, when the parameters for
3558 * one of the <count> binding points are invalid, that binding point
3559 * is not updated and an error will be generated. However, other
3560 * binding points in the same command will be updated if their
3561 * parameters are valid and no other error occurs."
3564 _mesa_begin_bufferobj_lookups(ctx
);
3566 for (i
= 0; i
< count
; i
++) {
3567 const GLuint index
= first
+ i
;
3568 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3569 struct gl_buffer_object
*bufObj
;
3571 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3574 /* The ARB_multi_bind spec says:
3576 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3577 * pair of values in <offsets> and <sizes> does not respectively
3578 * satisfy the constraints described for those parameters for the
3579 * specified target, as described in section 6.7.1 (per binding)."
3581 * Section 6.7.1 refers to table 6.5, which says:
3583 * "┌───────────────────────────────────────────────────────────────┐
3584 * │ Transform feedback array bindings (see sec. 13.2.2) │
3585 * ├───────────────────────┬───────────────────────────────────────┤
3587 * │ offset restriction │ multiple of 4 │
3589 * │ size restriction │ multiple of 4 │
3590 * └───────────────────────┴───────────────────────────────────────┘"
3592 if (offsets
[i
] & 0x3) {
3593 _mesa_error(ctx
, GL_INVALID_VALUE
,
3594 "glBindBuffersRange(offsets[%u]=%" PRId64
3595 " is misaligned; it must be a multiple of 4 when "
3596 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3597 i
, (int64_t) offsets
[i
]);
3601 if (sizes
[i
] & 0x3) {
3602 _mesa_error(ctx
, GL_INVALID_VALUE
,
3603 "glBindBuffersRange(sizes[%u]=%" PRId64
3604 " is misaligned; it must be a multiple of 4 when "
3605 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3606 i
, (int64_t) sizes
[i
]);
3610 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3611 bufObj
= boundBufObj
;
3613 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3614 "glBindBuffersRange");
3617 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3618 offsets
[i
], sizes
[i
]);
3621 _mesa_end_bufferobj_lookups(ctx
);
3625 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3626 GLuint first
, GLsizei count
,
3629 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3630 _mesa_error(ctx
, GL_INVALID_ENUM
,
3631 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3635 /* The ARB_multi_bind_spec says:
3637 * "An INVALID_OPERATION error is generated if <first> + <count> is
3638 * greater than the number of target-specific indexed binding points,
3639 * as described in section 6.7.1."
3641 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3642 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3643 "%s(first=%u + count=%d > the value of "
3644 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3645 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3653 * Unbind all atomic counter buffers in the range
3654 * <first> through <first>+<count>-1
3657 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3659 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3662 for (i
= 0; i
< count
; i
++)
3663 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3668 bind_atomic_buffers_base(struct gl_context
*ctx
,
3671 const GLuint
*buffers
)
3675 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
3676 "glBindBuffersBase"))
3679 /* Assume that at least one binding will be changed */
3680 FLUSH_VERTICES(ctx
, 0);
3681 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3684 /* The ARB_multi_bind spec says:
3686 * "If <buffers> is NULL, all bindings from <first> through
3687 * <first>+<count>-1 are reset to their unbound (zero) state."
3689 unbind_atomic_buffers(ctx
, first
, count
);
3693 /* Note that the error semantics for multi-bind commands differ from
3694 * those of other GL commands.
3696 * The Issues section in the ARB_multi_bind spec says:
3698 * "(11) Typically, OpenGL specifies that if an error is generated by a
3699 * command, that command has no effect. This is somewhat
3700 * unfortunate for multi-bind commands, because it would require a
3701 * first pass to scan the entire list of bound objects for errors
3702 * and then a second pass to actually perform the bindings.
3703 * Should we have different error semantics?
3705 * RESOLVED: Yes. In this specification, when the parameters for
3706 * one of the <count> binding points are invalid, that binding point
3707 * is not updated and an error will be generated. However, other
3708 * binding points in the same command will be updated if their
3709 * parameters are valid and no other error occurs."
3712 _mesa_begin_bufferobj_lookups(ctx
);
3714 for (i
= 0; i
< count
; i
++) {
3715 struct gl_atomic_buffer_binding
*binding
=
3716 &ctx
->AtomicBufferBindings
[first
+ i
];
3717 struct gl_buffer_object
*bufObj
;
3719 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3720 bufObj
= binding
->BufferObject
;
3722 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3723 "glBindBuffersBase");
3726 set_atomic_buffer_binding(ctx
, binding
, bufObj
, 0, 0);
3729 _mesa_end_bufferobj_lookups(ctx
);
3733 bind_atomic_buffers_range(struct gl_context
*ctx
,
3736 const GLuint
*buffers
,
3737 const GLintptr
*offsets
,
3738 const GLsizeiptr
*sizes
)
3742 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
3743 "glBindBuffersRange"))
3746 /* Assume that at least one binding will be changed */
3747 FLUSH_VERTICES(ctx
, 0);
3748 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3751 /* The ARB_multi_bind spec says:
3753 * "If <buffers> is NULL, all bindings from <first> through
3754 * <first>+<count>-1 are reset to their unbound (zero) state.
3755 * In this case, the offsets and sizes associated with the
3756 * binding points are set to default values, ignoring
3757 * <offsets> and <sizes>."
3759 unbind_atomic_buffers(ctx
, first
, count
);
3763 /* Note that the error semantics for multi-bind commands differ from
3764 * those of other GL commands.
3766 * The Issues section in the ARB_multi_bind spec says:
3768 * "(11) Typically, OpenGL specifies that if an error is generated by a
3769 * command, that command has no effect. This is somewhat
3770 * unfortunate for multi-bind commands, because it would require a
3771 * first pass to scan the entire list of bound objects for errors
3772 * and then a second pass to actually perform the bindings.
3773 * Should we have different error semantics?
3775 * RESOLVED: Yes. In this specification, when the parameters for
3776 * one of the <count> binding points are invalid, that binding point
3777 * is not updated and an error will be generated. However, other
3778 * binding points in the same command will be updated if their
3779 * parameters are valid and no other error occurs."
3782 _mesa_begin_bufferobj_lookups(ctx
);
3784 for (i
= 0; i
< count
; i
++) {
3785 struct gl_atomic_buffer_binding
*binding
=
3786 &ctx
->AtomicBufferBindings
[first
+ i
];
3787 struct gl_buffer_object
*bufObj
;
3789 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3792 /* The ARB_multi_bind spec says:
3794 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3795 * pair of values in <offsets> and <sizes> does not respectively
3796 * satisfy the constraints described for those parameters for the
3797 * specified target, as described in section 6.7.1 (per binding)."
3799 * Section 6.7.1 refers to table 6.5, which says:
3801 * "┌───────────────────────────────────────────────────────────────┐
3802 * │ Atomic counter array bindings (see sec. 7.7.2) │
3803 * ├───────────────────────┬───────────────────────────────────────┤
3805 * │ offset restriction │ multiple of 4 │
3807 * │ size restriction │ none │
3808 * └───────────────────────┴───────────────────────────────────────┘"
3810 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3811 _mesa_error(ctx
, GL_INVALID_VALUE
,
3812 "glBindBuffersRange(offsets[%u]=%" PRId64
3813 " is misaligned; it must be a multiple of %d when "
3814 "target=GL_ATOMIC_COUNTER_BUFFER)",
3815 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3819 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3820 bufObj
= binding
->BufferObject
;
3822 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3823 "glBindBuffersRange");
3826 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offsets
[i
], sizes
[i
]);
3829 _mesa_end_bufferobj_lookups(ctx
);
3833 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3834 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3836 GET_CURRENT_CONTEXT(ctx
);
3837 struct gl_buffer_object
*bufObj
;
3840 bufObj
= ctx
->Shared
->NullBufferObj
;
3842 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3844 if (!_mesa_handle_bind_buffer_gen(ctx
, target
, buffer
,
3845 &bufObj
, "glBindBufferRange"))
3849 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3850 "glBindBufferRange(invalid buffer=%u)", buffer
);
3856 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3863 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3864 _mesa_bind_buffer_range_transform_feedback(ctx
, index
, bufObj
,
3867 case GL_UNIFORM_BUFFER
:
3868 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3870 case GL_ATOMIC_COUNTER_BUFFER
:
3871 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3872 "glBindBufferRange");
3875 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3881 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3883 GET_CURRENT_CONTEXT(ctx
);
3884 struct gl_buffer_object
*bufObj
;
3887 bufObj
= ctx
->Shared
->NullBufferObj
;
3889 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3891 if (!_mesa_handle_bind_buffer_gen(ctx
, target
, buffer
,
3892 &bufObj
, "glBindBufferBase"))
3896 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3897 "glBindBufferBase(invalid buffer=%u)", buffer
);
3901 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3902 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3904 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3905 * zero and size equal to the size of buffer."
3907 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3909 * "If the parameter (starting offset or size) was not specified when the
3910 * buffer object was bound, zero is returned."
3912 * What happens if the size of the buffer changes? Does the size of the
3913 * buffer at the moment glBindBufferBase was called still play a role, like
3914 * the first quote would imply, or is the size meaningless in the
3915 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3916 * core spec page 45 says:
3918 * "It is equivalent to calling BindBufferRange with offset zero, while
3919 * size is determined by the size of the bound buffer at the time the
3922 * My interpretation is that the GL 4.1 spec was a clarification of the
3923 * behavior, not a change. In particular, this choice will only make
3924 * rendering work in cases where it would have had undefined results.
3928 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3929 _mesa_bind_buffer_base_transform_feedback(ctx
, index
, bufObj
);
3931 case GL_UNIFORM_BUFFER
:
3932 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
3934 case GL_ATOMIC_COUNTER_BUFFER
:
3935 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
3936 "glBindBufferBase");
3939 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
3945 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
3946 const GLuint
*buffers
,
3947 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3949 GET_CURRENT_CONTEXT(ctx
);
3952 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3953 bind_xfb_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
3955 case GL_UNIFORM_BUFFER
:
3956 bind_uniform_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
3958 case GL_ATOMIC_COUNTER_BUFFER
:
3959 bind_atomic_buffers_range(ctx
, first
, count
, buffers
,
3963 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
3964 _mesa_lookup_enum_by_nr(target
));
3970 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
3971 const GLuint
*buffers
)
3973 GET_CURRENT_CONTEXT(ctx
);
3976 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3977 bind_xfb_buffers_base(ctx
, first
, count
, buffers
);
3979 case GL_UNIFORM_BUFFER
:
3980 bind_uniform_buffers_base(ctx
, first
, count
, buffers
);
3982 case GL_ATOMIC_COUNTER_BUFFER
:
3983 bind_atomic_buffers_base(ctx
, first
, count
, buffers
);
3986 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
3987 _mesa_lookup_enum_by_nr(target
));
3993 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
3996 GET_CURRENT_CONTEXT(ctx
);
3997 struct gl_buffer_object
*bufObj
;
3998 const GLintptr end
= offset
+ length
;
4000 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4002 _mesa_error(ctx
, GL_INVALID_VALUE
,
4003 "glInvalidateBufferSubData(name = 0x%x) invalid object",
4008 /* The GL_ARB_invalidate_subdata spec says:
4010 * "An INVALID_VALUE error is generated if <offset> or <length> is
4011 * negative, or if <offset> + <length> is greater than the value of
4014 if (end
< 0 || end
> bufObj
->Size
) {
4015 _mesa_error(ctx
, GL_INVALID_VALUE
,
4016 "glInvalidateBufferSubData(invalid offset or length)");
4020 /* The OpenGL 4.4 (Core Profile) spec says:
4022 * "An INVALID_OPERATION error is generated if buffer is currently
4023 * mapped by MapBuffer or if the invalidate range intersects the range
4024 * currently mapped by MapBufferRange, unless it was mapped
4025 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4027 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4028 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4029 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4030 "glInvalidateBufferSubData(intersection with mapped "
4035 /* We don't actually do anything for this yet. Just return after
4036 * validating the parameters and generating the required errors.
4042 _mesa_InvalidateBufferData(GLuint buffer
)
4044 GET_CURRENT_CONTEXT(ctx
);
4045 struct gl_buffer_object
*bufObj
;
4047 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4049 _mesa_error(ctx
, GL_INVALID_VALUE
,
4050 "glInvalidateBufferData(name = 0x%x) invalid object",
4055 /* The OpenGL 4.4 (Core Profile) spec says:
4057 * "An INVALID_OPERATION error is generated if buffer is currently
4058 * mapped by MapBuffer or if the invalidate range intersects the range
4059 * currently mapped by MapBufferRange, unless it was mapped
4060 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4062 if (_mesa_check_disallowed_mapping(bufObj
)) {
4063 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4064 "glInvalidateBufferData(intersection with mapped "
4069 /* We don't actually do anything for this yet. Just return after
4070 * validating the parameters and generating the required errors.