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
40 #include "bufferobj.h"
45 #include "glformats.h"
47 #include "transformfeedback.h"
53 /*#define BOUNDS_CHECK*/
57 * Used as a placeholder for buffer objects between glGenBuffers() and
58 * glBindBuffer() so that glIsBuffer() can work correctly.
60 static struct gl_buffer_object DummyBufferObject
;
64 * Return pointer to address of a buffer object target.
65 * \param ctx the GL context
66 * \param target the buffer object target to be retrieved.
67 * \return pointer to pointer to the buffer object bound to \c target in the
68 * specified context or \c NULL if \c target is invalid.
70 static inline struct gl_buffer_object
**
71 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
73 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
75 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
76 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
80 case GL_ARRAY_BUFFER_ARB
:
81 return &ctx
->Array
.ArrayBufferObj
;
82 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
83 return &ctx
->Array
.VAO
->IndexBufferObj
;
84 case GL_PIXEL_PACK_BUFFER_EXT
:
85 return &ctx
->Pack
.BufferObj
;
86 case GL_PIXEL_UNPACK_BUFFER_EXT
:
87 return &ctx
->Unpack
.BufferObj
;
88 case GL_COPY_READ_BUFFER
:
89 return &ctx
->CopyReadBuffer
;
90 case GL_COPY_WRITE_BUFFER
:
91 return &ctx
->CopyWriteBuffer
;
92 case GL_DRAW_INDIRECT_BUFFER
:
93 if (ctx
->API
== API_OPENGL_CORE
&&
94 ctx
->Extensions
.ARB_draw_indirect
) {
95 return &ctx
->DrawIndirectBuffer
;
98 case GL_TRANSFORM_FEEDBACK_BUFFER
:
99 if (ctx
->Extensions
.EXT_transform_feedback
) {
100 return &ctx
->TransformFeedback
.CurrentBuffer
;
103 case GL_TEXTURE_BUFFER
:
104 if (ctx
->API
== API_OPENGL_CORE
&&
105 ctx
->Extensions
.ARB_texture_buffer_object
) {
106 return &ctx
->Texture
.BufferObject
;
109 case GL_UNIFORM_BUFFER
:
110 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
111 return &ctx
->UniformBuffer
;
114 case GL_ATOMIC_COUNTER_BUFFER
:
115 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
116 return &ctx
->AtomicBuffer
;
127 * Get the buffer object bound to the specified target in a GL context.
128 * \param ctx the GL context
129 * \param target the buffer object target to be retrieved.
130 * \param error the GL error to record if target is illegal.
131 * \return pointer to the buffer object bound to \c target in the
132 * specified context or \c NULL if \c target is invalid.
134 static inline struct gl_buffer_object
*
135 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
138 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
141 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
145 if (!_mesa_is_bufferobj(*bufObj
)) {
146 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
155 * Convert a GLbitfield describing the mapped buffer access flags
156 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
159 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
161 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
162 if ((access
& rwFlags
) == rwFlags
)
163 return GL_READ_WRITE
;
164 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
166 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
167 return GL_WRITE_ONLY
;
169 /* Otherwise, AccessFlags is zero (the default state).
171 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
173 * Name Type Initial Value Legal Values
175 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
178 * However, table 6.8 in the GL_OES_mapbuffer extension says:
180 * Get Value Type Get Command Value Description
181 * --------- ---- ----------- ----- -----------
182 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
184 * The difference is because GL_OES_mapbuffer only supports mapping buffers
189 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
194 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
196 * The regions do not overlap if and only if the end of the given
197 * region is before the mapped region or the start of the given region
198 * is after the mapped region.
200 * \param obj Buffer object target on which to operate.
201 * \param offset Offset of the first byte of the subdata range.
202 * \param size Size, in bytes, of the subdata range.
203 * \return true if ranges overlap, false otherwise
207 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
208 GLintptr offset
, GLsizeiptr size
)
210 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
211 const GLintptr end
= offset
+ size
;
212 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
213 obj
->Mappings
[MAP_USER
].Length
;
215 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
224 * Tests the subdata range parameters and sets the GL error code for
225 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
226 * \c glClearBufferSubData.
228 * \param ctx GL context.
229 * \param target Buffer object target on which to operate.
230 * \param offset Offset of the first byte of the subdata range.
231 * \param size Size, in bytes, of the subdata range.
232 * \param mappedRange If true, checks if an overlapping range is mapped.
233 * If false, checks if buffer is mapped.
234 * \param errorNoBuffer Error code if no buffer is bound to target.
235 * \param caller Name of calling function for recording errors.
236 * \return A pointer to the buffer object bound to \c target in the
237 * specified context or \c NULL if any of the parameter or state
238 * conditions are invalid.
240 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
242 static struct gl_buffer_object
*
243 buffer_object_subdata_range_good(struct gl_context
* ctx
, GLenum target
,
244 GLintptrARB offset
, GLsizeiptrARB size
,
245 bool mappedRange
, GLenum errorNoBuffer
,
248 struct gl_buffer_object
*bufObj
;
251 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
256 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
260 bufObj
= get_buffer(ctx
, caller
, target
, errorNoBuffer
);
264 if (offset
+ size
> bufObj
->Size
) {
265 _mesa_error(ctx
, GL_INVALID_VALUE
,
266 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
267 (unsigned long) offset
,
268 (unsigned long) size
,
269 (unsigned long) bufObj
->Size
);
273 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
277 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
278 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
283 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
284 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
294 * Test the format and type parameters and set the GL error code for
295 * \c glClearBufferData and \c glClearBufferSubData.
297 * \param ctx GL context.
298 * \param internalformat Format to which the data is to be converted.
299 * \param format Format of the supplied data.
300 * \param type Type of the supplied data.
301 * \param caller Name of calling function for recording errors.
302 * \return If internalformat, format and type are legal the mesa_format
303 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
305 * \sa glClearBufferData and glClearBufferSubData
308 validate_clear_buffer_format(struct gl_context
*ctx
,
309 GLenum internalformat
,
310 GLenum format
, GLenum type
,
313 mesa_format mesaFormat
;
314 GLenum errorFormatType
;
316 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
317 if (mesaFormat
== MESA_FORMAT_NONE
) {
318 _mesa_error(ctx
, GL_INVALID_ENUM
,
319 "%s(invalid internalformat)", caller
);
320 return MESA_FORMAT_NONE
;
323 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
324 * EXT_texture_integer there is no conversion between integer and
325 * non-integer formats
327 if (_mesa_is_enum_format_signed_int(format
) !=
328 _mesa_is_format_integer_color(mesaFormat
)) {
329 _mesa_error(ctx
, GL_INVALID_OPERATION
,
330 "%s(integer vs non-integer)", caller
);
331 return MESA_FORMAT_NONE
;
334 if (!_mesa_is_color_format(format
)) {
335 _mesa_error(ctx
, GL_INVALID_ENUM
,
336 "%s(format is not a color format)", caller
);
337 return MESA_FORMAT_NONE
;
340 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
341 if (errorFormatType
!= GL_NO_ERROR
) {
342 _mesa_error(ctx
, GL_INVALID_ENUM
,
343 "%s(invalid format or type)", caller
);
344 return MESA_FORMAT_NONE
;
352 * Convert user-specified clear value to the specified internal format.
354 * \param ctx GL context.
355 * \param internalformat Format to which the data is converted.
356 * \param clearValue Points to the converted clear value.
357 * \param format Format of the supplied data.
358 * \param type Type of the supplied data.
359 * \param data Data which is to be converted to internalformat.
360 * \param caller Name of calling function for recording errors.
361 * \return true if data could be converted, false otherwise.
363 * \sa glClearBufferData, glClearBufferSubData
366 convert_clear_buffer_data(struct gl_context
*ctx
,
367 mesa_format internalformat
,
368 GLubyte
*clearValue
, GLenum format
, GLenum type
,
369 const GLvoid
*data
, const char *caller
)
371 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
373 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
374 0, &clearValue
, 1, 1, 1,
375 format
, type
, data
, &ctx
->Unpack
)) {
379 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
386 * Allocate and initialize a new buffer object.
388 * Default callback for the \c dd_function_table::NewBufferObject() hook.
390 static struct gl_buffer_object
*
391 _mesa_new_buffer_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
393 struct gl_buffer_object
*obj
;
397 obj
= MALLOC_STRUCT(gl_buffer_object
);
398 _mesa_initialize_buffer_object(ctx
, obj
, name
, target
);
404 * Delete a buffer object.
406 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
409 _mesa_delete_buffer_object(struct gl_context
*ctx
,
410 struct gl_buffer_object
*bufObj
)
414 _mesa_align_free(bufObj
->Data
);
416 /* assign strange values here to help w/ debugging */
417 bufObj
->RefCount
= -1000;
420 mtx_destroy(&bufObj
->Mutex
);
428 * Set ptr to bufObj w/ reference counting.
429 * This is normally only called from the _mesa_reference_buffer_object() macro
430 * when there's a real pointer change.
433 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
434 struct gl_buffer_object
**ptr
,
435 struct gl_buffer_object
*bufObj
)
438 /* Unreference the old buffer */
439 GLboolean deleteFlag
= GL_FALSE
;
440 struct gl_buffer_object
*oldObj
= *ptr
;
442 mtx_lock(&oldObj
->Mutex
);
443 ASSERT(oldObj
->RefCount
> 0);
446 printf("BufferObj %p %d DECR to %d\n",
447 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
449 deleteFlag
= (oldObj
->RefCount
== 0);
450 mtx_unlock(&oldObj
->Mutex
);
454 /* some sanity checking: don't delete a buffer still in use */
456 /* unfortunately, these tests are invalid during context tear-down */
457 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
458 ASSERT(ctx
->Array
.VAO
->IndexBufferObj
!= bufObj
);
459 ASSERT(ctx
->Array
.VAO
->Vertex
.BufferObj
!= bufObj
);
462 ASSERT(ctx
->Driver
.DeleteBuffer
);
463 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
471 /* reference new buffer */
472 mtx_lock(&bufObj
->Mutex
);
473 if (bufObj
->RefCount
== 0) {
474 /* this buffer's being deleted (look just above) */
475 /* Not sure this can every really happen. Warn if it does. */
476 _mesa_problem(NULL
, "referencing deleted buffer object");
482 printf("BufferObj %p %d INCR to %d\n",
483 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
487 mtx_unlock(&bufObj
->Mutex
);
493 * Initialize a buffer object to default values.
496 _mesa_initialize_buffer_object( struct gl_context
*ctx
,
497 struct gl_buffer_object
*obj
,
498 GLuint name
, GLenum target
)
502 memset(obj
, 0, sizeof(struct gl_buffer_object
));
503 mtx_init(&obj
->Mutex
, mtx_plain
);
506 obj
->Usage
= GL_STATIC_DRAW_ARB
;
512 * Callback called from _mesa_HashWalk()
515 count_buffer_size(GLuint key
, void *data
, void *userData
)
517 const struct gl_buffer_object
*bufObj
=
518 (const struct gl_buffer_object
*) data
;
519 GLuint
*total
= (GLuint
*) userData
;
521 *total
= *total
+ bufObj
->Size
;
526 * Compute total size (in bytes) of all buffer objects for the given context.
527 * For debugging purposes.
530 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
534 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
541 * Allocate space for and store data in a buffer object. Any data that was
542 * previously stored in the buffer object is lost. If \c data is \c NULL,
543 * memory will be allocated, but no copy will occur.
545 * This is the default callback for \c dd_function_table::BufferData()
546 * Note that all GL error checking will have been done already.
548 * \param ctx GL context.
549 * \param target Buffer object target on which to operate.
550 * \param size Size, in bytes, of the new data store.
551 * \param data Pointer to the data to store in the buffer object. This
552 * pointer may be \c NULL.
553 * \param usage Hints about how the data will be used.
554 * \param bufObj Object to be used.
556 * \return GL_TRUE for success, GL_FALSE for failure
557 * \sa glBufferDataARB, dd_function_table::BufferData.
560 _mesa_buffer_data( struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
561 const GLvoid
* data
, GLenum usage
, GLenum storageFlags
,
562 struct gl_buffer_object
* bufObj
)
568 _mesa_align_free( bufObj
->Data
);
570 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
572 bufObj
->Data
= (GLubyte
*) new_data
;
574 bufObj
->Usage
= usage
;
575 bufObj
->StorageFlags
= storageFlags
;
578 memcpy( bufObj
->Data
, data
, size
);
590 * Replace data in a subrange of buffer object. If the data range
591 * specified by \c size + \c offset extends beyond the end of the buffer or
592 * if \c data is \c NULL, no copy is performed.
594 * This is the default callback for \c dd_function_table::BufferSubData()
595 * Note that all GL error checking will have been done already.
597 * \param ctx GL context.
598 * \param offset Offset of the first byte to be modified.
599 * \param size Size, in bytes, of the data range.
600 * \param data Pointer to the data to store in the buffer object.
601 * \param bufObj Object to be used.
603 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
606 _mesa_buffer_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
607 GLsizeiptrARB size
, const GLvoid
* data
,
608 struct gl_buffer_object
* bufObj
)
612 /* this should have been caught in _mesa_BufferSubData() */
613 ASSERT(size
+ offset
<= bufObj
->Size
);
616 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
622 * Retrieve data from a subrange of buffer object. If the data range
623 * specified by \c size + \c offset extends beyond the end of the buffer or
624 * if \c data is \c NULL, no copy is performed.
626 * This is the default callback for \c dd_function_table::GetBufferSubData()
627 * Note that all GL error checking will have been done already.
629 * \param ctx GL context.
630 * \param target Buffer object target on which to operate.
631 * \param offset Offset of the first byte to be fetched.
632 * \param size Size, in bytes, of the data range.
633 * \param data Destination for data
634 * \param bufObj Object to be used.
636 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
639 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
640 GLsizeiptrARB size
, GLvoid
* data
,
641 struct gl_buffer_object
* bufObj
)
645 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
646 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
652 * Clear a subrange of the buffer object with copies of the supplied data.
653 * If data is NULL the buffer is filled with zeros.
655 * This is the default callback for \c dd_function_table::ClearBufferSubData()
656 * Note that all GL error checking will have been done already.
658 * \param ctx GL context.
659 * \param offset Offset of the first byte to be cleared.
660 * \param size Size, in bytes, of the to be cleared range.
661 * \param clearValue Source of the data.
662 * \param clearValueSize Size, in bytes, of the supplied data.
663 * \param bufObj Object to be cleared.
665 * \sa glClearBufferSubData, glClearBufferData and
666 * dd_function_table::ClearBufferSubData.
669 _mesa_buffer_clear_subdata(struct gl_context
*ctx
,
670 GLintptr offset
, GLsizeiptr size
,
671 const GLvoid
*clearValue
,
672 GLsizeiptr clearValueSize
,
673 struct gl_buffer_object
*bufObj
)
678 ASSERT(ctx
->Driver
.MapBufferRange
);
679 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
681 GL_MAP_INVALIDATE_RANGE_BIT
,
682 bufObj
, MAP_INTERNAL
);
685 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
689 if (clearValue
== NULL
) {
690 /* Clear with zeros, per the spec */
691 memset(dest
, 0, size
);
692 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
696 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
697 memcpy(dest
, clearValue
, clearValueSize
);
698 dest
+= clearValueSize
;
701 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
706 * Default fallback for \c dd_function_table::MapBufferRange().
707 * Called via glMapBufferRange().
710 _mesa_buffer_map_range( struct gl_context
*ctx
, GLintptr offset
,
711 GLsizeiptr length
, GLbitfield access
,
712 struct gl_buffer_object
*bufObj
,
713 gl_map_buffer_index index
)
716 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
717 /* Just return a direct pointer to the data */
718 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
719 bufObj
->Mappings
[index
].Length
= length
;
720 bufObj
->Mappings
[index
].Offset
= offset
;
721 bufObj
->Mappings
[index
].AccessFlags
= access
;
722 return bufObj
->Mappings
[index
].Pointer
;
727 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
728 * Called via glFlushMappedBufferRange().
731 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
,
732 GLintptr offset
, GLsizeiptr length
,
733 struct gl_buffer_object
*obj
,
734 gl_map_buffer_index index
)
745 * Default callback for \c dd_function_table::MapBuffer().
747 * The input parameters will have been already tested for errors.
749 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
752 _mesa_buffer_unmap(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
753 gl_map_buffer_index index
)
756 /* XXX we might assert here that bufObj->Pointer is non-null */
757 bufObj
->Mappings
[index
].Pointer
= NULL
;
758 bufObj
->Mappings
[index
].Length
= 0;
759 bufObj
->Mappings
[index
].Offset
= 0;
760 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
766 * Default fallback for \c dd_function_table::CopyBufferSubData().
767 * Called via glCopyBufferSubData().
770 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
771 struct gl_buffer_object
*src
,
772 struct gl_buffer_object
*dst
,
773 GLintptr readOffset
, GLintptr writeOffset
,
776 GLubyte
*srcPtr
, *dstPtr
;
779 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
781 GL_MAP_WRITE_BIT
, src
,
787 srcPtr
+= readOffset
;
788 dstPtr
+= writeOffset
;
790 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
791 GL_MAP_READ_BIT
, src
,
793 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
795 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
799 /* Note: the src and dst regions will never overlap. Trying to do so
800 * would generate GL_INVALID_VALUE earlier.
802 if (srcPtr
&& dstPtr
)
803 memcpy(dstPtr
, srcPtr
, size
);
805 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
807 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
813 * Initialize the state associated with buffer objects
816 _mesa_init_buffer_objects( struct gl_context
*ctx
)
820 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
821 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
822 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
824 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
825 ctx
->Shared
->NullBufferObj
);
827 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
828 ctx
->Shared
->NullBufferObj
);
829 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
830 ctx
->Shared
->NullBufferObj
);
832 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
833 ctx
->Shared
->NullBufferObj
);
835 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
836 ctx
->Shared
->NullBufferObj
);
838 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
839 _mesa_reference_buffer_object(ctx
,
840 &ctx
->UniformBufferBindings
[i
].BufferObject
,
841 ctx
->Shared
->NullBufferObj
);
842 ctx
->UniformBufferBindings
[i
].Offset
= -1;
843 ctx
->UniformBufferBindings
[i
].Size
= -1;
849 _mesa_free_buffer_objects( struct gl_context
*ctx
)
853 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
855 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
856 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
858 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
860 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
862 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
863 _mesa_reference_buffer_object(ctx
,
864 &ctx
->UniformBufferBindings
[i
].BufferObject
,
870 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
873 struct gl_buffer_object
**buf_handle
,
876 struct gl_buffer_object
*buf
= *buf_handle
;
878 if (!buf
&& ctx
->API
== API_OPENGL_CORE
) {
879 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
883 if (!buf
|| buf
== &DummyBufferObject
) {
884 /* If this is a new buffer object id, or one which was generated but
885 * never used before, allocate a buffer object now.
887 ASSERT(ctx
->Driver
.NewBufferObject
);
888 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
890 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
893 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
901 * Bind the specified target to buffer for the specified context.
902 * Called by glBindBuffer() and other functions.
905 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
907 struct gl_buffer_object
*oldBufObj
;
908 struct gl_buffer_object
*newBufObj
= NULL
;
909 struct gl_buffer_object
**bindTarget
= NULL
;
911 bindTarget
= get_buffer_target(ctx
, target
);
913 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
917 /* Get pointer to old buffer object (to be unbound) */
918 oldBufObj
= *bindTarget
;
919 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
920 return; /* rebinding the same buffer object- no change */
923 * Get pointer to new buffer object (newBufObj)
926 /* The spec says there's not a buffer object named 0, but we use
927 * one internally because it simplifies things.
929 newBufObj
= ctx
->Shared
->NullBufferObj
;
932 /* non-default buffer object */
933 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
934 if (!_mesa_handle_bind_buffer_gen(ctx
, target
, buffer
,
935 &newBufObj
, "glBindBuffer"))
939 /* bind new buffer */
940 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
945 * Update the default buffer objects in the given context to reference those
946 * specified in the shared state and release those referencing the old
950 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
952 /* Bind the NullBufferObj to remove references to those
953 * in the shared context hash table.
955 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
956 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
957 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
958 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
964 * Return the gl_buffer_object for the given ID.
965 * Always return NULL for ID 0.
967 struct gl_buffer_object
*
968 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
973 return (struct gl_buffer_object
*)
974 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
979 * If *ptr points to obj, set ptr = the Null/default buffer object.
980 * This is a helper for buffer object deletion.
981 * The GL spec says that deleting a buffer object causes it to get
982 * unbound from all arrays in the current context.
985 unbind(struct gl_context
*ctx
,
986 struct gl_buffer_object
**ptr
,
987 struct gl_buffer_object
*obj
)
990 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
996 * Plug default/fallback buffer object functions into the device
1000 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1002 /* GL_ARB_vertex/pixel_buffer_object */
1003 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1004 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1005 driver
->BufferData
= _mesa_buffer_data
;
1006 driver
->BufferSubData
= _mesa_buffer_subdata
;
1007 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1008 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
1010 /* GL_ARB_clear_buffer_object */
1011 driver
->ClearBufferSubData
= _mesa_buffer_clear_subdata
;
1013 /* GL_ARB_map_buffer_range */
1014 driver
->MapBufferRange
= _mesa_buffer_map_range
;
1015 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
1017 /* GL_ARB_copy_buffer */
1018 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
1023 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1024 struct gl_buffer_object
*bufObj
)
1028 for (i
= 0; i
< MAP_COUNT
; i
++) {
1029 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1030 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1031 ASSERT(bufObj
->Mappings
[i
].Pointer
== NULL
);
1032 bufObj
->Mappings
[i
].AccessFlags
= 0;
1038 /**********************************************************************/
1040 /**********************************************************************/
1043 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1045 GET_CURRENT_CONTEXT(ctx
);
1047 if (MESA_VERBOSE
& VERBOSE_API
)
1048 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1049 _mesa_lookup_enum_by_nr(target
), buffer
);
1051 bind_buffer_object(ctx
, target
, buffer
);
1056 * Delete a set of buffer objects.
1058 * \param n Number of buffer objects to delete.
1059 * \param ids Array of \c n buffer object IDs.
1062 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1064 GET_CURRENT_CONTEXT(ctx
);
1066 FLUSH_VERTICES(ctx
, 0);
1069 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1073 mtx_lock(&ctx
->Shared
->Mutex
);
1075 for (i
= 0; i
< n
; i
++) {
1076 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
1078 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1081 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1083 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1085 /* unbind any vertex pointers bound to this buffer */
1086 for (j
= 0; j
< Elements(vao
->VertexBinding
); j
++) {
1087 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1090 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1091 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1093 if (vao
->IndexBufferObj
== bufObj
) {
1094 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1097 /* unbind ARB_draw_indirect binding point */
1098 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1099 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1102 /* unbind ARB_copy_buffer binding points */
1103 if (ctx
->CopyReadBuffer
== bufObj
) {
1104 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1106 if (ctx
->CopyWriteBuffer
== bufObj
) {
1107 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1110 /* unbind transform feedback binding points */
1111 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1112 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1114 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1115 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1116 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1120 /* unbind UBO binding points */
1121 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1122 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1123 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1127 if (ctx
->UniformBuffer
== bufObj
) {
1128 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1131 /* unbind any pixel pack/unpack pointers bound to this buffer */
1132 if (ctx
->Pack
.BufferObj
== bufObj
) {
1133 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1135 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1136 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1139 if (ctx
->Texture
.BufferObject
== bufObj
) {
1140 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1143 /* The ID is immediately freed for re-use */
1144 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1145 /* Make sure we do not run into the classic ABA problem on bind.
1146 * We don't want to allow re-binding a buffer object that's been
1147 * "deleted" by glDeleteBuffers().
1149 * The explicit rebinding to the default object in the current context
1150 * prevents the above in the current context, but another context
1151 * sharing the same objects might suffer from this problem.
1152 * The alternative would be to do the hash lookup in any case on bind
1153 * which would introduce more runtime overhead than this.
1155 bufObj
->DeletePending
= GL_TRUE
;
1156 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1160 mtx_unlock(&ctx
->Shared
->Mutex
);
1165 * Generate a set of unique buffer object IDs and store them in \c buffer.
1167 * \param n Number of IDs to generate.
1168 * \param buffer Array of \c n locations to store the IDs.
1171 _mesa_GenBuffers(GLsizei n
, GLuint
*buffer
)
1173 GET_CURRENT_CONTEXT(ctx
);
1177 if (MESA_VERBOSE
& VERBOSE_API
)
1178 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
1181 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
1190 * This must be atomic (generation and allocation of buffer object IDs)
1192 mtx_lock(&ctx
->Shared
->Mutex
);
1194 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1196 /* Insert the ID and pointer to dummy buffer object into hash table */
1197 for (i
= 0; i
< n
; i
++) {
1198 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
1199 &DummyBufferObject
);
1200 buffer
[i
] = first
+ i
;
1203 mtx_unlock(&ctx
->Shared
->Mutex
);
1208 * Determine if ID is the name of a buffer object.
1210 * \param id ID of the potential buffer object.
1211 * \return \c GL_TRUE if \c id is the name of a buffer object,
1212 * \c GL_FALSE otherwise.
1214 GLboolean GLAPIENTRY
1215 _mesa_IsBuffer(GLuint id
)
1217 struct gl_buffer_object
*bufObj
;
1218 GET_CURRENT_CONTEXT(ctx
);
1219 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1221 mtx_lock(&ctx
->Shared
->Mutex
);
1222 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1223 mtx_unlock(&ctx
->Shared
->Mutex
);
1225 return bufObj
&& bufObj
!= &DummyBufferObject
;
1230 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1233 GET_CURRENT_CONTEXT(ctx
);
1234 struct gl_buffer_object
*bufObj
;
1237 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferStorage(size <= 0)");
1241 if (flags
& ~(GL_MAP_READ_BIT
|
1243 GL_MAP_PERSISTENT_BIT
|
1244 GL_MAP_COHERENT_BIT
|
1245 GL_DYNAMIC_STORAGE_BIT
|
1246 GL_CLIENT_STORAGE_BIT
)) {
1247 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferStorage(flags)");
1251 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1252 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1253 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferStorage(flags!=READ/WRITE)");
1257 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1258 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferStorage(flags!=PERSISTENT)");
1262 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1266 if (bufObj
->Immutable
) {
1267 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferStorage(immutable)");
1271 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1272 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1274 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1276 bufObj
->Written
= GL_TRUE
;
1277 bufObj
->Immutable
= GL_TRUE
;
1279 ASSERT(ctx
->Driver
.BufferData
);
1280 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1282 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferStorage()");
1288 _mesa_BufferData(GLenum target
, GLsizeiptrARB size
,
1289 const GLvoid
* data
, GLenum usage
)
1291 GET_CURRENT_CONTEXT(ctx
);
1292 struct gl_buffer_object
*bufObj
;
1295 if (MESA_VERBOSE
& VERBOSE_API
)
1296 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
1297 _mesa_lookup_enum_by_nr(target
),
1298 (long int) size
, data
,
1299 _mesa_lookup_enum_by_nr(usage
));
1302 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1307 case GL_STREAM_DRAW_ARB
:
1308 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1311 case GL_STATIC_DRAW_ARB
:
1312 case GL_DYNAMIC_DRAW_ARB
:
1316 case GL_STREAM_READ_ARB
:
1317 case GL_STREAM_COPY_ARB
:
1318 case GL_STATIC_READ_ARB
:
1319 case GL_STATIC_COPY_ARB
:
1320 case GL_DYNAMIC_READ_ARB
:
1321 case GL_DYNAMIC_COPY_ARB
:
1322 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1326 valid_usage
= false;
1331 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferData(usage)");
1335 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
, GL_INVALID_OPERATION
);
1339 if (bufObj
->Immutable
) {
1340 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferData(immutable)");
1344 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1345 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1347 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1349 bufObj
->Written
= GL_TRUE
;
1352 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1353 bufObj
->Name
, size
, data
, usage
);
1360 ASSERT(ctx
->Driver
.BufferData
);
1361 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1364 GL_DYNAMIC_STORAGE_BIT
,
1366 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1372 _mesa_BufferSubData(GLenum target
, GLintptrARB offset
,
1373 GLsizeiptrARB size
, const GLvoid
* data
)
1375 GET_CURRENT_CONTEXT(ctx
);
1376 struct gl_buffer_object
*bufObj
;
1378 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1379 false, GL_INVALID_OPERATION
,
1380 "glBufferSubDataARB" );
1382 /* error already recorded */
1386 if (bufObj
->Immutable
&&
1387 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1388 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferSubData");
1395 bufObj
->Written
= GL_TRUE
;
1397 ASSERT(ctx
->Driver
.BufferSubData
);
1398 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
1403 _mesa_GetBufferSubData(GLenum target
, GLintptrARB offset
,
1404 GLsizeiptrARB size
, void * data
)
1406 GET_CURRENT_CONTEXT(ctx
);
1407 struct gl_buffer_object
*bufObj
;
1409 bufObj
= buffer_object_subdata_range_good(ctx
, target
, offset
, size
,
1410 false, GL_INVALID_OPERATION
,
1411 "glGetBufferSubDataARB");
1413 /* error already recorded */
1417 ASSERT(ctx
->Driver
.GetBufferSubData
);
1418 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1423 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1424 GLenum type
, const GLvoid
* data
)
1426 GET_CURRENT_CONTEXT(ctx
);
1427 struct gl_buffer_object
* bufObj
;
1428 mesa_format mesaFormat
;
1429 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1430 GLsizeiptr clearValueSize
;
1432 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1437 if (_mesa_check_disallowed_mapping(bufObj
)) {
1438 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1439 "glClearBufferData(buffer currently mapped)");
1443 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1445 "glClearBufferData");
1446 if (mesaFormat
== MESA_FORMAT_NONE
) {
1450 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1451 if (bufObj
->Size
% clearValueSize
!= 0) {
1452 _mesa_error(ctx
, GL_INVALID_VALUE
,
1453 "glClearBufferData(size is not a multiple of "
1454 "internalformat size)");
1459 /* clear to zeros, per the spec */
1460 ctx
->Driver
.ClearBufferSubData(ctx
, 0, bufObj
->Size
,
1461 NULL
, clearValueSize
, bufObj
);
1465 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1466 format
, type
, data
, "glClearBufferData")) {
1470 ctx
->Driver
.ClearBufferSubData(ctx
, 0, bufObj
->Size
,
1471 clearValue
, clearValueSize
, bufObj
);
1476 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1477 GLintptr offset
, GLsizeiptr size
,
1478 GLenum format
, GLenum type
,
1481 GET_CURRENT_CONTEXT(ctx
);
1482 struct gl_buffer_object
* bufObj
;
1483 mesa_format mesaFormat
;
1484 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1485 GLsizeiptr clearValueSize
;
1487 bufObj
= buffer_object_subdata_range_good(ctx
, target
, offset
, size
,
1488 true, GL_INVALID_VALUE
,
1489 "glClearBufferSubData");
1494 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1496 "glClearBufferSubData");
1497 if (mesaFormat
== MESA_FORMAT_NONE
) {
1501 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1502 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1503 _mesa_error(ctx
, GL_INVALID_VALUE
,
1504 "glClearBufferSubData(offset or size is not a multiple of "
1505 "internalformat size)");
1510 /* clear to zeros, per the spec */
1512 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1513 NULL
, clearValueSize
, bufObj
);
1518 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1520 "glClearBufferSubData")) {
1525 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1526 clearValue
, clearValueSize
, bufObj
);
1532 _mesa_MapBuffer(GLenum target
, GLenum access
)
1534 GET_CURRENT_CONTEXT(ctx
);
1535 struct gl_buffer_object
* bufObj
;
1536 GLbitfield accessFlags
;
1540 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1543 case GL_READ_ONLY_ARB
:
1544 accessFlags
= GL_MAP_READ_BIT
;
1545 valid_access
= _mesa_is_desktop_gl(ctx
);
1547 case GL_WRITE_ONLY_ARB
:
1548 accessFlags
= GL_MAP_WRITE_BIT
;
1549 valid_access
= true;
1551 case GL_READ_WRITE_ARB
:
1552 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1553 valid_access
= _mesa_is_desktop_gl(ctx
);
1556 valid_access
= false;
1560 if (!valid_access
) {
1561 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1565 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
, GL_INVALID_OPERATION
);
1569 if (accessFlags
& GL_MAP_READ_BIT
&&
1570 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
1571 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1572 "glMapBuffer(invalid read flag)");
1576 if (accessFlags
& GL_MAP_WRITE_BIT
&&
1577 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
1578 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1579 "glMapBuffer(invalid write flag)");
1583 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
1584 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1588 if (!bufObj
->Size
) {
1589 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1590 "glMapBuffer(buffer size = 0)");
1594 ASSERT(ctx
->Driver
.MapBufferRange
);
1595 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
,
1598 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1602 /* The driver callback should have set these fields.
1603 * This is important because other modules (like VBO) might call
1604 * the driver function directly.
1606 ASSERT(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
1607 ASSERT(bufObj
->Mappings
[MAP_USER
].Length
== bufObj
->Size
);
1608 ASSERT(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
1609 bufObj
->Mappings
[MAP_USER
].AccessFlags
= accessFlags
;
1612 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1613 bufObj
->Written
= GL_TRUE
;
1616 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1617 bufObj
->Name
, bufObj
->Size
, access
);
1618 if (access
== GL_WRITE_ONLY_ARB
) {
1620 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1621 for (i
= 0; i
< bufObj
->Size
; i
++)
1628 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1630 /* buffer is 100 bytes larger than requested, fill with magic value */
1631 for (i
= 0; i
< 100; i
++) {
1632 buf
[bufObj
->Size
- i
- 1] = 123;
1637 return bufObj
->Mappings
[MAP_USER
].Pointer
;
1641 GLboolean GLAPIENTRY
1642 _mesa_UnmapBuffer(GLenum target
)
1644 GET_CURRENT_CONTEXT(ctx
);
1645 struct gl_buffer_object
*bufObj
;
1646 GLboolean status
= GL_TRUE
;
1647 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1649 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
, GL_INVALID_OPERATION
);
1653 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
1654 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1659 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1660 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1662 /* check that last 100 bytes are still = magic value */
1663 for (i
= 0; i
< 100; i
++) {
1664 GLuint pos
= bufObj
->Size
- i
- 1;
1665 if (buf
[pos
] != 123) {
1666 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1667 " at position %d (value = %u)\n",
1675 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1676 GLuint i
, unchanged
= 0;
1677 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1679 /* check which bytes changed */
1680 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1681 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1688 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1689 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1694 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
1695 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
1696 ASSERT(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
1697 ASSERT(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
1698 ASSERT(bufObj
->Mappings
[MAP_USER
].Length
== 0);
1705 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1707 GET_CURRENT_CONTEXT(ctx
);
1708 struct gl_buffer_object
*bufObj
;
1710 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
,
1711 GL_INVALID_OPERATION
);
1716 case GL_BUFFER_SIZE_ARB
:
1717 *params
= (GLint
) bufObj
->Size
;
1719 case GL_BUFFER_USAGE_ARB
:
1720 *params
= bufObj
->Usage
;
1722 case GL_BUFFER_ACCESS_ARB
:
1723 *params
= simplified_access_mode(ctx
,
1724 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
1726 case GL_BUFFER_MAPPED_ARB
:
1727 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
1729 case GL_BUFFER_ACCESS_FLAGS
:
1730 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1732 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
1734 case GL_BUFFER_MAP_OFFSET
:
1735 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1737 *params
= (GLint
) bufObj
->Mappings
[MAP_USER
].Offset
;
1739 case GL_BUFFER_MAP_LENGTH
:
1740 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1742 *params
= (GLint
) bufObj
->Mappings
[MAP_USER
].Length
;
1744 case GL_BUFFER_IMMUTABLE_STORAGE
:
1745 if (!ctx
->Extensions
.ARB_buffer_storage
)
1747 *params
= bufObj
->Immutable
;
1749 case GL_BUFFER_STORAGE_FLAGS
:
1750 if (!ctx
->Extensions
.ARB_buffer_storage
)
1752 *params
= bufObj
->StorageFlags
;
1755 ; /* fall-through */
1759 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1760 _mesa_lookup_enum_by_nr(pname
));
1766 * This is pretty much a duplicate of GetBufferParameteriv() but the
1767 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1770 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1772 GET_CURRENT_CONTEXT(ctx
);
1773 struct gl_buffer_object
*bufObj
;
1775 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
1776 GL_INVALID_OPERATION
);
1781 case GL_BUFFER_SIZE_ARB
:
1782 *params
= bufObj
->Size
;
1784 case GL_BUFFER_USAGE_ARB
:
1785 *params
= bufObj
->Usage
;
1787 case GL_BUFFER_ACCESS_ARB
:
1788 *params
= simplified_access_mode(ctx
,
1789 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
1791 case GL_BUFFER_ACCESS_FLAGS
:
1792 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1794 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
1796 case GL_BUFFER_MAPPED_ARB
:
1797 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
1799 case GL_BUFFER_MAP_OFFSET
:
1800 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1802 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
1804 case GL_BUFFER_MAP_LENGTH
:
1805 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1807 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
1809 case GL_BUFFER_IMMUTABLE_STORAGE
:
1810 if (!ctx
->Extensions
.ARB_buffer_storage
)
1812 *params
= bufObj
->Immutable
;
1814 case GL_BUFFER_STORAGE_FLAGS
:
1815 if (!ctx
->Extensions
.ARB_buffer_storage
)
1817 *params
= bufObj
->StorageFlags
;
1820 ; /* fall-through */
1824 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1825 _mesa_lookup_enum_by_nr(pname
));
1830 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
1832 GET_CURRENT_CONTEXT(ctx
);
1833 struct gl_buffer_object
* bufObj
;
1835 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1836 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1840 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
,
1841 GL_INVALID_OPERATION
);
1845 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
1850 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1851 GLintptr readOffset
, GLintptr writeOffset
,
1854 GET_CURRENT_CONTEXT(ctx
);
1855 struct gl_buffer_object
*src
, *dst
;
1857 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
1858 GL_INVALID_OPERATION
);
1862 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
1863 GL_INVALID_OPERATION
);
1867 if (_mesa_check_disallowed_mapping(src
)) {
1868 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1869 "glCopyBufferSubData(readBuffer is mapped)");
1873 if (_mesa_check_disallowed_mapping(dst
)) {
1874 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1875 "glCopyBufferSubData(writeBuffer is mapped)");
1879 if (readOffset
< 0) {
1880 _mesa_error(ctx
, GL_INVALID_VALUE
,
1881 "glCopyBufferSubData(readOffset = %d)", (int) readOffset
);
1885 if (writeOffset
< 0) {
1886 _mesa_error(ctx
, GL_INVALID_VALUE
,
1887 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset
);
1892 _mesa_error(ctx
, GL_INVALID_VALUE
,
1893 "glCopyBufferSubData(writeOffset = %d)", (int) size
);
1897 if (readOffset
+ size
> src
->Size
) {
1898 _mesa_error(ctx
, GL_INVALID_VALUE
,
1899 "glCopyBufferSubData(readOffset + size = %d)",
1900 (int) (readOffset
+ size
));
1904 if (writeOffset
+ size
> dst
->Size
) {
1905 _mesa_error(ctx
, GL_INVALID_VALUE
,
1906 "glCopyBufferSubData(writeOffset + size = %d)",
1907 (int) (writeOffset
+ size
));
1912 if (readOffset
+ size
<= writeOffset
) {
1915 else if (writeOffset
+ size
<= readOffset
) {
1919 /* overlapping src/dst is illegal */
1920 _mesa_error(ctx
, GL_INVALID_VALUE
,
1921 "glCopyBufferSubData(overlapping src/dst)");
1926 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1931 * See GL_ARB_map_buffer_range spec
1934 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1937 GET_CURRENT_CONTEXT(ctx
);
1938 struct gl_buffer_object
*bufObj
;
1940 GLbitfield allowed_access
;
1942 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1944 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1945 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1946 "glMapBufferRange(extension not supported)");
1951 _mesa_error(ctx
, GL_INVALID_VALUE
,
1952 "glMapBufferRange(offset = %ld)", (long)offset
);
1957 _mesa_error(ctx
, GL_INVALID_VALUE
,
1958 "glMapBufferRange(length = %ld)", (long)length
);
1962 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
1964 * "An INVALID_OPERATION error is generated for any of the following
1967 * * <length> is zero."
1969 if (_mesa_is_gles(ctx
) && length
== 0) {
1970 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1971 "glMapBufferRange(length = 0)");
1975 allowed_access
= GL_MAP_READ_BIT
|
1977 GL_MAP_INVALIDATE_RANGE_BIT
|
1978 GL_MAP_INVALIDATE_BUFFER_BIT
|
1979 GL_MAP_FLUSH_EXPLICIT_BIT
|
1980 GL_MAP_UNSYNCHRONIZED_BIT
;
1982 if (ctx
->Extensions
.ARB_buffer_storage
) {
1983 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
1984 GL_MAP_COHERENT_BIT
;
1987 if (access
& ~allowed_access
) {
1988 /* generate an error if any other than allowed bit is set */
1989 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1993 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1994 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1995 "glMapBufferRange(access indicates neither read or write)");
1999 if ((access
& GL_MAP_READ_BIT
) &&
2000 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2001 GL_MAP_INVALIDATE_BUFFER_BIT
|
2002 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2003 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2004 "glMapBufferRange(invalid access flags)");
2008 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2009 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2010 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2011 "glMapBufferRange(invalid access flags)");
2015 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2019 if (access
& GL_MAP_READ_BIT
&&
2020 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2021 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2022 "glMapBufferRange(invalid read flag)");
2026 if (access
& GL_MAP_WRITE_BIT
&&
2027 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2028 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2029 "glMapBufferRange(invalid write flag)");
2033 if (access
& GL_MAP_COHERENT_BIT
&&
2034 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2035 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2036 "glMapBufferRange(invalid coherent flag)");
2040 if (access
& GL_MAP_PERSISTENT_BIT
&&
2041 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2042 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2043 "glMapBufferRange(invalid persistent flag)");
2047 if (offset
+ length
> bufObj
->Size
) {
2048 _mesa_error(ctx
, GL_INVALID_VALUE
,
2049 "glMapBufferRange(offset + length > size)");
2053 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2054 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2055 "glMapBufferRange(buffer already mapped)");
2059 if (!bufObj
->Size
) {
2060 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
2061 "glMapBufferRange(buffer size = 0)");
2065 /* Mapping zero bytes should return a non-null pointer. */
2067 static long dummy
= 0;
2068 bufObj
->Mappings
[MAP_USER
].Pointer
= &dummy
;
2069 bufObj
->Mappings
[MAP_USER
].Length
= length
;
2070 bufObj
->Mappings
[MAP_USER
].Offset
= offset
;
2071 bufObj
->Mappings
[MAP_USER
].AccessFlags
= access
;
2072 return bufObj
->Mappings
[MAP_USER
].Pointer
;
2075 ASSERT(ctx
->Driver
.MapBufferRange
);
2076 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2079 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
2082 /* The driver callback should have set all these fields.
2083 * This is important because other modules (like VBO) might call
2084 * the driver function directly.
2086 ASSERT(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2087 ASSERT(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2088 ASSERT(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2089 ASSERT(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2097 * See GL_ARB_map_buffer_range spec
2100 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
2102 GET_CURRENT_CONTEXT(ctx
);
2103 struct gl_buffer_object
*bufObj
;
2105 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2106 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2107 "glFlushMappedBufferRange(extension not supported)");
2112 _mesa_error(ctx
, GL_INVALID_VALUE
,
2113 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
2118 _mesa_error(ctx
, GL_INVALID_VALUE
,
2119 "glFlushMappedBufferRange(length = %ld)", (long)length
);
2123 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2124 GL_INVALID_OPERATION
);
2128 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2129 /* buffer is not mapped */
2130 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2131 "glFlushMappedBufferRange(buffer is not mapped)");
2135 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2136 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2137 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2138 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
2142 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2143 _mesa_error(ctx
, GL_INVALID_VALUE
,
2144 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
2145 (long)offset
, (long)length
,
2146 (long)bufObj
->Mappings
[MAP_USER
].Length
);
2150 ASSERT(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2152 if (ctx
->Driver
.FlushMappedBufferRange
)
2153 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2159 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2161 struct gl_buffer_object
*bufObj
;
2164 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2166 _mesa_error(ctx
, GL_INVALID_VALUE
,
2167 "glObjectPurgeable(name = 0x%x)", name
);
2170 if (!_mesa_is_bufferobj(bufObj
)) {
2171 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
2175 if (bufObj
->Purgeable
) {
2176 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2177 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
2178 return GL_VOLATILE_APPLE
;
2181 bufObj
->Purgeable
= GL_TRUE
;
2183 retval
= GL_VOLATILE_APPLE
;
2184 if (ctx
->Driver
.BufferObjectPurgeable
)
2185 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
2192 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2194 struct gl_renderbuffer
*bufObj
;
2197 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
2199 _mesa_error(ctx
, GL_INVALID_VALUE
,
2200 "glObjectUnpurgeable(name = 0x%x)", name
);
2204 if (bufObj
->Purgeable
) {
2205 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2206 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
2207 return GL_VOLATILE_APPLE
;
2210 bufObj
->Purgeable
= GL_TRUE
;
2212 retval
= GL_VOLATILE_APPLE
;
2213 if (ctx
->Driver
.RenderObjectPurgeable
)
2214 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
2221 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2223 struct gl_texture_object
*bufObj
;
2226 bufObj
= _mesa_lookup_texture(ctx
, name
);
2228 _mesa_error(ctx
, GL_INVALID_VALUE
,
2229 "glObjectPurgeable(name = 0x%x)", name
);
2233 if (bufObj
->Purgeable
) {
2234 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2235 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
2236 return GL_VOLATILE_APPLE
;
2239 bufObj
->Purgeable
= GL_TRUE
;
2241 retval
= GL_VOLATILE_APPLE
;
2242 if (ctx
->Driver
.TextureObjectPurgeable
)
2243 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
2250 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
2254 GET_CURRENT_CONTEXT(ctx
);
2255 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2258 _mesa_error(ctx
, GL_INVALID_VALUE
,
2259 "glObjectPurgeable(name = 0x%x)", name
);
2264 case GL_VOLATILE_APPLE
:
2265 case GL_RELEASED_APPLE
:
2269 _mesa_error(ctx
, GL_INVALID_ENUM
,
2270 "glObjectPurgeable(name = 0x%x) invalid option: %d",
2275 switch (objectType
) {
2277 retval
= texture_object_purgeable(ctx
, name
, option
);
2279 case GL_RENDERBUFFER_EXT
:
2280 retval
= renderbuffer_purgeable(ctx
, name
, option
);
2282 case GL_BUFFER_OBJECT_APPLE
:
2283 retval
= buffer_object_purgeable(ctx
, name
, option
);
2286 _mesa_error(ctx
, GL_INVALID_ENUM
,
2287 "glObjectPurgeable(name = 0x%x) invalid type: %d",
2292 /* In strict conformance to the spec, we must only return VOLATILE when
2293 * when passed the VOLATILE option. Madness.
2295 * XXX First fix the spec, then fix me.
2297 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
2302 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2304 struct gl_buffer_object
*bufObj
;
2307 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2309 _mesa_error(ctx
, GL_INVALID_VALUE
,
2310 "glObjectUnpurgeable(name = 0x%x)", name
);
2314 if (! bufObj
->Purgeable
) {
2315 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2316 "glObjectUnpurgeable(name = 0x%x) object is "
2317 " already \"unpurged\"", name
);
2321 bufObj
->Purgeable
= GL_FALSE
;
2324 if (ctx
->Driver
.BufferObjectUnpurgeable
)
2325 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
2332 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2334 struct gl_renderbuffer
*bufObj
;
2337 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
2339 _mesa_error(ctx
, GL_INVALID_VALUE
,
2340 "glObjectUnpurgeable(name = 0x%x)", name
);
2344 if (! bufObj
->Purgeable
) {
2345 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2346 "glObjectUnpurgeable(name = 0x%x) object is "
2347 " already \"unpurged\"", name
);
2351 bufObj
->Purgeable
= GL_FALSE
;
2354 if (ctx
->Driver
.RenderObjectUnpurgeable
)
2355 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
2362 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
2364 struct gl_texture_object
*bufObj
;
2367 bufObj
= _mesa_lookup_texture(ctx
, name
);
2369 _mesa_error(ctx
, GL_INVALID_VALUE
,
2370 "glObjectUnpurgeable(name = 0x%x)", name
);
2374 if (! bufObj
->Purgeable
) {
2375 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2376 "glObjectUnpurgeable(name = 0x%x) object is"
2377 " already \"unpurged\"", name
);
2381 bufObj
->Purgeable
= GL_FALSE
;
2384 if (ctx
->Driver
.TextureObjectUnpurgeable
)
2385 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
2392 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
2394 GET_CURRENT_CONTEXT(ctx
);
2395 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2398 _mesa_error(ctx
, GL_INVALID_VALUE
,
2399 "glObjectUnpurgeable(name = 0x%x)", name
);
2404 case GL_RETAINED_APPLE
:
2405 case GL_UNDEFINED_APPLE
:
2409 _mesa_error(ctx
, GL_INVALID_ENUM
,
2410 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2415 switch (objectType
) {
2416 case GL_BUFFER_OBJECT_APPLE
:
2417 return buffer_object_unpurgeable(ctx
, name
, option
);
2419 return texture_object_unpurgeable(ctx
, name
, option
);
2420 case GL_RENDERBUFFER_EXT
:
2421 return renderbuffer_unpurgeable(ctx
, name
, option
);
2423 _mesa_error(ctx
, GL_INVALID_ENUM
,
2424 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2432 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
2433 GLenum pname
, GLint
*params
)
2435 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2437 _mesa_error(ctx
, GL_INVALID_VALUE
,
2438 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
2443 case GL_PURGEABLE_APPLE
:
2444 *params
= bufObj
->Purgeable
;
2447 _mesa_error(ctx
, GL_INVALID_ENUM
,
2448 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2456 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
2457 GLenum pname
, GLint
*params
)
2459 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
2461 _mesa_error(ctx
, GL_INVALID_VALUE
,
2462 "glObjectUnpurgeable(name = 0x%x)", name
);
2467 case GL_PURGEABLE_APPLE
:
2468 *params
= rb
->Purgeable
;
2471 _mesa_error(ctx
, GL_INVALID_ENUM
,
2472 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2480 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
2481 GLenum pname
, GLint
*params
)
2483 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
2485 _mesa_error(ctx
, GL_INVALID_VALUE
,
2486 "glObjectUnpurgeable(name = 0x%x)", name
);
2491 case GL_PURGEABLE_APPLE
:
2492 *params
= texObj
->Purgeable
;
2495 _mesa_error(ctx
, GL_INVALID_ENUM
,
2496 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2504 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
2507 GET_CURRENT_CONTEXT(ctx
);
2510 _mesa_error(ctx
, GL_INVALID_VALUE
,
2511 "glGetObjectParameteriv(name = 0x%x)", name
);
2515 switch (objectType
) {
2517 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
2519 case GL_BUFFER_OBJECT_APPLE
:
2520 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
2522 case GL_RENDERBUFFER_EXT
:
2523 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
2526 _mesa_error(ctx
, GL_INVALID_ENUM
,
2527 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2533 set_ubo_binding(struct gl_context
*ctx
,
2535 struct gl_buffer_object
*bufObj
,
2540 struct gl_uniform_buffer_binding
*binding
;
2542 binding
= &ctx
->UniformBufferBindings
[index
];
2543 if (binding
->BufferObject
== bufObj
&&
2544 binding
->Offset
== offset
&&
2545 binding
->Size
== size
&&
2546 binding
->AutomaticSize
== autoSize
) {
2550 FLUSH_VERTICES(ctx
, 0);
2551 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2553 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2554 binding
->Offset
= offset
;
2555 binding
->Size
= size
;
2556 binding
->AutomaticSize
= autoSize
;
2560 * Bind a region of a buffer object to a uniform block binding point.
2561 * \param index the uniform buffer binding point index
2562 * \param bufObj the buffer object
2563 * \param offset offset to the start of buffer object region
2564 * \param size size of the buffer object region
2567 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2569 struct gl_buffer_object
*bufObj
,
2573 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2574 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2578 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2579 _mesa_error(ctx
, GL_INVALID_VALUE
,
2580 "glBindBufferRange(offset misalgned %d/%d)", (int) offset
,
2581 ctx
->Const
.UniformBufferOffsetAlignment
);
2585 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2590 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2591 set_ubo_binding(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2596 * Bind a buffer object to a uniform block binding point.
2597 * As above, but offset = 0.
2600 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2602 struct gl_buffer_object
*bufObj
)
2604 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2605 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2609 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2610 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2611 set_ubo_binding(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2613 set_ubo_binding(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2617 set_atomic_buffer_binding(struct gl_context
*ctx
,
2619 struct gl_buffer_object
*bufObj
,
2624 struct gl_atomic_buffer_binding
*binding
;
2626 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
2627 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
2631 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
2632 _mesa_error(ctx
, GL_INVALID_VALUE
,
2633 "%s(offset misalgned %d/%d)", name
, (int) offset
,
2634 ATOMIC_COUNTER_SIZE
);
2638 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
2640 binding
= &ctx
->AtomicBufferBindings
[index
];
2641 if (binding
->BufferObject
== bufObj
&&
2642 binding
->Offset
== offset
&&
2643 binding
->Size
== size
) {
2647 FLUSH_VERTICES(ctx
, 0);
2648 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
2650 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2652 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2653 binding
->Offset
= -1;
2656 binding
->Offset
= offset
;
2657 binding
->Size
= size
;
2662 _mesa_BindBufferRange(GLenum target
, GLuint index
,
2663 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
2665 GET_CURRENT_CONTEXT(ctx
);
2666 struct gl_buffer_object
*bufObj
;
2669 bufObj
= ctx
->Shared
->NullBufferObj
;
2671 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2673 if (!_mesa_handle_bind_buffer_gen(ctx
, target
, buffer
,
2674 &bufObj
, "glBindBufferRange"))
2678 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2679 "glBindBufferRange(invalid buffer=%u)", buffer
);
2685 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
2692 case GL_TRANSFORM_FEEDBACK_BUFFER
:
2693 _mesa_bind_buffer_range_transform_feedback(ctx
, index
, bufObj
,
2696 case GL_UNIFORM_BUFFER
:
2697 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
2699 case GL_ATOMIC_COUNTER_BUFFER
:
2700 set_atomic_buffer_binding(ctx
, index
, bufObj
, offset
, size
,
2701 "glBindBufferRange");
2704 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
2710 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
2712 GET_CURRENT_CONTEXT(ctx
);
2713 struct gl_buffer_object
*bufObj
;
2716 bufObj
= ctx
->Shared
->NullBufferObj
;
2718 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2720 if (!_mesa_handle_bind_buffer_gen(ctx
, target
, buffer
,
2721 &bufObj
, "glBindBufferBase"))
2725 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2726 "glBindBufferBase(invalid buffer=%u)", buffer
);
2730 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
2731 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
2733 * "BindBufferBase is equivalent to calling BindBufferRange with offset
2734 * zero and size equal to the size of buffer."
2736 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
2738 * "If the parameter (starting offset or size) was not specified when the
2739 * buffer object was bound, zero is returned."
2741 * What happens if the size of the buffer changes? Does the size of the
2742 * buffer at the moment glBindBufferBase was called still play a role, like
2743 * the first quote would imply, or is the size meaningless in the
2744 * glBindBufferBase case like the second quote would suggest? The GL 4.1
2745 * core spec page 45 says:
2747 * "It is equivalent to calling BindBufferRange with offset zero, while
2748 * size is determined by the size of the bound buffer at the time the
2751 * My interpretation is that the GL 4.1 spec was a clarification of the
2752 * behavior, not a change. In particular, this choice will only make
2753 * rendering work in cases where it would have had undefined results.
2757 case GL_TRANSFORM_FEEDBACK_BUFFER
:
2758 _mesa_bind_buffer_base_transform_feedback(ctx
, index
, bufObj
);
2760 case GL_UNIFORM_BUFFER
:
2761 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
2763 case GL_ATOMIC_COUNTER_BUFFER
:
2764 set_atomic_buffer_binding(ctx
, index
, bufObj
, 0, 0,
2765 "glBindBufferBase");
2768 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
2774 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
2775 const GLuint
*buffers
,
2776 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
2781 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
2782 const GLuint
*buffers
)
2787 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
2790 GET_CURRENT_CONTEXT(ctx
);
2791 struct gl_buffer_object
*bufObj
;
2792 const GLintptr end
= offset
+ length
;
2794 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2796 _mesa_error(ctx
, GL_INVALID_VALUE
,
2797 "glInvalidateBufferSubData(name = 0x%x) invalid object",
2802 /* The GL_ARB_invalidate_subdata spec says:
2804 * "An INVALID_VALUE error is generated if <offset> or <length> is
2805 * negative, or if <offset> + <length> is greater than the value of
2808 if (end
< 0 || end
> bufObj
->Size
) {
2809 _mesa_error(ctx
, GL_INVALID_VALUE
,
2810 "glInvalidateBufferSubData(invalid offset or length)");
2814 /* The OpenGL 4.4 (Core Profile) spec says:
2816 * "An INVALID_OPERATION error is generated if buffer is currently
2817 * mapped by MapBuffer or if the invalidate range intersects the range
2818 * currently mapped by MapBufferRange, unless it was mapped
2819 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
2821 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
2822 bufferobj_range_mapped(bufObj
, offset
, length
)) {
2823 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2824 "glInvalidateBufferSubData(intersection with mapped "
2829 /* We don't actually do anything for this yet. Just return after
2830 * validating the parameters and generating the required errors.
2836 _mesa_InvalidateBufferData(GLuint buffer
)
2838 GET_CURRENT_CONTEXT(ctx
);
2839 struct gl_buffer_object
*bufObj
;
2841 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2843 _mesa_error(ctx
, GL_INVALID_VALUE
,
2844 "glInvalidateBufferData(name = 0x%x) invalid object",
2849 /* The OpenGL 4.4 (Core Profile) spec says:
2851 * "An INVALID_OPERATION error is generated if buffer is currently
2852 * mapped by MapBuffer or if the invalidate range intersects the range
2853 * currently mapped by MapBufferRange, unless it was mapped
2854 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
2856 if (_mesa_check_disallowed_mapping(bufObj
)) {
2857 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2858 "glInvalidateBufferData(intersection with mapped "
2863 /* We don't actually do anything for this yet. Just return after
2864 * validating the parameters and generating the required errors.