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 an existing
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
);
1016 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1017 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1018 "%s(non-existent buffer object %u)", caller
, buffer
);
1027 _mesa_begin_bufferobj_lookups(struct gl_context
*ctx
)
1029 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1034 _mesa_end_bufferobj_lookups(struct gl_context
*ctx
)
1036 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1041 * Look up a buffer object for a multi-bind function.
1043 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1044 * of generating an error if the buffer ID is not zero or the name
1045 * of an existing buffer object.
1047 * If the buffer ID refers to an existing buffer object, a pointer
1048 * to the buffer object is returned. If the ID is zero, a pointer
1049 * to the shared NullBufferObj is returned. If the ID is not zero
1050 * and does not refer to a valid buffer object, this function
1053 * This function assumes that the caller has already locked the
1054 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1056 struct gl_buffer_object
*
1057 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1058 const GLuint
*buffers
,
1059 GLuint index
, const char *caller
)
1061 struct gl_buffer_object
*bufObj
;
1063 if (buffers
[index
] != 0) {
1064 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1066 /* The multi-bind functions don't create the buffer objects
1067 when they don't exist. */
1068 if (bufObj
== &DummyBufferObject
)
1071 bufObj
= ctx
->Shared
->NullBufferObj
;
1074 /* The ARB_multi_bind spec says:
1076 * "An INVALID_OPERATION error is generated if any value
1077 * in <buffers> is not zero or the name of an existing
1078 * buffer object (per binding)."
1080 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1081 "%s(buffers[%u]=%u is not zero or the name "
1082 "of an existing buffer object)",
1083 caller
, index
, buffers
[index
]);
1091 * If *ptr points to obj, set ptr = the Null/default buffer object.
1092 * This is a helper for buffer object deletion.
1093 * The GL spec says that deleting a buffer object causes it to get
1094 * unbound from all arrays in the current context.
1097 unbind(struct gl_context
*ctx
,
1098 struct gl_buffer_object
**ptr
,
1099 struct gl_buffer_object
*obj
)
1102 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
1108 * Plug default/fallback buffer object functions into the device
1112 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1114 /* GL_ARB_vertex/pixel_buffer_object */
1115 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1116 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1117 driver
->BufferData
= buffer_data_fallback
;
1118 driver
->BufferSubData
= buffer_sub_data_fallback
;
1119 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1120 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1122 /* GL_ARB_clear_buffer_object */
1123 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1125 /* GL_ARB_map_buffer_range */
1126 driver
->MapBufferRange
= map_buffer_range_fallback
;
1127 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1129 /* GL_ARB_copy_buffer */
1130 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1135 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1136 struct gl_buffer_object
*bufObj
)
1140 for (i
= 0; i
< MAP_COUNT
; i
++) {
1141 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1142 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1143 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1144 bufObj
->Mappings
[i
].AccessFlags
= 0;
1150 /**********************************************************************/
1152 /**********************************************************************/
1155 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1157 GET_CURRENT_CONTEXT(ctx
);
1159 if (MESA_VERBOSE
& VERBOSE_API
)
1160 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1161 _mesa_lookup_enum_by_nr(target
), buffer
);
1163 bind_buffer_object(ctx
, target
, buffer
);
1168 * Delete a set of buffer objects.
1170 * \param n Number of buffer objects to delete.
1171 * \param ids Array of \c n buffer object IDs.
1174 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1176 GET_CURRENT_CONTEXT(ctx
);
1178 FLUSH_VERTICES(ctx
, 0);
1181 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1185 mtx_lock(&ctx
->Shared
->Mutex
);
1187 for (i
= 0; i
< n
; i
++) {
1188 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
1190 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1193 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1195 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1197 /* unbind any vertex pointers bound to this buffer */
1198 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1199 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1202 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1203 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1205 if (vao
->IndexBufferObj
== bufObj
) {
1206 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1209 /* unbind ARB_draw_indirect binding point */
1210 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1211 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1214 /* unbind ARB_copy_buffer binding points */
1215 if (ctx
->CopyReadBuffer
== bufObj
) {
1216 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1218 if (ctx
->CopyWriteBuffer
== bufObj
) {
1219 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1222 /* unbind transform feedback binding points */
1223 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1224 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1226 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1227 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1228 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1232 /* unbind UBO binding points */
1233 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1234 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1235 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1239 if (ctx
->UniformBuffer
== bufObj
) {
1240 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1243 /* unbind Atomci Buffer binding points */
1244 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1245 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1246 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1250 if (ctx
->AtomicBuffer
== bufObj
) {
1251 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1254 /* unbind any pixel pack/unpack pointers bound to this buffer */
1255 if (ctx
->Pack
.BufferObj
== bufObj
) {
1256 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1258 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1259 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1262 if (ctx
->Texture
.BufferObject
== bufObj
) {
1263 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1266 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1267 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1270 /* The ID is immediately freed for re-use */
1271 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1272 /* Make sure we do not run into the classic ABA problem on bind.
1273 * We don't want to allow re-binding a buffer object that's been
1274 * "deleted" by glDeleteBuffers().
1276 * The explicit rebinding to the default object in the current context
1277 * prevents the above in the current context, but another context
1278 * sharing the same objects might suffer from this problem.
1279 * The alternative would be to do the hash lookup in any case on bind
1280 * which would introduce more runtime overhead than this.
1282 bufObj
->DeletePending
= GL_TRUE
;
1283 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1287 mtx_unlock(&ctx
->Shared
->Mutex
);
1292 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1293 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1297 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1299 GET_CURRENT_CONTEXT(ctx
);
1302 struct gl_buffer_object
*buf
;
1304 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1306 if (MESA_VERBOSE
& VERBOSE_API
)
1307 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1310 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1319 * This must be atomic (generation and allocation of buffer object IDs)
1321 mtx_lock(&ctx
->Shared
->Mutex
);
1323 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1325 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1326 * DummyBufferObject. Otherwise, create a new buffer object and insert
1329 for (i
= 0; i
< n
; i
++) {
1330 buffers
[i
] = first
+ i
;
1332 assert(ctx
->Driver
.NewBufferObject
);
1333 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1335 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1340 buf
= &DummyBufferObject
;
1342 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1345 mtx_unlock(&ctx
->Shared
->Mutex
);
1349 * Generate a set of unique buffer object IDs and store them in \c buffers.
1351 * \param n Number of IDs to generate.
1352 * \param buffers Array of \c n locations to store the IDs.
1355 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1357 create_buffers(n
, buffers
, false);
1361 * Create a set of buffer objects and store their unique IDs in \c buffers.
1363 * \param n Number of IDs to generate.
1364 * \param buffers Array of \c n locations to store the IDs.
1367 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1369 create_buffers(n
, buffers
, true);
1374 * Determine if ID is the name of a buffer object.
1376 * \param id ID of the potential buffer object.
1377 * \return \c GL_TRUE if \c id is the name of a buffer object,
1378 * \c GL_FALSE otherwise.
1380 GLboolean GLAPIENTRY
1381 _mesa_IsBuffer(GLuint id
)
1383 struct gl_buffer_object
*bufObj
;
1384 GET_CURRENT_CONTEXT(ctx
);
1385 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1387 mtx_lock(&ctx
->Shared
->Mutex
);
1388 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1389 mtx_unlock(&ctx
->Shared
->Mutex
);
1391 return bufObj
&& bufObj
!= &DummyBufferObject
;
1396 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1397 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1398 GLbitfield flags
, const char *func
)
1401 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1405 if (flags
& ~(GL_MAP_READ_BIT
|
1407 GL_MAP_PERSISTENT_BIT
|
1408 GL_MAP_COHERENT_BIT
|
1409 GL_DYNAMIC_STORAGE_BIT
|
1410 GL_CLIENT_STORAGE_BIT
)) {
1411 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1415 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1416 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1417 _mesa_error(ctx
, GL_INVALID_VALUE
,
1418 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1422 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1423 _mesa_error(ctx
, GL_INVALID_VALUE
,
1424 "%s(COHERENT and flags!=PERSISTENT)", func
);
1428 if (bufObj
->Immutable
) {
1429 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1433 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1434 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1436 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1438 bufObj
->Written
= GL_TRUE
;
1439 bufObj
->Immutable
= GL_TRUE
;
1441 assert(ctx
->Driver
.BufferData
);
1442 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1444 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1445 /* Even though the interaction between AMD_pinned_memory and
1446 * glBufferStorage is not described in the spec, Graham Sellers
1447 * said that it should behave the same as glBufferData.
1449 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1452 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1458 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1461 GET_CURRENT_CONTEXT(ctx
);
1462 struct gl_buffer_object
*bufObj
;
1464 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1468 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1473 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1476 GET_CURRENT_CONTEXT(ctx
);
1477 struct gl_buffer_object
*bufObj
;
1479 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1484 * In direct state access, buffer objects have an unspecified target since
1485 * they are not required to be bound.
1487 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1488 "glNamedBufferStorage");
1493 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1494 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1495 GLenum usage
, const char *func
)
1499 if (MESA_VERBOSE
& VERBOSE_API
)
1500 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1502 _mesa_lookup_enum_by_nr(target
),
1503 (long int) size
, data
,
1504 _mesa_lookup_enum_by_nr(usage
));
1507 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1512 case GL_STREAM_DRAW_ARB
:
1513 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1516 case GL_STATIC_DRAW_ARB
:
1517 case GL_DYNAMIC_DRAW_ARB
:
1521 case GL_STREAM_READ_ARB
:
1522 case GL_STREAM_COPY_ARB
:
1523 case GL_STATIC_READ_ARB
:
1524 case GL_STATIC_COPY_ARB
:
1525 case GL_DYNAMIC_READ_ARB
:
1526 case GL_DYNAMIC_COPY_ARB
:
1527 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1531 valid_usage
= false;
1536 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1537 _mesa_lookup_enum_by_nr(usage
));
1541 if (bufObj
->Immutable
) {
1542 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1546 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1547 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1549 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1551 bufObj
->Written
= GL_TRUE
;
1554 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1555 bufObj
->Name
, size
, data
, usage
);
1562 assert(ctx
->Driver
.BufferData
);
1563 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1566 GL_DYNAMIC_STORAGE_BIT
,
1568 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1569 /* From GL_AMD_pinned_memory:
1571 * INVALID_OPERATION is generated by BufferData if <target> is
1572 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1573 * mapped to the GPU address space.
1575 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1578 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1584 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1585 const GLvoid
*data
, GLenum usage
)
1587 GET_CURRENT_CONTEXT(ctx
);
1588 struct gl_buffer_object
*bufObj
;
1590 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1594 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1599 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1602 GET_CURRENT_CONTEXT(ctx
);
1603 struct gl_buffer_object
*bufObj
;
1605 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1609 /* In direct state access, buffer objects have an unspecified target since
1610 * they are not required to be bound.
1612 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1613 "glNamedBufferData");
1618 * Implementation for glBufferSubData and glNamedBufferSubData.
1620 * \param ctx GL context.
1621 * \param bufObj The buffer object.
1622 * \param offset Offset of the first byte of the subdata range.
1623 * \param size Size, in bytes, of the subdata range.
1624 * \param data The data store.
1625 * \param func Name of calling function for recording errors.
1629 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1630 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1633 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1635 /* error already recorded */
1639 if (bufObj
->Immutable
&&
1640 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1641 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1648 bufObj
->Written
= GL_TRUE
;
1650 assert(ctx
->Driver
.BufferSubData
);
1651 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1655 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1656 GLsizeiptr size
, const GLvoid
*data
)
1658 GET_CURRENT_CONTEXT(ctx
);
1659 struct gl_buffer_object
*bufObj
;
1661 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1665 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1669 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1670 GLsizeiptr size
, const GLvoid
*data
)
1672 GET_CURRENT_CONTEXT(ctx
);
1673 struct gl_buffer_object
*bufObj
;
1675 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1679 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1680 "glNamedBufferSubData");
1685 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1686 GLsizeiptr size
, GLvoid
*data
)
1688 GET_CURRENT_CONTEXT(ctx
);
1689 struct gl_buffer_object
*bufObj
;
1691 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1692 GL_INVALID_OPERATION
);
1696 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1697 "glGetBufferSubData")) {
1701 assert(ctx
->Driver
.GetBufferSubData
);
1702 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1706 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1707 GLsizeiptr size
, GLvoid
*data
)
1709 GET_CURRENT_CONTEXT(ctx
);
1710 struct gl_buffer_object
*bufObj
;
1712 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1713 "glGetNamedBufferSubData");
1717 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1718 "glGetNamedBufferSubData")) {
1722 assert(ctx
->Driver
.GetBufferSubData
);
1723 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1728 * \param subdata true if caller is *SubData, false if *Data
1731 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1732 struct gl_buffer_object
*bufObj
,
1733 GLenum internalformat
,
1734 GLintptr offset
, GLsizeiptr size
,
1735 GLenum format
, GLenum type
,
1737 const char *func
, bool subdata
)
1739 mesa_format mesaFormat
;
1740 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1741 GLsizeiptr clearValueSize
;
1743 /* This checks for disallowed mappings. */
1744 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1749 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1750 format
, type
, func
);
1752 if (mesaFormat
== MESA_FORMAT_NONE
) {
1756 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1757 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1758 _mesa_error(ctx
, GL_INVALID_VALUE
,
1759 "%s(offset or size is not a multiple of "
1760 "internalformat size)", func
);
1765 /* clear to zeros, per the spec */
1767 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1768 NULL
, clearValueSize
, bufObj
);
1773 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1774 format
, type
, data
, func
)) {
1779 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1780 clearValue
, clearValueSize
, bufObj
);
1785 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1786 GLenum type
, const GLvoid
*data
)
1788 GET_CURRENT_CONTEXT(ctx
);
1789 struct gl_buffer_object
*bufObj
;
1791 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1795 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1797 "glClearBufferData", false);
1801 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1802 GLenum format
, GLenum type
, const GLvoid
*data
)
1804 GET_CURRENT_CONTEXT(ctx
);
1805 struct gl_buffer_object
*bufObj
;
1807 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1811 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1813 "glClearNamedBufferData", false);
1818 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1819 GLintptr offset
, GLsizeiptr size
,
1820 GLenum format
, GLenum type
,
1823 GET_CURRENT_CONTEXT(ctx
);
1824 struct gl_buffer_object
*bufObj
;
1826 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1830 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1832 "glClearBufferSubData", true);
1836 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1837 GLintptr offset
, GLsizeiptr size
,
1838 GLenum format
, GLenum type
,
1841 GET_CURRENT_CONTEXT(ctx
);
1842 struct gl_buffer_object
*bufObj
;
1844 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1845 "glClearNamedBufferSubData");
1849 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1851 "glClearNamedBufferSubData", true);
1856 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1859 GLboolean status
= GL_TRUE
;
1860 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1862 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
1863 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1864 "%s(buffer is not mapped)", func
);
1869 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1870 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1872 /* check that last 100 bytes are still = magic value */
1873 for (i
= 0; i
< 100; i
++) {
1874 GLuint pos
= bufObj
->Size
- i
- 1;
1875 if (buf
[pos
] != 123) {
1876 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1877 " at position %d (value = %u)\n",
1885 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1886 GLuint i
, unchanged
= 0;
1887 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1889 /* check which bytes changed */
1890 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1891 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1898 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1899 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1904 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
1905 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
1906 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
1907 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
1908 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
1913 GLboolean GLAPIENTRY
1914 _mesa_UnmapBuffer(GLenum target
)
1916 GET_CURRENT_CONTEXT(ctx
);
1917 struct gl_buffer_object
*bufObj
;
1919 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
1923 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
1926 GLboolean GLAPIENTRY
1927 _mesa_UnmapNamedBuffer(GLuint buffer
)
1929 GET_CURRENT_CONTEXT(ctx
);
1930 struct gl_buffer_object
*bufObj
;
1932 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
1936 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
1941 get_buffer_parameter(struct gl_context
*ctx
,
1942 struct gl_buffer_object
*bufObj
, GLenum pname
,
1943 GLint64
*params
, const char *func
)
1946 case GL_BUFFER_SIZE_ARB
:
1947 *params
= bufObj
->Size
;
1949 case GL_BUFFER_USAGE_ARB
:
1950 *params
= bufObj
->Usage
;
1952 case GL_BUFFER_ACCESS_ARB
:
1953 *params
= simplified_access_mode(ctx
,
1954 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
1956 case GL_BUFFER_MAPPED_ARB
:
1957 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
1959 case GL_BUFFER_ACCESS_FLAGS
:
1960 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1962 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
1964 case GL_BUFFER_MAP_OFFSET
:
1965 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1967 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
1969 case GL_BUFFER_MAP_LENGTH
:
1970 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1972 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
1974 case GL_BUFFER_IMMUTABLE_STORAGE
:
1975 if (!ctx
->Extensions
.ARB_buffer_storage
)
1977 *params
= bufObj
->Immutable
;
1979 case GL_BUFFER_STORAGE_FLAGS
:
1980 if (!ctx
->Extensions
.ARB_buffer_storage
)
1982 *params
= bufObj
->StorageFlags
;
1991 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
1992 _mesa_lookup_enum_by_nr(pname
));
1997 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1999 GET_CURRENT_CONTEXT(ctx
);
2000 struct gl_buffer_object
*bufObj
;
2003 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2004 GL_INVALID_OPERATION
);
2008 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2009 "glGetBufferParameteriv"))
2010 return; /* Error already recorded. */
2012 *params
= (GLint
) parameter
;
2016 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2018 GET_CURRENT_CONTEXT(ctx
);
2019 struct gl_buffer_object
*bufObj
;
2022 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2023 GL_INVALID_OPERATION
);
2027 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2028 "glGetBufferParameteri64v"))
2029 return; /* Error already recorded. */
2031 *params
= parameter
;
2035 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2037 GET_CURRENT_CONTEXT(ctx
);
2038 struct gl_buffer_object
*bufObj
;
2041 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2042 "glGetNamedBufferParameteriv");
2046 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2047 "glGetNamedBufferParameteriv"))
2048 return; /* Error already recorded. */
2050 *params
= (GLint
) parameter
;
2054 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2057 GET_CURRENT_CONTEXT(ctx
);
2058 struct gl_buffer_object
*bufObj
;
2061 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2062 "glGetNamedBufferParameteri64v");
2066 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2067 "glGetNamedBufferParameteri64v"))
2068 return; /* Error already recorded. */
2070 *params
= parameter
;
2075 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2077 GET_CURRENT_CONTEXT(ctx
);
2078 struct gl_buffer_object
*bufObj
;
2080 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2081 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2082 "GL_BUFFER_MAP_POINTER)");
2086 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2087 GL_INVALID_OPERATION
);
2091 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2095 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2097 GET_CURRENT_CONTEXT(ctx
);
2098 struct gl_buffer_object
*bufObj
;
2100 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2101 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2102 "GL_BUFFER_MAP_POINTER)");
2106 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2107 "glGetNamedBufferPointerv");
2111 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2116 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2117 struct gl_buffer_object
*src
,
2118 struct gl_buffer_object
*dst
,
2119 GLintptr readOffset
, GLintptr writeOffset
,
2120 GLsizeiptr size
, const char *func
)
2122 if (_mesa_check_disallowed_mapping(src
)) {
2123 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2124 "%s(readBuffer is mapped)", func
);
2128 if (_mesa_check_disallowed_mapping(dst
)) {
2129 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2130 "%s(writeBuffer is mapped)", func
);
2134 if (readOffset
< 0) {
2135 _mesa_error(ctx
, GL_INVALID_VALUE
,
2136 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2140 if (writeOffset
< 0) {
2141 _mesa_error(ctx
, GL_INVALID_VALUE
,
2142 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2147 _mesa_error(ctx
, GL_INVALID_VALUE
,
2148 "%s(size %d < 0)", func
, (int) size
);
2152 if (readOffset
+ size
> src
->Size
) {
2153 _mesa_error(ctx
, GL_INVALID_VALUE
,
2154 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2155 (int) readOffset
, (int) size
, (int) src
->Size
);
2159 if (writeOffset
+ size
> dst
->Size
) {
2160 _mesa_error(ctx
, GL_INVALID_VALUE
,
2161 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2162 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2167 if (readOffset
+ size
<= writeOffset
) {
2170 else if (writeOffset
+ size
<= readOffset
) {
2174 /* overlapping src/dst is illegal */
2175 _mesa_error(ctx
, GL_INVALID_VALUE
,
2176 "%s(overlapping src/dst)", func
);
2181 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2185 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2186 GLintptr readOffset
, GLintptr writeOffset
,
2189 GET_CURRENT_CONTEXT(ctx
);
2190 struct gl_buffer_object
*src
, *dst
;
2192 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2193 GL_INVALID_OPERATION
);
2197 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2198 GL_INVALID_OPERATION
);
2202 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2203 "glCopyBufferSubData");
2207 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2208 GLintptr readOffset
, GLintptr writeOffset
,
2211 GET_CURRENT_CONTEXT(ctx
);
2212 struct gl_buffer_object
*src
, *dst
;
2214 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2215 "glCopyNamedBufferSubData");
2219 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2220 "glCopyNamedBufferSubData");
2224 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2225 "glCopyNamedBufferSubData");
2230 _mesa_map_buffer_range(struct gl_context
*ctx
,
2231 struct gl_buffer_object
*bufObj
,
2232 GLintptr offset
, GLsizeiptr length
,
2233 GLbitfield access
, const char *func
)
2236 GLbitfield allowed_access
;
2238 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2241 _mesa_error(ctx
, GL_INVALID_VALUE
,
2242 "%s(offset %ld < 0)", func
, (long) offset
);
2247 _mesa_error(ctx
, GL_INVALID_VALUE
,
2248 "%s(length %ld < 0)", func
, (long) length
);
2252 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2254 * "An INVALID_OPERATION error is generated for any of the following
2257 * * <length> is zero."
2259 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2260 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2264 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2268 allowed_access
= GL_MAP_READ_BIT
|
2270 GL_MAP_INVALIDATE_RANGE_BIT
|
2271 GL_MAP_INVALIDATE_BUFFER_BIT
|
2272 GL_MAP_FLUSH_EXPLICIT_BIT
|
2273 GL_MAP_UNSYNCHRONIZED_BIT
;
2275 if (ctx
->Extensions
.ARB_buffer_storage
) {
2276 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2277 GL_MAP_COHERENT_BIT
;
2280 if (access
& ~allowed_access
) {
2281 /* generate an error if any bits other than those allowed are set */
2282 _mesa_error(ctx
, GL_INVALID_VALUE
,
2283 "%s(access has undefined bits set)", func
);
2287 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2288 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2289 "%s(access indicates neither read or write)", func
);
2293 if ((access
& GL_MAP_READ_BIT
) &&
2294 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2295 GL_MAP_INVALIDATE_BUFFER_BIT
|
2296 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2297 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2298 "%s(read access with disallowed bits)", func
);
2302 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2303 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2304 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2305 "%s(access has flush explicit without write)", func
);
2309 if (access
& GL_MAP_READ_BIT
&&
2310 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2311 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2312 "%s(buffer does not allow read access)", func
);
2316 if (access
& GL_MAP_WRITE_BIT
&&
2317 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2318 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2319 "%s(buffer does not allow write access)", func
);
2323 if (access
& GL_MAP_COHERENT_BIT
&&
2324 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2325 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2326 "%s(buffer does not allow coherent access)", func
);
2330 if (access
& GL_MAP_PERSISTENT_BIT
&&
2331 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2332 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2333 "%s(buffer does not allow persistent access)", func
);
2337 if (offset
+ length
> bufObj
->Size
) {
2338 _mesa_error(ctx
, GL_INVALID_VALUE
,
2339 "%s(offset %ld + length %ld > buffer_size %ld)", func
,
2340 offset
, length
, bufObj
->Size
);
2344 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2345 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2346 "%s(buffer already mapped)", func
);
2350 if (!bufObj
->Size
) {
2351 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2356 assert(ctx
->Driver
.MapBufferRange
);
2357 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2360 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2363 /* The driver callback should have set all these fields.
2364 * This is important because other modules (like VBO) might call
2365 * the driver function directly.
2367 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2368 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2369 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2370 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2373 if (access
& GL_MAP_WRITE_BIT
)
2374 bufObj
->Written
= GL_TRUE
;
2377 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2378 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2379 bufObj
->Name
, bufObj
->Size
, access
);
2380 /* Access must be write only */
2381 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2383 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2384 for (i
= 0; i
< bufObj
->Size
; i
++)
2391 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2392 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2394 /* buffer is 100 bytes larger than requested, fill with magic value */
2395 for (i
= 0; i
< 100; i
++) {
2396 buf
[bufObj
->Size
- i
- 1] = 123;
2405 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2408 GET_CURRENT_CONTEXT(ctx
);
2409 struct gl_buffer_object
*bufObj
;
2411 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2412 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2413 "glMapBufferRange(ARB_map_buffer_range not supported)");
2417 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2421 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2422 "glMapBufferRange");
2426 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2429 GET_CURRENT_CONTEXT(ctx
);
2430 struct gl_buffer_object
*bufObj
;
2432 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2433 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2434 "glMapNamedBufferRange("
2435 "ARB_map_buffer_range not supported)");
2439 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2443 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2444 "glMapNamedBufferRange");
2448 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2449 * flags for input to _mesa_map_buffer_range.
2451 * \return true if the type of requested access is permissible.
2454 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2458 case GL_READ_ONLY_ARB
:
2459 *flags
= GL_MAP_READ_BIT
;
2460 return _mesa_is_desktop_gl(ctx
);
2461 case GL_WRITE_ONLY_ARB
:
2462 *flags
= GL_MAP_WRITE_BIT
;
2464 case GL_READ_WRITE_ARB
:
2465 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2466 return _mesa_is_desktop_gl(ctx
);
2473 _mesa_MapBuffer(GLenum target
, GLenum access
)
2475 GET_CURRENT_CONTEXT(ctx
);
2476 struct gl_buffer_object
*bufObj
;
2477 GLbitfield accessFlags
;
2479 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2480 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2484 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2488 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2493 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2495 GET_CURRENT_CONTEXT(ctx
);
2496 struct gl_buffer_object
*bufObj
;
2497 GLbitfield accessFlags
;
2499 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2500 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2504 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2508 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2509 "glMapNamedBuffer");
2514 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2515 struct gl_buffer_object
*bufObj
,
2516 GLintptr offset
, GLsizeiptr length
,
2519 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2520 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2521 "%s(ARB_map_buffer_range not supported)", func
);
2526 _mesa_error(ctx
, GL_INVALID_VALUE
,
2527 "%s(offset %ld < 0)", func
, (long) offset
);
2532 _mesa_error(ctx
, GL_INVALID_VALUE
,
2533 "%s(length %ld < 0)", func
, (long) length
);
2537 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2538 /* buffer is not mapped */
2539 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2540 "%s(buffer is not mapped)", func
);
2544 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2545 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2546 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2547 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2551 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2552 _mesa_error(ctx
, GL_INVALID_VALUE
,
2553 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2554 (long) offset
, (long) length
,
2555 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2559 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2561 if (ctx
->Driver
.FlushMappedBufferRange
)
2562 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2567 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2570 GET_CURRENT_CONTEXT(ctx
);
2571 struct gl_buffer_object
*bufObj
;
2573 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2574 GL_INVALID_OPERATION
);
2578 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2579 "glFlushMappedBufferRange");
2583 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2586 GET_CURRENT_CONTEXT(ctx
);
2587 struct gl_buffer_object
*bufObj
;
2589 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2590 "glFlushMappedNamedBufferRange");
2594 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2595 "glFlushMappedNamedBufferRange");
2600 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2602 struct gl_buffer_object
*bufObj
;
2605 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2607 _mesa_error(ctx
, GL_INVALID_VALUE
,
2608 "glObjectPurgeable(name = 0x%x)", name
);
2611 if (!_mesa_is_bufferobj(bufObj
)) {
2612 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
2616 if (bufObj
->Purgeable
) {
2617 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2618 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
2619 return GL_VOLATILE_APPLE
;
2622 bufObj
->Purgeable
= GL_TRUE
;
2624 retval
= GL_VOLATILE_APPLE
;
2625 if (ctx
->Driver
.BufferObjectPurgeable
)
2626 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
2633 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2635 struct gl_renderbuffer
*bufObj
;
2638 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
2640 _mesa_error(ctx
, GL_INVALID_VALUE
,
2641 "glObjectUnpurgeable(name = 0x%x)", name
);
2645 if (bufObj
->Purgeable
) {
2646 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2647 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
2648 return GL_VOLATILE_APPLE
;
2651 bufObj
->Purgeable
= GL_TRUE
;
2653 retval
= GL_VOLATILE_APPLE
;
2654 if (ctx
->Driver
.RenderObjectPurgeable
)
2655 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
2662 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2664 struct gl_texture_object
*bufObj
;
2667 bufObj
= _mesa_lookup_texture(ctx
, name
);
2669 _mesa_error(ctx
, GL_INVALID_VALUE
,
2670 "glObjectPurgeable(name = 0x%x)", name
);
2674 if (bufObj
->Purgeable
) {
2675 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2676 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
2677 return GL_VOLATILE_APPLE
;
2680 bufObj
->Purgeable
= GL_TRUE
;
2682 retval
= GL_VOLATILE_APPLE
;
2683 if (ctx
->Driver
.TextureObjectPurgeable
)
2684 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
2691 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
2695 GET_CURRENT_CONTEXT(ctx
);
2696 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2699 _mesa_error(ctx
, GL_INVALID_VALUE
,
2700 "glObjectPurgeable(name = 0x%x)", name
);
2705 case GL_VOLATILE_APPLE
:
2706 case GL_RELEASED_APPLE
:
2710 _mesa_error(ctx
, GL_INVALID_ENUM
,
2711 "glObjectPurgeable(name = 0x%x) invalid option: %d",
2716 switch (objectType
) {
2718 retval
= texture_object_purgeable(ctx
, name
, option
);
2720 case GL_RENDERBUFFER_EXT
:
2721 retval
= renderbuffer_purgeable(ctx
, name
, option
);
2723 case GL_BUFFER_OBJECT_APPLE
:
2724 retval
= buffer_object_purgeable(ctx
, name
, option
);
2727 _mesa_error(ctx
, GL_INVALID_ENUM
,
2728 "glObjectPurgeable(name = 0x%x) invalid type: %d",
2733 /* In strict conformance to the spec, we must only return VOLATILE when
2734 * when passed the VOLATILE option. Madness.
2736 * XXX First fix the spec, then fix me.
2738 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
2743 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2745 struct gl_buffer_object
*bufObj
;
2748 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2750 _mesa_error(ctx
, GL_INVALID_VALUE
,
2751 "glObjectUnpurgeable(name = 0x%x)", name
);
2755 if (! bufObj
->Purgeable
) {
2756 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2757 "glObjectUnpurgeable(name = 0x%x) object is "
2758 " already \"unpurged\"", name
);
2762 bufObj
->Purgeable
= GL_FALSE
;
2765 if (ctx
->Driver
.BufferObjectUnpurgeable
)
2766 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
2773 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2775 struct gl_renderbuffer
*bufObj
;
2778 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
2780 _mesa_error(ctx
, GL_INVALID_VALUE
,
2781 "glObjectUnpurgeable(name = 0x%x)", name
);
2785 if (! bufObj
->Purgeable
) {
2786 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2787 "glObjectUnpurgeable(name = 0x%x) object is "
2788 " already \"unpurged\"", name
);
2792 bufObj
->Purgeable
= GL_FALSE
;
2795 if (ctx
->Driver
.RenderObjectUnpurgeable
)
2796 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
2803 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2805 struct gl_texture_object
*bufObj
;
2808 bufObj
= _mesa_lookup_texture(ctx
, name
);
2810 _mesa_error(ctx
, GL_INVALID_VALUE
,
2811 "glObjectUnpurgeable(name = 0x%x)", name
);
2815 if (! bufObj
->Purgeable
) {
2816 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2817 "glObjectUnpurgeable(name = 0x%x) object is"
2818 " already \"unpurged\"", name
);
2822 bufObj
->Purgeable
= GL_FALSE
;
2825 if (ctx
->Driver
.TextureObjectUnpurgeable
)
2826 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
2833 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
2835 GET_CURRENT_CONTEXT(ctx
);
2836 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2839 _mesa_error(ctx
, GL_INVALID_VALUE
,
2840 "glObjectUnpurgeable(name = 0x%x)", name
);
2845 case GL_RETAINED_APPLE
:
2846 case GL_UNDEFINED_APPLE
:
2850 _mesa_error(ctx
, GL_INVALID_ENUM
,
2851 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2856 switch (objectType
) {
2857 case GL_BUFFER_OBJECT_APPLE
:
2858 return buffer_object_unpurgeable(ctx
, name
, option
);
2860 return texture_object_unpurgeable(ctx
, name
, option
);
2861 case GL_RENDERBUFFER_EXT
:
2862 return renderbuffer_unpurgeable(ctx
, name
, option
);
2864 _mesa_error(ctx
, GL_INVALID_ENUM
,
2865 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2873 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
2874 GLenum pname
, GLint
*params
)
2876 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2878 _mesa_error(ctx
, GL_INVALID_VALUE
,
2879 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
2884 case GL_PURGEABLE_APPLE
:
2885 *params
= bufObj
->Purgeable
;
2888 _mesa_error(ctx
, GL_INVALID_ENUM
,
2889 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2897 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
2898 GLenum pname
, GLint
*params
)
2900 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
2902 _mesa_error(ctx
, GL_INVALID_VALUE
,
2903 "glObjectUnpurgeable(name = 0x%x)", name
);
2908 case GL_PURGEABLE_APPLE
:
2909 *params
= rb
->Purgeable
;
2912 _mesa_error(ctx
, GL_INVALID_ENUM
,
2913 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2921 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
2922 GLenum pname
, GLint
*params
)
2924 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
2926 _mesa_error(ctx
, GL_INVALID_VALUE
,
2927 "glObjectUnpurgeable(name = 0x%x)", name
);
2932 case GL_PURGEABLE_APPLE
:
2933 *params
= texObj
->Purgeable
;
2936 _mesa_error(ctx
, GL_INVALID_ENUM
,
2937 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2945 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
2948 GET_CURRENT_CONTEXT(ctx
);
2951 _mesa_error(ctx
, GL_INVALID_VALUE
,
2952 "glGetObjectParameteriv(name = 0x%x)", name
);
2956 switch (objectType
) {
2958 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
2960 case GL_BUFFER_OBJECT_APPLE
:
2961 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
2963 case GL_RENDERBUFFER_EXT
:
2964 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
2967 _mesa_error(ctx
, GL_INVALID_ENUM
,
2968 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2974 * Binds a buffer object to a uniform buffer binding point.
2976 * The caller is responsible for flushing vertices and updating
2980 set_ubo_binding(struct gl_context
*ctx
,
2981 struct gl_uniform_buffer_binding
*binding
,
2982 struct gl_buffer_object
*bufObj
,
2987 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2989 binding
->Offset
= offset
;
2990 binding
->Size
= size
;
2991 binding
->AutomaticSize
= autoSize
;
2993 /* If this is a real buffer object, mark it has having been used
2994 * at some point as a UBO.
2997 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
3001 * Binds a buffer object to a uniform buffer binding point.
3003 * Unlike set_ubo_binding(), this function also flushes vertices
3004 * and updates NewDriverState. It also checks if the binding
3005 * has actually changed before updating it.
3008 bind_uniform_buffer(struct gl_context
*ctx
,
3010 struct gl_buffer_object
*bufObj
,
3015 struct gl_uniform_buffer_binding
*binding
=
3016 &ctx
->UniformBufferBindings
[index
];
3018 if (binding
->BufferObject
== bufObj
&&
3019 binding
->Offset
== offset
&&
3020 binding
->Size
== size
&&
3021 binding
->AutomaticSize
== autoSize
) {
3025 FLUSH_VERTICES(ctx
, 0);
3026 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3028 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
3032 * Bind a region of a buffer object to a uniform block binding point.
3033 * \param index the uniform buffer binding point index
3034 * \param bufObj the buffer object
3035 * \param offset offset to the start of buffer object region
3036 * \param size size of the buffer object region
3039 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
3041 struct gl_buffer_object
*bufObj
,
3045 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3046 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3050 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3051 _mesa_error(ctx
, GL_INVALID_VALUE
,
3052 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3053 ctx
->Const
.UniformBufferOffsetAlignment
);
3057 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3062 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3063 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3068 * Bind a buffer object to a uniform block binding point.
3069 * As above, but offset = 0.
3072 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
3074 struct gl_buffer_object
*bufObj
)
3076 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3077 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
3081 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3083 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3084 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
3086 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
3090 * Binds a buffer object to an atomic buffer binding point.
3092 * The caller is responsible for validating the offset,
3093 * flushing the vertices and updating NewDriverState.
3096 set_atomic_buffer_binding(struct gl_context
*ctx
,
3097 struct gl_atomic_buffer_binding
*binding
,
3098 struct gl_buffer_object
*bufObj
,
3102 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
3104 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3105 binding
->Offset
= -1;
3108 binding
->Offset
= offset
;
3109 binding
->Size
= size
;
3110 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
3115 * Binds a buffer object to an atomic buffer binding point.
3117 * Unlike set_atomic_buffer_binding(), this function also validates the
3118 * index and offset, flushes vertices, and updates NewDriverState.
3119 * It also checks if the binding has actually changing before
3123 bind_atomic_buffer(struct gl_context
*ctx
,
3125 struct gl_buffer_object
*bufObj
,
3130 struct gl_atomic_buffer_binding
*binding
;
3132 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3133 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3137 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3138 _mesa_error(ctx
, GL_INVALID_VALUE
,
3139 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3140 ATOMIC_COUNTER_SIZE
);
3144 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3146 binding
= &ctx
->AtomicBufferBindings
[index
];
3147 if (binding
->BufferObject
== bufObj
&&
3148 binding
->Offset
== offset
&&
3149 binding
->Size
== size
) {
3153 FLUSH_VERTICES(ctx
, 0);
3154 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3156 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3160 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3162 const GLintptr
*offsets
,
3163 const GLsizeiptr
*sizes
)
3165 if (offsets
[index
] < 0) {
3166 /* The ARB_multi_bind spec says:
3168 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3169 * value in <offsets> is less than zero (per binding)."
3171 _mesa_error(ctx
, GL_INVALID_VALUE
,
3172 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3173 index
, (int64_t) offsets
[index
]);
3177 if (sizes
[index
] <= 0) {
3178 /* The ARB_multi_bind spec says:
3180 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3181 * value in <sizes> is less than or equal to zero (per binding)."
3183 _mesa_error(ctx
, GL_INVALID_VALUE
,
3184 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3185 index
, (int64_t) sizes
[index
]);
3193 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3194 GLuint first
, GLsizei count
,
3197 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3198 _mesa_error(ctx
, GL_INVALID_ENUM
,
3199 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3203 /* The ARB_multi_bind_spec says:
3205 * "An INVALID_OPERATION error is generated if <first> + <count> is
3206 * greater than the number of target-specific indexed binding points,
3207 * as described in section 6.7.1."
3209 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3210 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3211 "%s(first=%u + count=%d > the value of "
3212 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3213 caller
, first
, count
,
3214 ctx
->Const
.MaxUniformBufferBindings
);
3222 * Unbind all uniform buffers in the range
3223 * <first> through <first>+<count>-1
3226 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3228 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3231 for (i
= 0; i
< count
; i
++)
3232 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3233 bufObj
, -1, -1, GL_TRUE
);
3237 bind_uniform_buffers_base(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3238 const GLuint
*buffers
)
3242 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, "glBindBuffersBase"))
3245 /* Assume that at least one binding will be changed */
3246 FLUSH_VERTICES(ctx
, 0);
3247 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3250 /* The ARB_multi_bind spec says:
3252 * "If <buffers> is NULL, all bindings from <first> through
3253 * <first>+<count>-1 are reset to their unbound (zero) state."
3255 unbind_uniform_buffers(ctx
, first
, count
);
3259 /* Note that the error semantics for multi-bind commands differ from
3260 * those of other GL commands.
3262 * The Issues section in the ARB_multi_bind spec says:
3264 * "(11) Typically, OpenGL specifies that if an error is generated by a
3265 * command, that command has no effect. This is somewhat
3266 * unfortunate for multi-bind commands, because it would require a
3267 * first pass to scan the entire list of bound objects for errors
3268 * and then a second pass to actually perform the bindings.
3269 * Should we have different error semantics?
3271 * RESOLVED: Yes. In this specification, when the parameters for
3272 * one of the <count> binding points are invalid, that binding point
3273 * is not updated and an error will be generated. However, other
3274 * binding points in the same command will be updated if their
3275 * parameters are valid and no other error occurs."
3278 _mesa_begin_bufferobj_lookups(ctx
);
3280 for (i
= 0; i
< count
; i
++) {
3281 struct gl_uniform_buffer_binding
*binding
=
3282 &ctx
->UniformBufferBindings
[first
+ i
];
3283 struct gl_buffer_object
*bufObj
;
3285 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3286 bufObj
= binding
->BufferObject
;
3288 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3289 "glBindBuffersBase");
3292 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3293 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_TRUE
);
3295 set_ubo_binding(ctx
, binding
, bufObj
, 0, 0, GL_TRUE
);
3299 _mesa_end_bufferobj_lookups(ctx
);
3303 bind_uniform_buffers_range(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3304 const GLuint
*buffers
,
3305 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3309 if (!error_check_bind_uniform_buffers(ctx
, first
, count
,
3310 "glBindBuffersRange"))
3313 /* Assume that at least one binding will be changed */
3314 FLUSH_VERTICES(ctx
, 0);
3315 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3318 /* The ARB_multi_bind spec says:
3320 * "If <buffers> is NULL, all bindings from <first> through
3321 * <first>+<count>-1 are reset to their unbound (zero) state.
3322 * In this case, the offsets and sizes associated with the
3323 * binding points are set to default values, ignoring
3324 * <offsets> and <sizes>."
3326 unbind_uniform_buffers(ctx
, first
, count
);
3330 /* Note that the error semantics for multi-bind commands differ from
3331 * those of other GL commands.
3333 * The Issues section in the ARB_multi_bind spec says:
3335 * "(11) Typically, OpenGL specifies that if an error is generated by a
3336 * command, that command has no effect. This is somewhat
3337 * unfortunate for multi-bind commands, because it would require a
3338 * first pass to scan the entire list of bound objects for errors
3339 * and then a second pass to actually perform the bindings.
3340 * Should we have different error semantics?
3342 * RESOLVED: Yes. In this specification, when the parameters for
3343 * one of the <count> binding points are invalid, that binding point
3344 * is not updated and an error will be generated. However, other
3345 * binding points in the same command will be updated if their
3346 * parameters are valid and no other error occurs."
3349 _mesa_begin_bufferobj_lookups(ctx
);
3351 for (i
= 0; i
< count
; i
++) {
3352 struct gl_uniform_buffer_binding
*binding
=
3353 &ctx
->UniformBufferBindings
[first
+ i
];
3354 struct gl_buffer_object
*bufObj
;
3356 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3359 /* The ARB_multi_bind spec says:
3361 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3362 * pair of values in <offsets> and <sizes> does not respectively
3363 * satisfy the constraints described for those parameters for the
3364 * specified target, as described in section 6.7.1 (per binding)."
3366 * Section 6.7.1 refers to table 6.5, which says:
3368 * "┌───────────────────────────────────────────────────────────────┐
3369 * │ Uniform buffer array bindings (see sec. 7.6) │
3370 * ├─────────────────────┬─────────────────────────────────────────┤
3372 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3373 * │ │ OFFSET_ALIGNMENT │
3375 * │ size restriction │ none │
3376 * └─────────────────────┴─────────────────────────────────────────┘"
3378 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3379 _mesa_error(ctx
, GL_INVALID_VALUE
,
3380 "glBindBuffersRange(offsets[%u]=%" PRId64
3381 " is misaligned; it must be a multiple of the value of "
3382 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3383 "target=GL_UNIFORM_BUFFER)",
3384 i
, (int64_t) offsets
[i
],
3385 ctx
->Const
.UniformBufferOffsetAlignment
);
3389 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3390 bufObj
= binding
->BufferObject
;
3392 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3393 "glBindBuffersRange");
3396 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3397 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_FALSE
);
3399 set_ubo_binding(ctx
, binding
, bufObj
,
3400 offsets
[i
], sizes
[i
], GL_FALSE
);
3404 _mesa_end_bufferobj_lookups(ctx
);
3408 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3409 struct gl_transform_feedback_object
*tfObj
,
3410 GLuint first
, GLsizei count
, const char *caller
)
3412 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3413 _mesa_error(ctx
, GL_INVALID_ENUM
,
3414 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3418 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3420 * "An INVALID_OPERATION error is generated :
3423 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3424 * FEEDBACK_BUFFER and transform feedback is currently active."
3426 * We assume that this is also meant to apply to BindBuffersRange
3427 * and BindBuffersBase.
3429 if (tfObj
->Active
) {
3430 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3431 "%s(Changing transform feedback buffers while "
3432 "transform feedback is active)", caller
);
3436 /* The ARB_multi_bind_spec says:
3438 * "An INVALID_OPERATION error is generated if <first> + <count> is
3439 * greater than the number of target-specific indexed binding points,
3440 * as described in section 6.7.1."
3442 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3443 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3444 "%s(first=%u + count=%d > the value of "
3445 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3446 caller
, first
, count
,
3447 ctx
->Const
.MaxTransformFeedbackBuffers
);
3455 * Unbind all transform feedback buffers in the range
3456 * <first> through <first>+<count>-1
3459 unbind_xfb_buffers(struct gl_context
*ctx
,
3460 struct gl_transform_feedback_object
*tfObj
,
3461 GLuint first
, GLsizei count
)
3463 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3466 for (i
= 0; i
< count
; i
++)
3467 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3472 bind_xfb_buffers_base(struct gl_context
*ctx
,
3473 GLuint first
, GLsizei count
,
3474 const GLuint
*buffers
)
3476 struct gl_transform_feedback_object
*tfObj
=
3477 ctx
->TransformFeedback
.CurrentObject
;
3480 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3481 "glBindBuffersBase"))
3484 /* Assume that at least one binding will be changed */
3485 FLUSH_VERTICES(ctx
, 0);
3486 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3489 /* The ARB_multi_bind spec says:
3491 * "If <buffers> is NULL, all bindings from <first> through
3492 * <first>+<count>-1 are reset to their unbound (zero) state."
3494 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3498 /* Note that the error semantics for multi-bind commands differ from
3499 * those of other GL commands.
3501 * The Issues section in the ARB_multi_bind spec says:
3503 * "(11) Typically, OpenGL specifies that if an error is generated by a
3504 * command, that command has no effect. This is somewhat
3505 * unfortunate for multi-bind commands, because it would require a
3506 * first pass to scan the entire list of bound objects for errors
3507 * and then a second pass to actually perform the bindings.
3508 * Should we have different error semantics?
3510 * RESOLVED: Yes. In this specification, when the parameters for
3511 * one of the <count> binding points are invalid, that binding point
3512 * is not updated and an error will be generated. However, other
3513 * binding points in the same command will be updated if their
3514 * parameters are valid and no other error occurs."
3517 _mesa_begin_bufferobj_lookups(ctx
);
3519 for (i
= 0; i
< count
; i
++) {
3520 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[first
+ i
];
3521 struct gl_buffer_object
*bufObj
;
3523 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3524 bufObj
= boundBufObj
;
3526 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3527 "glBindBuffersBase");
3530 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3534 _mesa_end_bufferobj_lookups(ctx
);
3538 bind_xfb_buffers_range(struct gl_context
*ctx
,
3539 GLuint first
, GLsizei count
,
3540 const GLuint
*buffers
,
3541 const GLintptr
*offsets
,
3542 const GLsizeiptr
*sizes
)
3544 struct gl_transform_feedback_object
*tfObj
=
3545 ctx
->TransformFeedback
.CurrentObject
;
3548 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3549 "glBindBuffersRange"))
3552 /* Assume that at least one binding will be changed */
3553 FLUSH_VERTICES(ctx
, 0);
3554 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3557 /* The ARB_multi_bind spec says:
3559 * "If <buffers> is NULL, all bindings from <first> through
3560 * <first>+<count>-1 are reset to their unbound (zero) state.
3561 * In this case, the offsets and sizes associated with the
3562 * binding points are set to default values, ignoring
3563 * <offsets> and <sizes>."
3565 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3569 /* Note that the error semantics for multi-bind commands differ from
3570 * those of other GL commands.
3572 * The Issues section in the ARB_multi_bind spec says:
3574 * "(11) Typically, OpenGL specifies that if an error is generated by a
3575 * command, that command has no effect. This is somewhat
3576 * unfortunate for multi-bind commands, because it would require a
3577 * first pass to scan the entire list of bound objects for errors
3578 * and then a second pass to actually perform the bindings.
3579 * Should we have different error semantics?
3581 * RESOLVED: Yes. In this specification, when the parameters for
3582 * one of the <count> binding points are invalid, that binding point
3583 * is not updated and an error will be generated. However, other
3584 * binding points in the same command will be updated if their
3585 * parameters are valid and no other error occurs."
3588 _mesa_begin_bufferobj_lookups(ctx
);
3590 for (i
= 0; i
< count
; i
++) {
3591 const GLuint index
= first
+ i
;
3592 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3593 struct gl_buffer_object
*bufObj
;
3595 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3598 /* The ARB_multi_bind spec says:
3600 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3601 * pair of values in <offsets> and <sizes> does not respectively
3602 * satisfy the constraints described for those parameters for the
3603 * specified target, as described in section 6.7.1 (per binding)."
3605 * Section 6.7.1 refers to table 6.5, which says:
3607 * "┌───────────────────────────────────────────────────────────────┐
3608 * │ Transform feedback array bindings (see sec. 13.2.2) │
3609 * ├───────────────────────┬───────────────────────────────────────┤
3611 * │ offset restriction │ multiple of 4 │
3613 * │ size restriction │ multiple of 4 │
3614 * └───────────────────────┴───────────────────────────────────────┘"
3616 if (offsets
[i
] & 0x3) {
3617 _mesa_error(ctx
, GL_INVALID_VALUE
,
3618 "glBindBuffersRange(offsets[%u]=%" PRId64
3619 " is misaligned; it must be a multiple of 4 when "
3620 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3621 i
, (int64_t) offsets
[i
]);
3625 if (sizes
[i
] & 0x3) {
3626 _mesa_error(ctx
, GL_INVALID_VALUE
,
3627 "glBindBuffersRange(sizes[%u]=%" PRId64
3628 " is misaligned; it must be a multiple of 4 when "
3629 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3630 i
, (int64_t) sizes
[i
]);
3634 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3635 bufObj
= boundBufObj
;
3637 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3638 "glBindBuffersRange");
3641 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3642 offsets
[i
], sizes
[i
]);
3645 _mesa_end_bufferobj_lookups(ctx
);
3649 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3650 GLuint first
, GLsizei count
,
3653 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3654 _mesa_error(ctx
, GL_INVALID_ENUM
,
3655 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3659 /* The ARB_multi_bind_spec says:
3661 * "An INVALID_OPERATION error is generated if <first> + <count> is
3662 * greater than the number of target-specific indexed binding points,
3663 * as described in section 6.7.1."
3665 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3666 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3667 "%s(first=%u + count=%d > the value of "
3668 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3669 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3677 * Unbind all atomic counter buffers in the range
3678 * <first> through <first>+<count>-1
3681 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3683 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3686 for (i
= 0; i
< count
; i
++)
3687 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3692 bind_atomic_buffers_base(struct gl_context
*ctx
,
3695 const GLuint
*buffers
)
3699 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
3700 "glBindBuffersBase"))
3703 /* Assume that at least one binding will be changed */
3704 FLUSH_VERTICES(ctx
, 0);
3705 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3708 /* The ARB_multi_bind spec says:
3710 * "If <buffers> is NULL, all bindings from <first> through
3711 * <first>+<count>-1 are reset to their unbound (zero) state."
3713 unbind_atomic_buffers(ctx
, first
, count
);
3717 /* Note that the error semantics for multi-bind commands differ from
3718 * those of other GL commands.
3720 * The Issues section in the ARB_multi_bind spec says:
3722 * "(11) Typically, OpenGL specifies that if an error is generated by a
3723 * command, that command has no effect. This is somewhat
3724 * unfortunate for multi-bind commands, because it would require a
3725 * first pass to scan the entire list of bound objects for errors
3726 * and then a second pass to actually perform the bindings.
3727 * Should we have different error semantics?
3729 * RESOLVED: Yes. In this specification, when the parameters for
3730 * one of the <count> binding points are invalid, that binding point
3731 * is not updated and an error will be generated. However, other
3732 * binding points in the same command will be updated if their
3733 * parameters are valid and no other error occurs."
3736 _mesa_begin_bufferobj_lookups(ctx
);
3738 for (i
= 0; i
< count
; i
++) {
3739 struct gl_atomic_buffer_binding
*binding
=
3740 &ctx
->AtomicBufferBindings
[first
+ i
];
3741 struct gl_buffer_object
*bufObj
;
3743 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3744 bufObj
= binding
->BufferObject
;
3746 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3747 "glBindBuffersBase");
3750 set_atomic_buffer_binding(ctx
, binding
, bufObj
, 0, 0);
3753 _mesa_end_bufferobj_lookups(ctx
);
3757 bind_atomic_buffers_range(struct gl_context
*ctx
,
3760 const GLuint
*buffers
,
3761 const GLintptr
*offsets
,
3762 const GLsizeiptr
*sizes
)
3766 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
3767 "glBindBuffersRange"))
3770 /* Assume that at least one binding will be changed */
3771 FLUSH_VERTICES(ctx
, 0);
3772 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3775 /* The ARB_multi_bind spec says:
3777 * "If <buffers> is NULL, all bindings from <first> through
3778 * <first>+<count>-1 are reset to their unbound (zero) state.
3779 * In this case, the offsets and sizes associated with the
3780 * binding points are set to default values, ignoring
3781 * <offsets> and <sizes>."
3783 unbind_atomic_buffers(ctx
, first
, count
);
3787 /* Note that the error semantics for multi-bind commands differ from
3788 * those of other GL commands.
3790 * The Issues section in the ARB_multi_bind spec says:
3792 * "(11) Typically, OpenGL specifies that if an error is generated by a
3793 * command, that command has no effect. This is somewhat
3794 * unfortunate for multi-bind commands, because it would require a
3795 * first pass to scan the entire list of bound objects for errors
3796 * and then a second pass to actually perform the bindings.
3797 * Should we have different error semantics?
3799 * RESOLVED: Yes. In this specification, when the parameters for
3800 * one of the <count> binding points are invalid, that binding point
3801 * is not updated and an error will be generated. However, other
3802 * binding points in the same command will be updated if their
3803 * parameters are valid and no other error occurs."
3806 _mesa_begin_bufferobj_lookups(ctx
);
3808 for (i
= 0; i
< count
; i
++) {
3809 struct gl_atomic_buffer_binding
*binding
=
3810 &ctx
->AtomicBufferBindings
[first
+ i
];
3811 struct gl_buffer_object
*bufObj
;
3813 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3816 /* The ARB_multi_bind spec says:
3818 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3819 * pair of values in <offsets> and <sizes> does not respectively
3820 * satisfy the constraints described for those parameters for the
3821 * specified target, as described in section 6.7.1 (per binding)."
3823 * Section 6.7.1 refers to table 6.5, which says:
3825 * "┌───────────────────────────────────────────────────────────────┐
3826 * │ Atomic counter array bindings (see sec. 7.7.2) │
3827 * ├───────────────────────┬───────────────────────────────────────┤
3829 * │ offset restriction │ multiple of 4 │
3831 * │ size restriction │ none │
3832 * └───────────────────────┴───────────────────────────────────────┘"
3834 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3835 _mesa_error(ctx
, GL_INVALID_VALUE
,
3836 "glBindBuffersRange(offsets[%u]=%" PRId64
3837 " is misaligned; it must be a multiple of %d when "
3838 "target=GL_ATOMIC_COUNTER_BUFFER)",
3839 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3843 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3844 bufObj
= binding
->BufferObject
;
3846 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3847 "glBindBuffersRange");
3850 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offsets
[i
], sizes
[i
]);
3853 _mesa_end_bufferobj_lookups(ctx
);
3857 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3858 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3860 GET_CURRENT_CONTEXT(ctx
);
3861 struct gl_buffer_object
*bufObj
;
3864 bufObj
= ctx
->Shared
->NullBufferObj
;
3866 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3868 if (!_mesa_handle_bind_buffer_gen(ctx
, target
, buffer
,
3869 &bufObj
, "glBindBufferRange"))
3873 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3874 "glBindBufferRange(invalid buffer=%u)", buffer
);
3880 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3887 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3888 _mesa_bind_buffer_range_transform_feedback(ctx
,
3889 ctx
->TransformFeedback
.CurrentObject
,
3890 index
, bufObj
, offset
, size
);
3892 case GL_UNIFORM_BUFFER
:
3893 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3895 case GL_ATOMIC_COUNTER_BUFFER
:
3896 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3897 "glBindBufferRange");
3900 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3906 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3908 GET_CURRENT_CONTEXT(ctx
);
3909 struct gl_buffer_object
*bufObj
;
3912 bufObj
= ctx
->Shared
->NullBufferObj
;
3914 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3916 if (!_mesa_handle_bind_buffer_gen(ctx
, target
, buffer
,
3917 &bufObj
, "glBindBufferBase"))
3921 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3922 "glBindBufferBase(invalid buffer=%u)", buffer
);
3926 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3927 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3929 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3930 * zero and size equal to the size of buffer."
3932 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3934 * "If the parameter (starting offset or size) was not specified when the
3935 * buffer object was bound, zero is returned."
3937 * What happens if the size of the buffer changes? Does the size of the
3938 * buffer at the moment glBindBufferBase was called still play a role, like
3939 * the first quote would imply, or is the size meaningless in the
3940 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3941 * core spec page 45 says:
3943 * "It is equivalent to calling BindBufferRange with offset zero, while
3944 * size is determined by the size of the bound buffer at the time the
3947 * My interpretation is that the GL 4.1 spec was a clarification of the
3948 * behavior, not a change. In particular, this choice will only make
3949 * rendering work in cases where it would have had undefined results.
3953 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3954 _mesa_bind_buffer_base_transform_feedback(ctx
,
3955 ctx
->TransformFeedback
.CurrentObject
,
3956 index
, bufObj
, false);
3958 case GL_UNIFORM_BUFFER
:
3959 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
3961 case GL_ATOMIC_COUNTER_BUFFER
:
3962 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
3963 "glBindBufferBase");
3966 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
3972 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
3973 const GLuint
*buffers
,
3974 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3976 GET_CURRENT_CONTEXT(ctx
);
3979 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3980 bind_xfb_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
3982 case GL_UNIFORM_BUFFER
:
3983 bind_uniform_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
3985 case GL_ATOMIC_COUNTER_BUFFER
:
3986 bind_atomic_buffers_range(ctx
, first
, count
, buffers
,
3990 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
3991 _mesa_lookup_enum_by_nr(target
));
3997 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
3998 const GLuint
*buffers
)
4000 GET_CURRENT_CONTEXT(ctx
);
4003 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4004 bind_xfb_buffers_base(ctx
, first
, count
, buffers
);
4006 case GL_UNIFORM_BUFFER
:
4007 bind_uniform_buffers_base(ctx
, first
, count
, buffers
);
4009 case GL_ATOMIC_COUNTER_BUFFER
:
4010 bind_atomic_buffers_base(ctx
, first
, count
, buffers
);
4013 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4014 _mesa_lookup_enum_by_nr(target
));
4020 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4023 GET_CURRENT_CONTEXT(ctx
);
4024 struct gl_buffer_object
*bufObj
;
4025 const GLintptr end
= offset
+ length
;
4027 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4029 _mesa_error(ctx
, GL_INVALID_VALUE
,
4030 "glInvalidateBufferSubData(name = 0x%x) invalid object",
4035 /* The GL_ARB_invalidate_subdata spec says:
4037 * "An INVALID_VALUE error is generated if <offset> or <length> is
4038 * negative, or if <offset> + <length> is greater than the value of
4041 if (end
< 0 || end
> bufObj
->Size
) {
4042 _mesa_error(ctx
, GL_INVALID_VALUE
,
4043 "glInvalidateBufferSubData(invalid offset or length)");
4047 /* The OpenGL 4.4 (Core Profile) spec says:
4049 * "An INVALID_OPERATION error is generated if buffer is currently
4050 * mapped by MapBuffer or if the invalidate range intersects the range
4051 * currently mapped by MapBufferRange, unless it was mapped
4052 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4054 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4055 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4056 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4057 "glInvalidateBufferSubData(intersection with mapped "
4062 /* We don't actually do anything for this yet. Just return after
4063 * validating the parameters and generating the required errors.
4069 _mesa_InvalidateBufferData(GLuint buffer
)
4071 GET_CURRENT_CONTEXT(ctx
);
4072 struct gl_buffer_object
*bufObj
;
4074 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4076 _mesa_error(ctx
, GL_INVALID_VALUE
,
4077 "glInvalidateBufferData(name = 0x%x) invalid object",
4082 /* The OpenGL 4.4 (Core Profile) spec says:
4084 * "An INVALID_OPERATION error is generated if buffer is currently
4085 * mapped by MapBuffer or if the invalidate range intersects the range
4086 * currently mapped by MapBufferRange, unless it was mapped
4087 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4089 if (_mesa_check_disallowed_mapping(bufObj
)) {
4090 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4091 "glInvalidateBufferData(intersection with mapped "
4096 /* We don't actually do anything for this yet. Just return after
4097 * validating the parameters and generating the required errors.