c3548b5908e8b3dcbd406b1df116917ab1551330
[mesa.git] / src / mesa / main / bufferobj.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
24 */
25
26
27 /**
28 * \file bufferobj.c
29 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
30 * \author Brian Paul, Ian Romanick
31 */
32
33 #include <stdbool.h>
34 #include <inttypes.h> /* for PRId64 macro */
35 #include "glheader.h"
36 #include "enums.h"
37 #include "hash.h"
38 #include "imports.h"
39 #include "image.h"
40 #include "context.h"
41 #include "bufferobj.h"
42 #include "fbobject.h"
43 #include "mtypes.h"
44 #include "texobj.h"
45 #include "teximage.h"
46 #include "glformats.h"
47 #include "texstore.h"
48 #include "transformfeedback.h"
49 #include "dispatch.h"
50
51
52 /* Debug flags */
53 /*#define VBO_DEBUG*/
54 /*#define BOUNDS_CHECK*/
55
56
57 /**
58 * Used as a placeholder for buffer objects between glGenBuffers() and
59 * glBindBuffer() so that glIsBuffer() can work correctly.
60 */
61 static struct gl_buffer_object DummyBufferObject;
62
63
64 /**
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.
70 */
71 static inline struct gl_buffer_object **
72 get_buffer_target(struct gl_context *ctx, GLenum target)
73 {
74 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
75 */
76 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx)
77 && target != GL_ARRAY_BUFFER && target != GL_ELEMENT_ARRAY_BUFFER)
78 return NULL;
79
80 switch (target) {
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;
97 }
98 break;
99 case GL_TRANSFORM_FEEDBACK_BUFFER:
100 if (ctx->Extensions.EXT_transform_feedback) {
101 return &ctx->TransformFeedback.CurrentBuffer;
102 }
103 break;
104 case GL_TEXTURE_BUFFER:
105 if (ctx->API == API_OPENGL_CORE &&
106 ctx->Extensions.ARB_texture_buffer_object) {
107 return &ctx->Texture.BufferObject;
108 }
109 break;
110 case GL_UNIFORM_BUFFER:
111 if (ctx->Extensions.ARB_uniform_buffer_object) {
112 return &ctx->UniformBuffer;
113 }
114 break;
115 case GL_SHADER_STORAGE_BUFFER:
116 if (ctx->Extensions.ARB_shader_storage_buffer_object) {
117 return &ctx->ShaderStorageBuffer;
118 }
119 break;
120 case GL_ATOMIC_COUNTER_BUFFER:
121 if (ctx->Extensions.ARB_shader_atomic_counters) {
122 return &ctx->AtomicBuffer;
123 }
124 break;
125 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD:
126 if (ctx->Extensions.AMD_pinned_memory) {
127 return &ctx->ExternalVirtualMemoryBuffer;
128 }
129 break;
130 default:
131 return NULL;
132 }
133 return NULL;
134 }
135
136
137 /**
138 * Get the buffer object bound to the specified target in a GL context.
139 * \param ctx the GL context
140 * \param target the buffer object target to be retrieved.
141 * \param error the GL error to record if target is illegal.
142 * \return pointer to the buffer object bound to \c target in the
143 * specified context or \c NULL if \c target is invalid.
144 */
145 static inline struct gl_buffer_object *
146 get_buffer(struct gl_context *ctx, const char *func, GLenum target,
147 GLenum error)
148 {
149 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
150
151 if (!bufObj) {
152 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
153 return NULL;
154 }
155
156 if (!_mesa_is_bufferobj(*bufObj)) {
157 _mesa_error(ctx, error, "%s(no buffer bound)", func);
158 return NULL;
159 }
160
161 return *bufObj;
162 }
163
164
165 /**
166 * Convert a GLbitfield describing the mapped buffer access flags
167 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
168 */
169 static GLenum
170 simplified_access_mode(struct gl_context *ctx, GLbitfield access)
171 {
172 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
173 if ((access & rwFlags) == rwFlags)
174 return GL_READ_WRITE;
175 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
176 return GL_READ_ONLY;
177 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
178 return GL_WRITE_ONLY;
179
180 /* Otherwise, AccessFlags is zero (the default state).
181 *
182 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
183 *
184 * Name Type Initial Value Legal Values
185 * ... ... ... ...
186 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
187 * READ_WRITE
188 *
189 * However, table 6.8 in the GL_OES_mapbuffer extension says:
190 *
191 * Get Value Type Get Command Value Description
192 * --------- ---- ----------- ----- -----------
193 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
194 *
195 * The difference is because GL_OES_mapbuffer only supports mapping buffers
196 * write-only.
197 */
198 assert(access == 0);
199
200 return _mesa_is_gles(ctx) ? GL_WRITE_ONLY : GL_READ_WRITE;
201 }
202
203
204 /**
205 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
206 * given range.
207 * The regions do not overlap if and only if the end of the given
208 * region is before the mapped region or the start of the given region
209 * is after the mapped region.
210 *
211 * \param obj Buffer object target on which to operate.
212 * \param offset Offset of the first byte of the subdata range.
213 * \param size Size, in bytes, of the subdata range.
214 * \return true if ranges overlap, false otherwise
215 *
216 */
217 static bool
218 bufferobj_range_mapped(const struct gl_buffer_object *obj,
219 GLintptr offset, GLsizeiptr size)
220 {
221 if (_mesa_bufferobj_mapped(obj, MAP_USER)) {
222 const GLintptr end = offset + size;
223 const GLintptr mapEnd = obj->Mappings[MAP_USER].Offset +
224 obj->Mappings[MAP_USER].Length;
225
226 if (!(end <= obj->Mappings[MAP_USER].Offset || offset >= mapEnd)) {
227 return true;
228 }
229 }
230 return false;
231 }
232
233
234 /**
235 * Tests the subdata range parameters and sets the GL error code for
236 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
237 * \c glClearBufferSubData.
238 *
239 * \param ctx GL context.
240 * \param bufObj The buffer object.
241 * \param offset Offset of the first byte of the subdata range.
242 * \param size Size, in bytes, of the subdata range.
243 * \param mappedRange If true, checks if an overlapping range is mapped.
244 * If false, checks if buffer is mapped.
245 * \param caller Name of calling function for recording errors.
246 * \return false if error, true otherwise
247 *
248 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
249 */
250 static bool
251 buffer_object_subdata_range_good(struct gl_context *ctx,
252 struct gl_buffer_object *bufObj,
253 GLintptr offset, GLsizeiptr size,
254 bool mappedRange, const char *caller)
255 {
256 if (size < 0) {
257 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
258 return false;
259 }
260
261 if (offset < 0) {
262 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
263 return false;
264 }
265
266 if (offset + size > bufObj->Size) {
267 _mesa_error(ctx, GL_INVALID_VALUE,
268 "%s(offset %lu + size %lu > buffer size %lu)", caller,
269 (unsigned long) offset,
270 (unsigned long) size,
271 (unsigned long) bufObj->Size);
272 return false;
273 }
274
275 if (bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT)
276 return true;
277
278 if (mappedRange) {
279 if (bufferobj_range_mapped(bufObj, offset, size)) {
280 _mesa_error(ctx, GL_INVALID_OPERATION,
281 "%s(range is mapped without persistent bit)",
282 caller);
283 return false;
284 }
285 }
286 else {
287 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
288 _mesa_error(ctx, GL_INVALID_OPERATION,
289 "%s(buffer is mapped without persistent bit)",
290 caller);
291 return false;
292 }
293 }
294
295 return true;
296 }
297
298
299 /**
300 * Test the format and type parameters and set the GL error code for
301 * \c glClearBufferData and \c glClearBufferSubData.
302 *
303 * \param ctx GL context.
304 * \param internalformat Format to which the data is to be converted.
305 * \param format Format of the supplied data.
306 * \param type Type of the supplied data.
307 * \param caller Name of calling function for recording errors.
308 * \return If internalformat, format and type are legal the mesa_format
309 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
310 *
311 * \sa glClearBufferData and glClearBufferSubData
312 */
313 static mesa_format
314 validate_clear_buffer_format(struct gl_context *ctx,
315 GLenum internalformat,
316 GLenum format, GLenum type,
317 const char *caller)
318 {
319 mesa_format mesaFormat;
320 GLenum errorFormatType;
321
322 mesaFormat = _mesa_validate_texbuffer_format(ctx, internalformat);
323 if (mesaFormat == MESA_FORMAT_NONE) {
324 _mesa_error(ctx, GL_INVALID_ENUM,
325 "%s(invalid internalformat)", caller);
326 return MESA_FORMAT_NONE;
327 }
328
329 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
330 * EXT_texture_integer there is no conversion between integer and
331 * non-integer formats
332 */
333 if (_mesa_is_enum_format_signed_int(format) !=
334 _mesa_is_format_integer_color(mesaFormat)) {
335 _mesa_error(ctx, GL_INVALID_OPERATION,
336 "%s(integer vs non-integer)", caller);
337 return MESA_FORMAT_NONE;
338 }
339
340 if (!_mesa_is_color_format(format)) {
341 _mesa_error(ctx, GL_INVALID_ENUM,
342 "%s(format is not a color format)", caller);
343 return MESA_FORMAT_NONE;
344 }
345
346 errorFormatType = _mesa_error_check_format_and_type(ctx, format, type);
347 if (errorFormatType != GL_NO_ERROR) {
348 _mesa_error(ctx, GL_INVALID_ENUM,
349 "%s(invalid format or type)", caller);
350 return MESA_FORMAT_NONE;
351 }
352
353 return mesaFormat;
354 }
355
356
357 /**
358 * Convert user-specified clear value to the specified internal format.
359 *
360 * \param ctx GL context.
361 * \param internalformat Format to which the data is converted.
362 * \param clearValue Points to the converted clear value.
363 * \param format Format of the supplied data.
364 * \param type Type of the supplied data.
365 * \param data Data which is to be converted to internalformat.
366 * \param caller Name of calling function for recording errors.
367 * \return true if data could be converted, false otherwise.
368 *
369 * \sa glClearBufferData, glClearBufferSubData
370 */
371 static bool
372 convert_clear_buffer_data(struct gl_context *ctx,
373 mesa_format internalformat,
374 GLubyte *clearValue, GLenum format, GLenum type,
375 const GLvoid *data, const char *caller)
376 {
377 GLenum internalformatBase = _mesa_get_format_base_format(internalformat);
378
379 if (_mesa_texstore(ctx, 1, internalformatBase, internalformat,
380 0, &clearValue, 1, 1, 1,
381 format, type, data, &ctx->Unpack)) {
382 return true;
383 }
384 else {
385 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
386 return false;
387 }
388 }
389
390
391 /**
392 * Allocate and initialize a new buffer object.
393 *
394 * Default callback for the \c dd_function_table::NewBufferObject() hook.
395 */
396 static struct gl_buffer_object *
397 _mesa_new_buffer_object(struct gl_context *ctx, GLuint name)
398 {
399 struct gl_buffer_object *obj;
400
401 (void) ctx;
402
403 obj = MALLOC_STRUCT(gl_buffer_object);
404 _mesa_initialize_buffer_object(ctx, obj, name);
405 return obj;
406 }
407
408
409 /**
410 * Delete a buffer object.
411 *
412 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
413 */
414 static void
415 _mesa_delete_buffer_object(struct gl_context *ctx,
416 struct gl_buffer_object *bufObj)
417 {
418 (void) ctx;
419
420 _mesa_align_free(bufObj->Data);
421
422 /* assign strange values here to help w/ debugging */
423 bufObj->RefCount = -1000;
424 bufObj->Name = ~0;
425
426 mtx_destroy(&bufObj->Mutex);
427 free(bufObj->Label);
428 free(bufObj);
429 }
430
431
432
433 /**
434 * Set ptr to bufObj w/ reference counting.
435 * This is normally only called from the _mesa_reference_buffer_object() macro
436 * when there's a real pointer change.
437 */
438 void
439 _mesa_reference_buffer_object_(struct gl_context *ctx,
440 struct gl_buffer_object **ptr,
441 struct gl_buffer_object *bufObj)
442 {
443 if (*ptr) {
444 /* Unreference the old buffer */
445 GLboolean deleteFlag = GL_FALSE;
446 struct gl_buffer_object *oldObj = *ptr;
447
448 mtx_lock(&oldObj->Mutex);
449 assert(oldObj->RefCount > 0);
450 oldObj->RefCount--;
451 #if 0
452 printf("BufferObj %p %d DECR to %d\n",
453 (void *) oldObj, oldObj->Name, oldObj->RefCount);
454 #endif
455 deleteFlag = (oldObj->RefCount == 0);
456 mtx_unlock(&oldObj->Mutex);
457
458 if (deleteFlag) {
459
460 /* some sanity checking: don't delete a buffer still in use */
461 #if 0
462 /* unfortunately, these tests are invalid during context tear-down */
463 assert(ctx->Array.ArrayBufferObj != bufObj);
464 assert(ctx->Array.VAO->IndexBufferObj != bufObj);
465 assert(ctx->Array.VAO->Vertex.BufferObj != bufObj);
466 #endif
467
468 assert(ctx->Driver.DeleteBuffer);
469 ctx->Driver.DeleteBuffer(ctx, oldObj);
470 }
471
472 *ptr = NULL;
473 }
474 assert(!*ptr);
475
476 if (bufObj) {
477 /* reference new buffer */
478 mtx_lock(&bufObj->Mutex);
479 if (bufObj->RefCount == 0) {
480 /* this buffer's being deleted (look just above) */
481 /* Not sure this can every really happen. Warn if it does. */
482 _mesa_problem(NULL, "referencing deleted buffer object");
483 *ptr = NULL;
484 }
485 else {
486 bufObj->RefCount++;
487 #if 0
488 printf("BufferObj %p %d INCR to %d\n",
489 (void *) bufObj, bufObj->Name, bufObj->RefCount);
490 #endif
491 *ptr = bufObj;
492 }
493 mtx_unlock(&bufObj->Mutex);
494 }
495 }
496
497
498 /**
499 * Initialize a buffer object to default values.
500 */
501 void
502 _mesa_initialize_buffer_object(struct gl_context *ctx,
503 struct gl_buffer_object *obj,
504 GLuint name)
505 {
506 memset(obj, 0, sizeof(struct gl_buffer_object));
507 mtx_init(&obj->Mutex, mtx_plain);
508 obj->RefCount = 1;
509 obj->Name = name;
510 obj->Usage = GL_STATIC_DRAW_ARB;
511 }
512
513
514
515 /**
516 * Callback called from _mesa_HashWalk()
517 */
518 static void
519 count_buffer_size(GLuint key, void *data, void *userData)
520 {
521 const struct gl_buffer_object *bufObj =
522 (const struct gl_buffer_object *) data;
523 GLuint *total = (GLuint *) userData;
524
525 *total = *total + bufObj->Size;
526 }
527
528
529 /**
530 * Compute total size (in bytes) of all buffer objects for the given context.
531 * For debugging purposes.
532 */
533 GLuint
534 _mesa_total_buffer_object_memory(struct gl_context *ctx)
535 {
536 GLuint total = 0;
537
538 _mesa_HashWalk(ctx->Shared->BufferObjects, count_buffer_size, &total);
539
540 return total;
541 }
542
543
544 /**
545 * Allocate space for and store data in a buffer object. Any data that was
546 * previously stored in the buffer object is lost. If \c data is \c NULL,
547 * memory will be allocated, but no copy will occur.
548 *
549 * This is the default callback for \c dd_function_table::BufferData()
550 * Note that all GL error checking will have been done already.
551 *
552 * \param ctx GL context.
553 * \param target Buffer object target on which to operate.
554 * \param size Size, in bytes, of the new data store.
555 * \param data Pointer to the data to store in the buffer object. This
556 * pointer may be \c NULL.
557 * \param usage Hints about how the data will be used.
558 * \param bufObj Object to be used.
559 *
560 * \return GL_TRUE for success, GL_FALSE for failure
561 * \sa glBufferDataARB, dd_function_table::BufferData.
562 */
563 static GLboolean
564 buffer_data_fallback(struct gl_context *ctx, GLenum target, GLsizeiptr size,
565 const GLvoid *data, GLenum usage, GLenum storageFlags,
566 struct gl_buffer_object *bufObj)
567 {
568 void * new_data;
569
570 (void) target;
571
572 _mesa_align_free( bufObj->Data );
573
574 new_data = _mesa_align_malloc( size, ctx->Const.MinMapBufferAlignment );
575 if (new_data) {
576 bufObj->Data = (GLubyte *) new_data;
577 bufObj->Size = size;
578 bufObj->Usage = usage;
579 bufObj->StorageFlags = storageFlags;
580
581 if (data) {
582 memcpy( bufObj->Data, data, size );
583 }
584
585 return GL_TRUE;
586 }
587 else {
588 return GL_FALSE;
589 }
590 }
591
592
593 /**
594 * Replace data in a subrange of buffer object. If the data range
595 * specified by \c size + \c offset extends beyond the end of the buffer or
596 * if \c data is \c NULL, no copy is performed.
597 *
598 * This is the default callback for \c dd_function_table::BufferSubData()
599 * Note that all GL error checking will have been done already.
600 *
601 * \param ctx GL context.
602 * \param offset Offset of the first byte to be modified.
603 * \param size Size, in bytes, of the data range.
604 * \param data Pointer to the data to store in the buffer object.
605 * \param bufObj Object to be used.
606 *
607 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
608 */
609 static void
610 buffer_sub_data_fallback(struct gl_context *ctx, GLintptr offset,
611 GLsizeiptr size, const GLvoid *data,
612 struct gl_buffer_object *bufObj)
613 {
614 (void) ctx;
615
616 /* this should have been caught in _mesa_BufferSubData() */
617 assert(size + offset <= bufObj->Size);
618
619 if (bufObj->Data) {
620 memcpy( (GLubyte *) bufObj->Data + offset, data, size );
621 }
622 }
623
624
625 /**
626 * Retrieve data from a subrange of buffer object. If the data range
627 * specified by \c size + \c offset extends beyond the end of the buffer or
628 * if \c data is \c NULL, no copy is performed.
629 *
630 * This is the default callback for \c dd_function_table::GetBufferSubData()
631 * Note that all GL error checking will have been done already.
632 *
633 * \param ctx GL context.
634 * \param target Buffer object target on which to operate.
635 * \param offset Offset of the first byte to be fetched.
636 * \param size Size, in bytes, of the data range.
637 * \param data Destination for data
638 * \param bufObj Object to be used.
639 *
640 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
641 */
642 static void
643 _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset,
644 GLsizeiptrARB size, GLvoid * data,
645 struct gl_buffer_object * bufObj )
646 {
647 (void) ctx;
648
649 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
650 memcpy( data, (GLubyte *) bufObj->Data + offset, size );
651 }
652 }
653
654
655 /**
656 * Clear a subrange of the buffer object with copies of the supplied data.
657 * If data is NULL the buffer is filled with zeros.
658 *
659 * This is the default callback for \c dd_function_table::ClearBufferSubData()
660 * Note that all GL error checking will have been done already.
661 *
662 * \param ctx GL context.
663 * \param offset Offset of the first byte to be cleared.
664 * \param size Size, in bytes, of the to be cleared range.
665 * \param clearValue Source of the data.
666 * \param clearValueSize Size, in bytes, of the supplied data.
667 * \param bufObj Object to be cleared.
668 *
669 * \sa glClearBufferSubData, glClearBufferData and
670 * dd_function_table::ClearBufferSubData.
671 */
672 void
673 _mesa_ClearBufferSubData_sw(struct gl_context *ctx,
674 GLintptr offset, GLsizeiptr size,
675 const GLvoid *clearValue,
676 GLsizeiptr clearValueSize,
677 struct gl_buffer_object *bufObj)
678 {
679 GLsizeiptr i;
680 GLubyte *dest;
681
682 assert(ctx->Driver.MapBufferRange);
683 dest = ctx->Driver.MapBufferRange(ctx, offset, size,
684 GL_MAP_WRITE_BIT |
685 GL_MAP_INVALIDATE_RANGE_BIT,
686 bufObj, MAP_INTERNAL);
687
688 if (!dest) {
689 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearBuffer[Sub]Data");
690 return;
691 }
692
693 if (clearValue == NULL) {
694 /* Clear with zeros, per the spec */
695 memset(dest, 0, size);
696 ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
697 return;
698 }
699
700 for (i = 0; i < size/clearValueSize; ++i) {
701 memcpy(dest, clearValue, clearValueSize);
702 dest += clearValueSize;
703 }
704
705 ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
706 }
707
708
709 /**
710 * Default fallback for \c dd_function_table::MapBufferRange().
711 * Called via glMapBufferRange().
712 */
713 static void *
714 map_buffer_range_fallback(struct gl_context *ctx, GLintptr offset,
715 GLsizeiptr length, GLbitfield access,
716 struct gl_buffer_object *bufObj,
717 gl_map_buffer_index index)
718 {
719 (void) ctx;
720 assert(!_mesa_bufferobj_mapped(bufObj, index));
721 /* Just return a direct pointer to the data */
722 bufObj->Mappings[index].Pointer = bufObj->Data + offset;
723 bufObj->Mappings[index].Length = length;
724 bufObj->Mappings[index].Offset = offset;
725 bufObj->Mappings[index].AccessFlags = access;
726 return bufObj->Mappings[index].Pointer;
727 }
728
729
730 /**
731 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
732 * Called via glFlushMappedBufferRange().
733 */
734 static void
735 flush_mapped_buffer_range_fallback(struct gl_context *ctx,
736 GLintptr offset, GLsizeiptr length,
737 struct gl_buffer_object *obj,
738 gl_map_buffer_index index)
739 {
740 (void) ctx;
741 (void) offset;
742 (void) length;
743 (void) obj;
744 /* no-op */
745 }
746
747
748 /**
749 * Default callback for \c dd_function_table::UnmapBuffer().
750 *
751 * The input parameters will have been already tested for errors.
752 *
753 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
754 */
755 static GLboolean
756 unmap_buffer_fallback(struct gl_context *ctx, struct gl_buffer_object *bufObj,
757 gl_map_buffer_index index)
758 {
759 (void) ctx;
760 /* XXX we might assert here that bufObj->Pointer is non-null */
761 bufObj->Mappings[index].Pointer = NULL;
762 bufObj->Mappings[index].Length = 0;
763 bufObj->Mappings[index].Offset = 0;
764 bufObj->Mappings[index].AccessFlags = 0x0;
765 return GL_TRUE;
766 }
767
768
769 /**
770 * Default fallback for \c dd_function_table::CopyBufferSubData().
771 * Called via glCopyBufferSubData().
772 */
773 static void
774 copy_buffer_sub_data_fallback(struct gl_context *ctx,
775 struct gl_buffer_object *src,
776 struct gl_buffer_object *dst,
777 GLintptr readOffset, GLintptr writeOffset,
778 GLsizeiptr size)
779 {
780 GLubyte *srcPtr, *dstPtr;
781
782 if (src == dst) {
783 srcPtr = dstPtr = ctx->Driver.MapBufferRange(ctx, 0, src->Size,
784 GL_MAP_READ_BIT |
785 GL_MAP_WRITE_BIT, src,
786 MAP_INTERNAL);
787
788 if (!srcPtr)
789 return;
790
791 srcPtr += readOffset;
792 dstPtr += writeOffset;
793 } else {
794 srcPtr = ctx->Driver.MapBufferRange(ctx, readOffset, size,
795 GL_MAP_READ_BIT, src,
796 MAP_INTERNAL);
797 dstPtr = ctx->Driver.MapBufferRange(ctx, writeOffset, size,
798 (GL_MAP_WRITE_BIT |
799 GL_MAP_INVALIDATE_RANGE_BIT), dst,
800 MAP_INTERNAL);
801 }
802
803 /* Note: the src and dst regions will never overlap. Trying to do so
804 * would generate GL_INVALID_VALUE earlier.
805 */
806 if (srcPtr && dstPtr)
807 memcpy(dstPtr, srcPtr, size);
808
809 ctx->Driver.UnmapBuffer(ctx, src, MAP_INTERNAL);
810 if (dst != src)
811 ctx->Driver.UnmapBuffer(ctx, dst, MAP_INTERNAL);
812 }
813
814
815
816 /**
817 * Initialize the state associated with buffer objects
818 */
819 void
820 _mesa_init_buffer_objects( struct gl_context *ctx )
821 {
822 GLuint i;
823
824 memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
825 mtx_init(&DummyBufferObject.Mutex, mtx_plain);
826 DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
827
828 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
829 ctx->Shared->NullBufferObj);
830
831 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
832 ctx->Shared->NullBufferObj);
833 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
834 ctx->Shared->NullBufferObj);
835
836 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer,
837 ctx->Shared->NullBufferObj);
838
839 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer,
840 ctx->Shared->NullBufferObj);
841
842 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer,
843 ctx->Shared->NullBufferObj);
844
845 _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer,
846 ctx->Shared->NullBufferObj);
847
848 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
849 _mesa_reference_buffer_object(ctx,
850 &ctx->UniformBufferBindings[i].BufferObject,
851 ctx->Shared->NullBufferObj);
852 ctx->UniformBufferBindings[i].Offset = -1;
853 ctx->UniformBufferBindings[i].Size = -1;
854 }
855
856 for (i = 0; i < MAX_COMBINED_SHADER_STORAGE_BUFFERS; i++) {
857 _mesa_reference_buffer_object(ctx,
858 &ctx->ShaderStorageBufferBindings[i].BufferObject,
859 ctx->Shared->NullBufferObj);
860 ctx->ShaderStorageBufferBindings[i].Offset = -1;
861 ctx->ShaderStorageBufferBindings[i].Size = -1;
862 }
863
864 for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) {
865 _mesa_reference_buffer_object(ctx,
866 &ctx->AtomicBufferBindings[i].BufferObject,
867 ctx->Shared->NullBufferObj);
868 ctx->AtomicBufferBindings[i].Offset = -1;
869 ctx->AtomicBufferBindings[i].Size = -1;
870 }
871 }
872
873
874 void
875 _mesa_free_buffer_objects( struct gl_context *ctx )
876 {
877 GLuint i;
878
879 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
880
881 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
882 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
883
884 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, NULL);
885
886 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, NULL);
887
888 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, NULL);
889
890 _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer, NULL);
891
892 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
893 _mesa_reference_buffer_object(ctx,
894 &ctx->UniformBufferBindings[i].BufferObject,
895 NULL);
896 }
897
898 for (i = 0; i < MAX_COMBINED_SHADER_STORAGE_BUFFERS; i++) {
899 _mesa_reference_buffer_object(ctx,
900 &ctx->ShaderStorageBufferBindings[i].BufferObject,
901 NULL);
902 }
903
904 for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) {
905 _mesa_reference_buffer_object(ctx,
906 &ctx->AtomicBufferBindings[i].BufferObject,
907 NULL);
908 }
909
910 }
911
912 bool
913 _mesa_handle_bind_buffer_gen(struct gl_context *ctx,
914 GLenum target,
915 GLuint buffer,
916 struct gl_buffer_object **buf_handle,
917 const char *caller)
918 {
919 struct gl_buffer_object *buf = *buf_handle;
920
921 if (!buf && ctx->API == API_OPENGL_CORE) {
922 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", caller);
923 return false;
924 }
925
926 if (!buf || buf == &DummyBufferObject) {
927 /* If this is a new buffer object id, or one which was generated but
928 * never used before, allocate a buffer object now.
929 */
930 assert(ctx->Driver.NewBufferObject);
931 buf = ctx->Driver.NewBufferObject(ctx, buffer);
932 if (!buf) {
933 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
934 return false;
935 }
936 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf);
937 *buf_handle = buf;
938 }
939
940 return true;
941 }
942
943 /**
944 * Bind the specified target to buffer for the specified context.
945 * Called by glBindBuffer() and other functions.
946 */
947 static void
948 bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
949 {
950 struct gl_buffer_object *oldBufObj;
951 struct gl_buffer_object *newBufObj = NULL;
952 struct gl_buffer_object **bindTarget = NULL;
953
954 bindTarget = get_buffer_target(ctx, target);
955 if (!bindTarget) {
956 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
957 return;
958 }
959
960 /* Get pointer to old buffer object (to be unbound) */
961 oldBufObj = *bindTarget;
962 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
963 return; /* rebinding the same buffer object- no change */
964
965 /*
966 * Get pointer to new buffer object (newBufObj)
967 */
968 if (buffer == 0) {
969 /* The spec says there's not a buffer object named 0, but we use
970 * one internally because it simplifies things.
971 */
972 newBufObj = ctx->Shared->NullBufferObj;
973 }
974 else {
975 /* non-default buffer object */
976 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
977 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
978 &newBufObj, "glBindBuffer"))
979 return;
980 }
981
982 /* bind new buffer */
983 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
984 }
985
986
987 /**
988 * Update the default buffer objects in the given context to reference those
989 * specified in the shared state and release those referencing the old
990 * shared state.
991 */
992 void
993 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
994 {
995 /* Bind the NullBufferObj to remove references to those
996 * in the shared context hash table.
997 */
998 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
999 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1000 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
1001 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
1002 }
1003
1004
1005
1006 /**
1007 * Return the gl_buffer_object for the given ID.
1008 * Always return NULL for ID 0.
1009 */
1010 struct gl_buffer_object *
1011 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
1012 {
1013 if (buffer == 0)
1014 return NULL;
1015 else
1016 return (struct gl_buffer_object *)
1017 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
1018 }
1019
1020
1021 struct gl_buffer_object *
1022 _mesa_lookup_bufferobj_locked(struct gl_context *ctx, GLuint buffer)
1023 {
1024 return (struct gl_buffer_object *)
1025 _mesa_HashLookupLocked(ctx->Shared->BufferObjects, buffer);
1026 }
1027
1028 /**
1029 * A convenience function for direct state access functions that throws
1030 * GL_INVALID_OPERATION if buffer is not the name of an existing
1031 * buffer object.
1032 */
1033 struct gl_buffer_object *
1034 _mesa_lookup_bufferobj_err(struct gl_context *ctx, GLuint buffer,
1035 const char *caller)
1036 {
1037 struct gl_buffer_object *bufObj;
1038
1039 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
1040 if (!bufObj || bufObj == &DummyBufferObject) {
1041 _mesa_error(ctx, GL_INVALID_OPERATION,
1042 "%s(non-existent buffer object %u)", caller, buffer);
1043 return NULL;
1044 }
1045
1046 return bufObj;
1047 }
1048
1049
1050 void
1051 _mesa_begin_bufferobj_lookups(struct gl_context *ctx)
1052 {
1053 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
1054 }
1055
1056
1057 void
1058 _mesa_end_bufferobj_lookups(struct gl_context *ctx)
1059 {
1060 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
1061 }
1062
1063
1064 /**
1065 * Look up a buffer object for a multi-bind function.
1066 *
1067 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1068 * of generating an error if the buffer ID is not zero or the name
1069 * of an existing buffer object.
1070 *
1071 * If the buffer ID refers to an existing buffer object, a pointer
1072 * to the buffer object is returned. If the ID is zero, a pointer
1073 * to the shared NullBufferObj is returned. If the ID is not zero
1074 * and does not refer to a valid buffer object, this function
1075 * returns NULL.
1076 *
1077 * This function assumes that the caller has already locked the
1078 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1079 */
1080 struct gl_buffer_object *
1081 _mesa_multi_bind_lookup_bufferobj(struct gl_context *ctx,
1082 const GLuint *buffers,
1083 GLuint index, const char *caller)
1084 {
1085 struct gl_buffer_object *bufObj;
1086
1087 if (buffers[index] != 0) {
1088 bufObj = _mesa_lookup_bufferobj_locked(ctx, buffers[index]);
1089
1090 /* The multi-bind functions don't create the buffer objects
1091 when they don't exist. */
1092 if (bufObj == &DummyBufferObject)
1093 bufObj = NULL;
1094 } else
1095 bufObj = ctx->Shared->NullBufferObj;
1096
1097 if (!bufObj) {
1098 /* The ARB_multi_bind spec says:
1099 *
1100 * "An INVALID_OPERATION error is generated if any value
1101 * in <buffers> is not zero or the name of an existing
1102 * buffer object (per binding)."
1103 */
1104 _mesa_error(ctx, GL_INVALID_OPERATION,
1105 "%s(buffers[%u]=%u is not zero or the name "
1106 "of an existing buffer object)",
1107 caller, index, buffers[index]);
1108 }
1109
1110 return bufObj;
1111 }
1112
1113
1114 /**
1115 * If *ptr points to obj, set ptr = the Null/default buffer object.
1116 * This is a helper for buffer object deletion.
1117 * The GL spec says that deleting a buffer object causes it to get
1118 * unbound from all arrays in the current context.
1119 */
1120 static void
1121 unbind(struct gl_context *ctx,
1122 struct gl_buffer_object **ptr,
1123 struct gl_buffer_object *obj)
1124 {
1125 if (*ptr == obj) {
1126 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
1127 }
1128 }
1129
1130
1131 /**
1132 * Plug default/fallback buffer object functions into the device
1133 * driver hooks.
1134 */
1135 void
1136 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
1137 {
1138 /* GL_ARB_vertex/pixel_buffer_object */
1139 driver->NewBufferObject = _mesa_new_buffer_object;
1140 driver->DeleteBuffer = _mesa_delete_buffer_object;
1141 driver->BufferData = buffer_data_fallback;
1142 driver->BufferSubData = buffer_sub_data_fallback;
1143 driver->GetBufferSubData = _mesa_buffer_get_subdata;
1144 driver->UnmapBuffer = unmap_buffer_fallback;
1145
1146 /* GL_ARB_clear_buffer_object */
1147 driver->ClearBufferSubData = _mesa_ClearBufferSubData_sw;
1148
1149 /* GL_ARB_map_buffer_range */
1150 driver->MapBufferRange = map_buffer_range_fallback;
1151 driver->FlushMappedBufferRange = flush_mapped_buffer_range_fallback;
1152
1153 /* GL_ARB_copy_buffer */
1154 driver->CopyBufferSubData = copy_buffer_sub_data_fallback;
1155 }
1156
1157
1158 void
1159 _mesa_buffer_unmap_all_mappings(struct gl_context *ctx,
1160 struct gl_buffer_object *bufObj)
1161 {
1162 int i;
1163
1164 for (i = 0; i < MAP_COUNT; i++) {
1165 if (_mesa_bufferobj_mapped(bufObj, i)) {
1166 ctx->Driver.UnmapBuffer(ctx, bufObj, i);
1167 assert(bufObj->Mappings[i].Pointer == NULL);
1168 bufObj->Mappings[i].AccessFlags = 0;
1169 }
1170 }
1171 }
1172
1173
1174 /**********************************************************************/
1175 /* API Functions */
1176 /**********************************************************************/
1177
1178 void GLAPIENTRY
1179 _mesa_BindBuffer(GLenum target, GLuint buffer)
1180 {
1181 GET_CURRENT_CONTEXT(ctx);
1182
1183 if (MESA_VERBOSE & VERBOSE_API)
1184 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
1185 _mesa_lookup_enum_by_nr(target), buffer);
1186
1187 bind_buffer_object(ctx, target, buffer);
1188 }
1189
1190
1191 /**
1192 * Delete a set of buffer objects.
1193 *
1194 * \param n Number of buffer objects to delete.
1195 * \param ids Array of \c n buffer object IDs.
1196 */
1197 void GLAPIENTRY
1198 _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
1199 {
1200 GET_CURRENT_CONTEXT(ctx);
1201 GLsizei i;
1202 FLUSH_VERTICES(ctx, 0);
1203
1204 if (n < 0) {
1205 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
1206 return;
1207 }
1208
1209 mtx_lock(&ctx->Shared->Mutex);
1210
1211 for (i = 0; i < n; i++) {
1212 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
1213 if (bufObj) {
1214 struct gl_vertex_array_object *vao = ctx->Array.VAO;
1215 GLuint j;
1216
1217 assert(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
1218
1219 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1220
1221 /* unbind any vertex pointers bound to this buffer */
1222 for (j = 0; j < ARRAY_SIZE(vao->VertexBinding); j++) {
1223 unbind(ctx, &vao->VertexBinding[j].BufferObj, bufObj);
1224 }
1225
1226 if (ctx->Array.ArrayBufferObj == bufObj) {
1227 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
1228 }
1229 if (vao->IndexBufferObj == bufObj) {
1230 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
1231 }
1232
1233 /* unbind ARB_draw_indirect binding point */
1234 if (ctx->DrawIndirectBuffer == bufObj) {
1235 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER, 0 );
1236 }
1237
1238 /* unbind ARB_copy_buffer binding points */
1239 if (ctx->CopyReadBuffer == bufObj) {
1240 _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
1241 }
1242 if (ctx->CopyWriteBuffer == bufObj) {
1243 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
1244 }
1245
1246 /* unbind transform feedback binding points */
1247 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
1248 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
1249 }
1250 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
1251 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
1252 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
1253 }
1254 }
1255
1256 /* unbind UBO binding points */
1257 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
1258 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
1259 _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
1260 }
1261 }
1262
1263 if (ctx->UniformBuffer == bufObj) {
1264 _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
1265 }
1266
1267 /* unbind SSBO binding points */
1268 for (j = 0; j < ctx->Const.MaxShaderStorageBufferBindings; j++) {
1269 if (ctx->ShaderStorageBufferBindings[j].BufferObject == bufObj) {
1270 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER, j, 0);
1271 }
1272 }
1273
1274 if (ctx->ShaderStorageBuffer == bufObj) {
1275 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
1276 }
1277
1278 /* unbind Atomci Buffer binding points */
1279 for (j = 0; j < ctx->Const.MaxAtomicBufferBindings; j++) {
1280 if (ctx->AtomicBufferBindings[j].BufferObject == bufObj) {
1281 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER, j, 0 );
1282 }
1283 }
1284
1285 if (ctx->AtomicBuffer == bufObj) {
1286 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER, 0 );
1287 }
1288
1289 /* unbind any pixel pack/unpack pointers bound to this buffer */
1290 if (ctx->Pack.BufferObj == bufObj) {
1291 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
1292 }
1293 if (ctx->Unpack.BufferObj == bufObj) {
1294 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
1295 }
1296
1297 if (ctx->Texture.BufferObject == bufObj) {
1298 _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
1299 }
1300
1301 if (ctx->ExternalVirtualMemoryBuffer == bufObj) {
1302 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, 0);
1303 }
1304
1305 /* The ID is immediately freed for re-use */
1306 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
1307 /* Make sure we do not run into the classic ABA problem on bind.
1308 * We don't want to allow re-binding a buffer object that's been
1309 * "deleted" by glDeleteBuffers().
1310 *
1311 * The explicit rebinding to the default object in the current context
1312 * prevents the above in the current context, but another context
1313 * sharing the same objects might suffer from this problem.
1314 * The alternative would be to do the hash lookup in any case on bind
1315 * which would introduce more runtime overhead than this.
1316 */
1317 bufObj->DeletePending = GL_TRUE;
1318 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
1319 }
1320 }
1321
1322 mtx_unlock(&ctx->Shared->Mutex);
1323 }
1324
1325
1326 /**
1327 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1328 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1329 * driver internals.
1330 */
1331 static void
1332 create_buffers(GLsizei n, GLuint *buffers, bool dsa)
1333 {
1334 GET_CURRENT_CONTEXT(ctx);
1335 GLuint first;
1336 GLint i;
1337 struct gl_buffer_object *buf;
1338
1339 const char *func = dsa ? "glCreateBuffers" : "glGenBuffers";
1340
1341 if (MESA_VERBOSE & VERBOSE_API)
1342 _mesa_debug(ctx, "%s(%d)\n", func, n);
1343
1344 if (n < 0) {
1345 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n %d < 0)", func, n);
1346 return;
1347 }
1348
1349 if (!buffers) {
1350 return;
1351 }
1352
1353 /*
1354 * This must be atomic (generation and allocation of buffer object IDs)
1355 */
1356 mtx_lock(&ctx->Shared->Mutex);
1357
1358 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
1359
1360 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1361 * DummyBufferObject. Otherwise, create a new buffer object and insert
1362 * it.
1363 */
1364 for (i = 0; i < n; i++) {
1365 buffers[i] = first + i;
1366 if (dsa) {
1367 assert(ctx->Driver.NewBufferObject);
1368 buf = ctx->Driver.NewBufferObject(ctx, buffers[i]);
1369 if (!buf) {
1370 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1371 mtx_unlock(&ctx->Shared->Mutex);
1372 return;
1373 }
1374 }
1375 else
1376 buf = &DummyBufferObject;
1377
1378 _mesa_HashInsert(ctx->Shared->BufferObjects, buffers[i], buf);
1379 }
1380
1381 mtx_unlock(&ctx->Shared->Mutex);
1382 }
1383
1384 /**
1385 * Generate a set of unique buffer object IDs and store them in \c buffers.
1386 *
1387 * \param n Number of IDs to generate.
1388 * \param buffers Array of \c n locations to store the IDs.
1389 */
1390 void GLAPIENTRY
1391 _mesa_GenBuffers(GLsizei n, GLuint *buffers)
1392 {
1393 create_buffers(n, buffers, false);
1394 }
1395
1396 /**
1397 * Create a set of buffer objects and store their unique IDs in \c buffers.
1398 *
1399 * \param n Number of IDs to generate.
1400 * \param buffers Array of \c n locations to store the IDs.
1401 */
1402 void GLAPIENTRY
1403 _mesa_CreateBuffers(GLsizei n, GLuint *buffers)
1404 {
1405 create_buffers(n, buffers, true);
1406 }
1407
1408
1409 /**
1410 * Determine if ID is the name of a buffer object.
1411 *
1412 * \param id ID of the potential buffer object.
1413 * \return \c GL_TRUE if \c id is the name of a buffer object,
1414 * \c GL_FALSE otherwise.
1415 */
1416 GLboolean GLAPIENTRY
1417 _mesa_IsBuffer(GLuint id)
1418 {
1419 struct gl_buffer_object *bufObj;
1420 GET_CURRENT_CONTEXT(ctx);
1421 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1422
1423 mtx_lock(&ctx->Shared->Mutex);
1424 bufObj = _mesa_lookup_bufferobj(ctx, id);
1425 mtx_unlock(&ctx->Shared->Mutex);
1426
1427 return bufObj && bufObj != &DummyBufferObject;
1428 }
1429
1430
1431 void
1432 _mesa_buffer_storage(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1433 GLenum target, GLsizeiptr size, const GLvoid *data,
1434 GLbitfield flags, const char *func)
1435 {
1436 if (size <= 0) {
1437 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size <= 0)", func);
1438 return;
1439 }
1440
1441 if (flags & ~(GL_MAP_READ_BIT |
1442 GL_MAP_WRITE_BIT |
1443 GL_MAP_PERSISTENT_BIT |
1444 GL_MAP_COHERENT_BIT |
1445 GL_DYNAMIC_STORAGE_BIT |
1446 GL_CLIENT_STORAGE_BIT)) {
1447 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid flag bits set)", func);
1448 return;
1449 }
1450
1451 if (flags & GL_MAP_PERSISTENT_BIT &&
1452 !(flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))) {
1453 _mesa_error(ctx, GL_INVALID_VALUE,
1454 "%s(PERSISTENT and flags!=READ/WRITE)", func);
1455 return;
1456 }
1457
1458 if (flags & GL_MAP_COHERENT_BIT && !(flags & GL_MAP_PERSISTENT_BIT)) {
1459 _mesa_error(ctx, GL_INVALID_VALUE,
1460 "%s(COHERENT and flags!=PERSISTENT)", func);
1461 return;
1462 }
1463
1464 if (bufObj->Immutable) {
1465 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
1466 return;
1467 }
1468
1469 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1470 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1471
1472 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1473
1474 bufObj->Written = GL_TRUE;
1475 bufObj->Immutable = GL_TRUE;
1476
1477 assert(ctx->Driver.BufferData);
1478 if (!ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
1479 flags, bufObj)) {
1480 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
1481 /* Even though the interaction between AMD_pinned_memory and
1482 * glBufferStorage is not described in the spec, Graham Sellers
1483 * said that it should behave the same as glBufferData.
1484 */
1485 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1486 }
1487 else {
1488 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1489 }
1490 }
1491 }
1492
1493 void GLAPIENTRY
1494 _mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data,
1495 GLbitfield flags)
1496 {
1497 GET_CURRENT_CONTEXT(ctx);
1498 struct gl_buffer_object *bufObj;
1499
1500 bufObj = get_buffer(ctx, "glBufferStorage", target, GL_INVALID_OPERATION);
1501 if (!bufObj)
1502 return;
1503
1504 _mesa_buffer_storage(ctx, bufObj, target, size, data, flags,
1505 "glBufferStorage");
1506 }
1507
1508 void GLAPIENTRY
1509 _mesa_NamedBufferStorage(GLuint buffer, GLsizeiptr size, const GLvoid *data,
1510 GLbitfield flags)
1511 {
1512 GET_CURRENT_CONTEXT(ctx);
1513 struct gl_buffer_object *bufObj;
1514
1515 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferStorage");
1516 if (!bufObj)
1517 return;
1518
1519 /*
1520 * In direct state access, buffer objects have an unspecified target since
1521 * they are not required to be bound.
1522 */
1523 _mesa_buffer_storage(ctx, bufObj, GL_NONE, size, data, flags,
1524 "glNamedBufferStorage");
1525 }
1526
1527
1528 void
1529 _mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1530 GLenum target, GLsizeiptr size, const GLvoid *data,
1531 GLenum usage, const char *func)
1532 {
1533 bool valid_usage;
1534
1535 if (MESA_VERBOSE & VERBOSE_API)
1536 _mesa_debug(ctx, "%s(%s, %ld, %p, %s)\n",
1537 func,
1538 _mesa_lookup_enum_by_nr(target),
1539 (long int) size, data,
1540 _mesa_lookup_enum_by_nr(usage));
1541
1542 if (size < 0) {
1543 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", func);
1544 return;
1545 }
1546
1547 switch (usage) {
1548 case GL_STREAM_DRAW_ARB:
1549 valid_usage = (ctx->API != API_OPENGLES);
1550 break;
1551
1552 case GL_STATIC_DRAW_ARB:
1553 case GL_DYNAMIC_DRAW_ARB:
1554 valid_usage = true;
1555 break;
1556
1557 case GL_STREAM_READ_ARB:
1558 case GL_STREAM_COPY_ARB:
1559 case GL_STATIC_READ_ARB:
1560 case GL_STATIC_COPY_ARB:
1561 case GL_DYNAMIC_READ_ARB:
1562 case GL_DYNAMIC_COPY_ARB:
1563 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1564 break;
1565
1566 default:
1567 valid_usage = false;
1568 break;
1569 }
1570
1571 if (!valid_usage) {
1572 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid usage: %s)", func,
1573 _mesa_lookup_enum_by_nr(usage));
1574 return;
1575 }
1576
1577 if (bufObj->Immutable) {
1578 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
1579 return;
1580 }
1581
1582 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1583 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1584
1585 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1586
1587 bufObj->Written = GL_TRUE;
1588
1589 #ifdef VBO_DEBUG
1590 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1591 bufObj->Name, size, data, usage);
1592 #endif
1593
1594 #ifdef BOUNDS_CHECK
1595 size += 100;
1596 #endif
1597
1598 assert(ctx->Driver.BufferData);
1599 if (!ctx->Driver.BufferData(ctx, target, size, data, usage,
1600 GL_MAP_READ_BIT |
1601 GL_MAP_WRITE_BIT |
1602 GL_DYNAMIC_STORAGE_BIT,
1603 bufObj)) {
1604 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
1605 /* From GL_AMD_pinned_memory:
1606 *
1607 * INVALID_OPERATION is generated by BufferData if <target> is
1608 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1609 * mapped to the GPU address space.
1610 */
1611 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1612 }
1613 else {
1614 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1615 }
1616 }
1617 }
1618
1619 void GLAPIENTRY
1620 _mesa_BufferData(GLenum target, GLsizeiptr size,
1621 const GLvoid *data, GLenum usage)
1622 {
1623 GET_CURRENT_CONTEXT(ctx);
1624 struct gl_buffer_object *bufObj;
1625
1626 bufObj = get_buffer(ctx, "glBufferData", target, GL_INVALID_OPERATION);
1627 if (!bufObj)
1628 return;
1629
1630 _mesa_buffer_data(ctx, bufObj, target, size, data, usage,
1631 "glBufferData");
1632 }
1633
1634 void GLAPIENTRY
1635 _mesa_NamedBufferData(GLuint buffer, GLsizeiptr size, const GLvoid *data,
1636 GLenum usage)
1637 {
1638 GET_CURRENT_CONTEXT(ctx);
1639 struct gl_buffer_object *bufObj;
1640
1641 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferData");
1642 if (!bufObj)
1643 return;
1644
1645 /* In direct state access, buffer objects have an unspecified target since
1646 * they are not required to be bound.
1647 */
1648 _mesa_buffer_data(ctx, bufObj, GL_NONE, size, data, usage,
1649 "glNamedBufferData");
1650 }
1651
1652
1653 /**
1654 * Implementation for glBufferSubData and glNamedBufferSubData.
1655 *
1656 * \param ctx GL context.
1657 * \param bufObj The buffer object.
1658 * \param offset Offset of the first byte of the subdata range.
1659 * \param size Size, in bytes, of the subdata range.
1660 * \param data The data store.
1661 * \param func Name of calling function for recording errors.
1662 *
1663 */
1664 void
1665 _mesa_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1666 GLintptr offset, GLsizeiptr size, const GLvoid *data,
1667 const char *func)
1668 {
1669 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
1670 false, func)) {
1671 /* error already recorded */
1672 return;
1673 }
1674
1675 if (bufObj->Immutable &&
1676 !(bufObj->StorageFlags & GL_DYNAMIC_STORAGE_BIT)) {
1677 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1678 return;
1679 }
1680
1681 if (size == 0)
1682 return;
1683
1684 bufObj->Written = GL_TRUE;
1685
1686 assert(ctx->Driver.BufferSubData);
1687 ctx->Driver.BufferSubData(ctx, offset, size, data, bufObj);
1688 }
1689
1690 void GLAPIENTRY
1691 _mesa_BufferSubData(GLenum target, GLintptr offset,
1692 GLsizeiptr size, const GLvoid *data)
1693 {
1694 GET_CURRENT_CONTEXT(ctx);
1695 struct gl_buffer_object *bufObj;
1696
1697 bufObj = get_buffer(ctx, "glBufferSubData", target, GL_INVALID_OPERATION);
1698 if (!bufObj)
1699 return;
1700
1701 _mesa_buffer_sub_data(ctx, bufObj, offset, size, data, "glBufferSubData");
1702 }
1703
1704 void GLAPIENTRY
1705 _mesa_NamedBufferSubData(GLuint buffer, GLintptr offset,
1706 GLsizeiptr size, const GLvoid *data)
1707 {
1708 GET_CURRENT_CONTEXT(ctx);
1709 struct gl_buffer_object *bufObj;
1710
1711 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferSubData");
1712 if (!bufObj)
1713 return;
1714
1715 _mesa_buffer_sub_data(ctx, bufObj, offset, size, data,
1716 "glNamedBufferSubData");
1717 }
1718
1719
1720 void GLAPIENTRY
1721 _mesa_GetBufferSubData(GLenum target, GLintptr offset,
1722 GLsizeiptr size, GLvoid *data)
1723 {
1724 GET_CURRENT_CONTEXT(ctx);
1725 struct gl_buffer_object *bufObj;
1726
1727 bufObj = get_buffer(ctx, "glGetBufferSubData", target,
1728 GL_INVALID_OPERATION);
1729 if (!bufObj)
1730 return;
1731
1732 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
1733 "glGetBufferSubData")) {
1734 return;
1735 }
1736
1737 assert(ctx->Driver.GetBufferSubData);
1738 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
1739 }
1740
1741 void GLAPIENTRY
1742 _mesa_GetNamedBufferSubData(GLuint buffer, GLintptr offset,
1743 GLsizeiptr size, GLvoid *data)
1744 {
1745 GET_CURRENT_CONTEXT(ctx);
1746 struct gl_buffer_object *bufObj;
1747
1748 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
1749 "glGetNamedBufferSubData");
1750 if (!bufObj)
1751 return;
1752
1753 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
1754 "glGetNamedBufferSubData")) {
1755 return;
1756 }
1757
1758 assert(ctx->Driver.GetBufferSubData);
1759 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
1760 }
1761
1762
1763 /**
1764 * \param subdata true if caller is *SubData, false if *Data
1765 */
1766 void
1767 _mesa_clear_buffer_sub_data(struct gl_context *ctx,
1768 struct gl_buffer_object *bufObj,
1769 GLenum internalformat,
1770 GLintptr offset, GLsizeiptr size,
1771 GLenum format, GLenum type,
1772 const GLvoid *data,
1773 const char *func, bool subdata)
1774 {
1775 mesa_format mesaFormat;
1776 GLubyte clearValue[MAX_PIXEL_BYTES];
1777 GLsizeiptr clearValueSize;
1778
1779 /* This checks for disallowed mappings. */
1780 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
1781 subdata, func)) {
1782 return;
1783 }
1784
1785 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1786 format, type, func);
1787
1788 if (mesaFormat == MESA_FORMAT_NONE) {
1789 return;
1790 }
1791
1792 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1793 if (offset % clearValueSize != 0 || size % clearValueSize != 0) {
1794 _mesa_error(ctx, GL_INVALID_VALUE,
1795 "%s(offset or size is not a multiple of "
1796 "internalformat size)", func);
1797 return;
1798 }
1799
1800 if (data == NULL) {
1801 /* clear to zeros, per the spec */
1802 if (size > 0) {
1803 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1804 NULL, clearValueSize, bufObj);
1805 }
1806 return;
1807 }
1808
1809 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1810 format, type, data, func)) {
1811 return;
1812 }
1813
1814 if (size > 0) {
1815 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1816 clearValue, clearValueSize, bufObj);
1817 }
1818 }
1819
1820 void GLAPIENTRY
1821 _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
1822 GLenum type, const GLvoid *data)
1823 {
1824 GET_CURRENT_CONTEXT(ctx);
1825 struct gl_buffer_object *bufObj;
1826
1827 bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
1828 if (!bufObj)
1829 return;
1830
1831 _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size,
1832 format, type, data,
1833 "glClearBufferData", false);
1834 }
1835
1836 void GLAPIENTRY
1837 _mesa_ClearNamedBufferData(GLuint buffer, GLenum internalformat,
1838 GLenum format, GLenum type, const GLvoid *data)
1839 {
1840 GET_CURRENT_CONTEXT(ctx);
1841 struct gl_buffer_object *bufObj;
1842
1843 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glClearNamedBufferData");
1844 if (!bufObj)
1845 return;
1846
1847 _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size,
1848 format, type, data,
1849 "glClearNamedBufferData", false);
1850 }
1851
1852
1853 void GLAPIENTRY
1854 _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
1855 GLintptr offset, GLsizeiptr size,
1856 GLenum format, GLenum type,
1857 const GLvoid *data)
1858 {
1859 GET_CURRENT_CONTEXT(ctx);
1860 struct gl_buffer_object *bufObj;
1861
1862 bufObj = get_buffer(ctx, "glClearBufferSubData", target, GL_INVALID_VALUE);
1863 if (!bufObj)
1864 return;
1865
1866 _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size,
1867 format, type, data,
1868 "glClearBufferSubData", true);
1869 }
1870
1871 void GLAPIENTRY
1872 _mesa_ClearNamedBufferSubData(GLuint buffer, GLenum internalformat,
1873 GLintptr offset, GLsizeiptr size,
1874 GLenum format, GLenum type,
1875 const GLvoid *data)
1876 {
1877 GET_CURRENT_CONTEXT(ctx);
1878 struct gl_buffer_object *bufObj;
1879
1880 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
1881 "glClearNamedBufferSubData");
1882 if (!bufObj)
1883 return;
1884
1885 _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size,
1886 format, type, data,
1887 "glClearNamedBufferSubData", true);
1888 }
1889
1890
1891 GLboolean
1892 _mesa_unmap_buffer(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1893 const char *func)
1894 {
1895 GLboolean status = GL_TRUE;
1896 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1897
1898 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
1899 _mesa_error(ctx, GL_INVALID_OPERATION,
1900 "%s(buffer is not mapped)", func);
1901 return GL_FALSE;
1902 }
1903
1904 #ifdef BOUNDS_CHECK
1905 if (bufObj->Access != GL_READ_ONLY_ARB) {
1906 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1907 GLuint i;
1908 /* check that last 100 bytes are still = magic value */
1909 for (i = 0; i < 100; i++) {
1910 GLuint pos = bufObj->Size - i - 1;
1911 if (buf[pos] != 123) {
1912 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1913 " at position %d (value = %u)\n",
1914 pos, buf[pos]);
1915 }
1916 }
1917 }
1918 #endif
1919
1920 #ifdef VBO_DEBUG
1921 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1922 GLuint i, unchanged = 0;
1923 GLubyte *b = (GLubyte *) bufObj->Pointer;
1924 GLint pos = -1;
1925 /* check which bytes changed */
1926 for (i = 0; i < bufObj->Size - 1; i++) {
1927 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1928 unchanged++;
1929 if (pos == -1)
1930 pos = i;
1931 }
1932 }
1933 if (unchanged) {
1934 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1935 bufObj->Name, unchanged, bufObj->Size, pos);
1936 }
1937 }
1938 #endif
1939
1940 status = ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_USER);
1941 bufObj->Mappings[MAP_USER].AccessFlags = 0;
1942 assert(bufObj->Mappings[MAP_USER].Pointer == NULL);
1943 assert(bufObj->Mappings[MAP_USER].Offset == 0);
1944 assert(bufObj->Mappings[MAP_USER].Length == 0);
1945
1946 return status;
1947 }
1948
1949 GLboolean GLAPIENTRY
1950 _mesa_UnmapBuffer(GLenum target)
1951 {
1952 GET_CURRENT_CONTEXT(ctx);
1953 struct gl_buffer_object *bufObj;
1954
1955 bufObj = get_buffer(ctx, "glUnmapBuffer", target, GL_INVALID_OPERATION);
1956 if (!bufObj)
1957 return GL_FALSE;
1958
1959 return _mesa_unmap_buffer(ctx, bufObj, "glUnmapBuffer");
1960 }
1961
1962 GLboolean GLAPIENTRY
1963 _mesa_UnmapNamedBuffer(GLuint buffer)
1964 {
1965 GET_CURRENT_CONTEXT(ctx);
1966 struct gl_buffer_object *bufObj;
1967
1968 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glUnmapNamedBuffer");
1969 if (!bufObj)
1970 return GL_FALSE;
1971
1972 return _mesa_unmap_buffer(ctx, bufObj, "glUnmapNamedBuffer");
1973 }
1974
1975
1976 static bool
1977 get_buffer_parameter(struct gl_context *ctx,
1978 struct gl_buffer_object *bufObj, GLenum pname,
1979 GLint64 *params, const char *func)
1980 {
1981 switch (pname) {
1982 case GL_BUFFER_SIZE_ARB:
1983 *params = bufObj->Size;
1984 break;
1985 case GL_BUFFER_USAGE_ARB:
1986 *params = bufObj->Usage;
1987 break;
1988 case GL_BUFFER_ACCESS_ARB:
1989 *params = simplified_access_mode(ctx,
1990 bufObj->Mappings[MAP_USER].AccessFlags);
1991 break;
1992 case GL_BUFFER_MAPPED_ARB:
1993 *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
1994 break;
1995 case GL_BUFFER_ACCESS_FLAGS:
1996 if (!ctx->Extensions.ARB_map_buffer_range)
1997 goto invalid_pname;
1998 *params = bufObj->Mappings[MAP_USER].AccessFlags;
1999 break;
2000 case GL_BUFFER_MAP_OFFSET:
2001 if (!ctx->Extensions.ARB_map_buffer_range)
2002 goto invalid_pname;
2003 *params = bufObj->Mappings[MAP_USER].Offset;
2004 break;
2005 case GL_BUFFER_MAP_LENGTH:
2006 if (!ctx->Extensions.ARB_map_buffer_range)
2007 goto invalid_pname;
2008 *params = bufObj->Mappings[MAP_USER].Length;
2009 break;
2010 case GL_BUFFER_IMMUTABLE_STORAGE:
2011 if (!ctx->Extensions.ARB_buffer_storage)
2012 goto invalid_pname;
2013 *params = bufObj->Immutable;
2014 break;
2015 case GL_BUFFER_STORAGE_FLAGS:
2016 if (!ctx->Extensions.ARB_buffer_storage)
2017 goto invalid_pname;
2018 *params = bufObj->StorageFlags;
2019 break;
2020 default:
2021 goto invalid_pname;
2022 }
2023
2024 return true;
2025
2026 invalid_pname:
2027 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname: %s)", func,
2028 _mesa_lookup_enum_by_nr(pname));
2029 return false;
2030 }
2031
2032 void GLAPIENTRY
2033 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
2034 {
2035 GET_CURRENT_CONTEXT(ctx);
2036 struct gl_buffer_object *bufObj;
2037 GLint64 parameter;
2038
2039 bufObj = get_buffer(ctx, "glGetBufferParameteriv", target,
2040 GL_INVALID_OPERATION);
2041 if (!bufObj)
2042 return;
2043
2044 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2045 "glGetBufferParameteriv"))
2046 return; /* Error already recorded. */
2047
2048 *params = (GLint) parameter;
2049 }
2050
2051 void GLAPIENTRY
2052 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
2053 {
2054 GET_CURRENT_CONTEXT(ctx);
2055 struct gl_buffer_object *bufObj;
2056 GLint64 parameter;
2057
2058 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
2059 GL_INVALID_OPERATION);
2060 if (!bufObj)
2061 return;
2062
2063 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2064 "glGetBufferParameteri64v"))
2065 return; /* Error already recorded. */
2066
2067 *params = parameter;
2068 }
2069
2070 void GLAPIENTRY
2071 _mesa_GetNamedBufferParameteriv(GLuint buffer, GLenum pname, GLint *params)
2072 {
2073 GET_CURRENT_CONTEXT(ctx);
2074 struct gl_buffer_object *bufObj;
2075 GLint64 parameter;
2076
2077 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2078 "glGetNamedBufferParameteriv");
2079 if (!bufObj)
2080 return;
2081
2082 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2083 "glGetNamedBufferParameteriv"))
2084 return; /* Error already recorded. */
2085
2086 *params = (GLint) parameter;
2087 }
2088
2089 void GLAPIENTRY
2090 _mesa_GetNamedBufferParameteri64v(GLuint buffer, GLenum pname,
2091 GLint64 *params)
2092 {
2093 GET_CURRENT_CONTEXT(ctx);
2094 struct gl_buffer_object *bufObj;
2095 GLint64 parameter;
2096
2097 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2098 "glGetNamedBufferParameteri64v");
2099 if (!bufObj)
2100 return;
2101
2102 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2103 "glGetNamedBufferParameteri64v"))
2104 return; /* Error already recorded. */
2105
2106 *params = parameter;
2107 }
2108
2109
2110 void GLAPIENTRY
2111 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
2112 {
2113 GET_CURRENT_CONTEXT(ctx);
2114 struct gl_buffer_object *bufObj;
2115
2116 if (pname != GL_BUFFER_MAP_POINTER) {
2117 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointerv(pname != "
2118 "GL_BUFFER_MAP_POINTER)");
2119 return;
2120 }
2121
2122 bufObj = get_buffer(ctx, "glGetBufferPointerv", target,
2123 GL_INVALID_OPERATION);
2124 if (!bufObj)
2125 return;
2126
2127 *params = bufObj->Mappings[MAP_USER].Pointer;
2128 }
2129
2130 void GLAPIENTRY
2131 _mesa_GetNamedBufferPointerv(GLuint buffer, GLenum pname, GLvoid **params)
2132 {
2133 GET_CURRENT_CONTEXT(ctx);
2134 struct gl_buffer_object *bufObj;
2135
2136 if (pname != GL_BUFFER_MAP_POINTER) {
2137 _mesa_error(ctx, GL_INVALID_ENUM, "glGetNamedBufferPointerv(pname != "
2138 "GL_BUFFER_MAP_POINTER)");
2139 return;
2140 }
2141
2142 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2143 "glGetNamedBufferPointerv");
2144 if (!bufObj)
2145 return;
2146
2147 *params = bufObj->Mappings[MAP_USER].Pointer;
2148 }
2149
2150
2151 void
2152 _mesa_copy_buffer_sub_data(struct gl_context *ctx,
2153 struct gl_buffer_object *src,
2154 struct gl_buffer_object *dst,
2155 GLintptr readOffset, GLintptr writeOffset,
2156 GLsizeiptr size, const char *func)
2157 {
2158 if (_mesa_check_disallowed_mapping(src)) {
2159 _mesa_error(ctx, GL_INVALID_OPERATION,
2160 "%s(readBuffer is mapped)", func);
2161 return;
2162 }
2163
2164 if (_mesa_check_disallowed_mapping(dst)) {
2165 _mesa_error(ctx, GL_INVALID_OPERATION,
2166 "%s(writeBuffer is mapped)", func);
2167 return;
2168 }
2169
2170 if (readOffset < 0) {
2171 _mesa_error(ctx, GL_INVALID_VALUE,
2172 "%s(readOffset %d < 0)", func, (int) readOffset);
2173 return;
2174 }
2175
2176 if (writeOffset < 0) {
2177 _mesa_error(ctx, GL_INVALID_VALUE,
2178 "%s(writeOffset %d < 0)", func, (int) writeOffset);
2179 return;
2180 }
2181
2182 if (size < 0) {
2183 _mesa_error(ctx, GL_INVALID_VALUE,
2184 "%s(size %d < 0)", func, (int) size);
2185 return;
2186 }
2187
2188 if (readOffset + size > src->Size) {
2189 _mesa_error(ctx, GL_INVALID_VALUE,
2190 "%s(readOffset %d + size %d > src_buffer_size %d)", func,
2191 (int) readOffset, (int) size, (int) src->Size);
2192 return;
2193 }
2194
2195 if (writeOffset + size > dst->Size) {
2196 _mesa_error(ctx, GL_INVALID_VALUE,
2197 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func,
2198 (int) writeOffset, (int) size, (int) dst->Size);
2199 return;
2200 }
2201
2202 if (src == dst) {
2203 if (readOffset + size <= writeOffset) {
2204 /* OK */
2205 }
2206 else if (writeOffset + size <= readOffset) {
2207 /* OK */
2208 }
2209 else {
2210 /* overlapping src/dst is illegal */
2211 _mesa_error(ctx, GL_INVALID_VALUE,
2212 "%s(overlapping src/dst)", func);
2213 return;
2214 }
2215 }
2216
2217 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
2218 }
2219
2220 void GLAPIENTRY
2221 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
2222 GLintptr readOffset, GLintptr writeOffset,
2223 GLsizeiptr size)
2224 {
2225 GET_CURRENT_CONTEXT(ctx);
2226 struct gl_buffer_object *src, *dst;
2227
2228 src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
2229 GL_INVALID_OPERATION);
2230 if (!src)
2231 return;
2232
2233 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
2234 GL_INVALID_OPERATION);
2235 if (!dst)
2236 return;
2237
2238 _mesa_copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
2239 "glCopyBufferSubData");
2240 }
2241
2242 void GLAPIENTRY
2243 _mesa_CopyNamedBufferSubData(GLuint readBuffer, GLuint writeBuffer,
2244 GLintptr readOffset, GLintptr writeOffset,
2245 GLsizeiptr size)
2246 {
2247 GET_CURRENT_CONTEXT(ctx);
2248 struct gl_buffer_object *src, *dst;
2249
2250 src = _mesa_lookup_bufferobj_err(ctx, readBuffer,
2251 "glCopyNamedBufferSubData");
2252 if (!src)
2253 return;
2254
2255 dst = _mesa_lookup_bufferobj_err(ctx, writeBuffer,
2256 "glCopyNamedBufferSubData");
2257 if (!dst)
2258 return;
2259
2260 _mesa_copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
2261 "glCopyNamedBufferSubData");
2262 }
2263
2264
2265 void *
2266 _mesa_map_buffer_range(struct gl_context *ctx,
2267 struct gl_buffer_object *bufObj,
2268 GLintptr offset, GLsizeiptr length,
2269 GLbitfield access, const char *func)
2270 {
2271 void *map;
2272 GLbitfield allowed_access;
2273
2274 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
2275
2276 if (offset < 0) {
2277 _mesa_error(ctx, GL_INVALID_VALUE,
2278 "%s(offset %ld < 0)", func, (long) offset);
2279 return NULL;
2280 }
2281
2282 if (length < 0) {
2283 _mesa_error(ctx, GL_INVALID_VALUE,
2284 "%s(length %ld < 0)", func, (long) length);
2285 return NULL;
2286 }
2287
2288 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2289 *
2290 * "An INVALID_OPERATION error is generated for any of the following
2291 * conditions:
2292 *
2293 * * <length> is zero."
2294 *
2295 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2296 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2297 * either.
2298 */
2299 if (length == 0) {
2300 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(length = 0)", func);
2301 return NULL;
2302 }
2303
2304 allowed_access = GL_MAP_READ_BIT |
2305 GL_MAP_WRITE_BIT |
2306 GL_MAP_INVALIDATE_RANGE_BIT |
2307 GL_MAP_INVALIDATE_BUFFER_BIT |
2308 GL_MAP_FLUSH_EXPLICIT_BIT |
2309 GL_MAP_UNSYNCHRONIZED_BIT;
2310
2311 if (ctx->Extensions.ARB_buffer_storage) {
2312 allowed_access |= GL_MAP_PERSISTENT_BIT |
2313 GL_MAP_COHERENT_BIT;
2314 }
2315
2316 if (access & ~allowed_access) {
2317 /* generate an error if any bits other than those allowed are set */
2318 _mesa_error(ctx, GL_INVALID_VALUE,
2319 "%s(access has undefined bits set)", func);
2320 return NULL;
2321 }
2322
2323 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
2324 _mesa_error(ctx, GL_INVALID_OPERATION,
2325 "%s(access indicates neither read or write)", func);
2326 return NULL;
2327 }
2328
2329 if ((access & GL_MAP_READ_BIT) &&
2330 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
2331 GL_MAP_INVALIDATE_BUFFER_BIT |
2332 GL_MAP_UNSYNCHRONIZED_BIT))) {
2333 _mesa_error(ctx, GL_INVALID_OPERATION,
2334 "%s(read access with disallowed bits)", func);
2335 return NULL;
2336 }
2337
2338 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
2339 ((access & GL_MAP_WRITE_BIT) == 0)) {
2340 _mesa_error(ctx, GL_INVALID_OPERATION,
2341 "%s(access has flush explicit without write)", func);
2342 return NULL;
2343 }
2344
2345 if (access & GL_MAP_READ_BIT &&
2346 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
2347 _mesa_error(ctx, GL_INVALID_OPERATION,
2348 "%s(buffer does not allow read access)", func);
2349 return NULL;
2350 }
2351
2352 if (access & GL_MAP_WRITE_BIT &&
2353 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
2354 _mesa_error(ctx, GL_INVALID_OPERATION,
2355 "%s(buffer does not allow write access)", func);
2356 return NULL;
2357 }
2358
2359 if (access & GL_MAP_COHERENT_BIT &&
2360 !(bufObj->StorageFlags & GL_MAP_COHERENT_BIT)) {
2361 _mesa_error(ctx, GL_INVALID_OPERATION,
2362 "%s(buffer does not allow coherent access)", func);
2363 return NULL;
2364 }
2365
2366 if (access & GL_MAP_PERSISTENT_BIT &&
2367 !(bufObj->StorageFlags & GL_MAP_PERSISTENT_BIT)) {
2368 _mesa_error(ctx, GL_INVALID_OPERATION,
2369 "%s(buffer does not allow persistent access)", func);
2370 return NULL;
2371 }
2372
2373 if (offset + length > bufObj->Size) {
2374 _mesa_error(ctx, GL_INVALID_VALUE,
2375 "%s(offset %ld + length %ld > buffer_size %ld)", func,
2376 offset, length, bufObj->Size);
2377 return NULL;
2378 }
2379
2380 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2381 _mesa_error(ctx, GL_INVALID_OPERATION,
2382 "%s(buffer already mapped)", func);
2383 return NULL;
2384 }
2385
2386 if (!bufObj->Size) {
2387 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(buffer size = 0)", func);
2388 return NULL;
2389 }
2390
2391
2392 assert(ctx->Driver.MapBufferRange);
2393 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj,
2394 MAP_USER);
2395 if (!map) {
2396 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(map failed)", func);
2397 }
2398 else {
2399 /* The driver callback should have set all these fields.
2400 * This is important because other modules (like VBO) might call
2401 * the driver function directly.
2402 */
2403 assert(bufObj->Mappings[MAP_USER].Pointer == map);
2404 assert(bufObj->Mappings[MAP_USER].Length == length);
2405 assert(bufObj->Mappings[MAP_USER].Offset == offset);
2406 assert(bufObj->Mappings[MAP_USER].AccessFlags == access);
2407 }
2408
2409 if (access & GL_MAP_WRITE_BIT)
2410 bufObj->Written = GL_TRUE;
2411
2412 #ifdef VBO_DEBUG
2413 if (strstr(func, "Range") == NULL) { /* If not MapRange */
2414 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2415 bufObj->Name, bufObj->Size, access);
2416 /* Access must be write only */
2417 if ((access & GL_MAP_WRITE_BIT) && (!(access & ~GL_MAP_WRITE_BIT))) {
2418 GLuint i;
2419 GLubyte *b = (GLubyte *) bufObj->Pointer;
2420 for (i = 0; i < bufObj->Size; i++)
2421 b[i] = i & 0xff;
2422 }
2423 }
2424 #endif
2425
2426 #ifdef BOUNDS_CHECK
2427 if (strstr(func, "Range") == NULL) { /* If not MapRange */
2428 GLubyte *buf = (GLubyte *) bufObj->Pointer;
2429 GLuint i;
2430 /* buffer is 100 bytes larger than requested, fill with magic value */
2431 for (i = 0; i < 100; i++) {
2432 buf[bufObj->Size - i - 1] = 123;
2433 }
2434 }
2435 #endif
2436
2437 return map;
2438 }
2439
2440 void * GLAPIENTRY
2441 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
2442 GLbitfield access)
2443 {
2444 GET_CURRENT_CONTEXT(ctx);
2445 struct gl_buffer_object *bufObj;
2446
2447 if (!ctx->Extensions.ARB_map_buffer_range) {
2448 _mesa_error(ctx, GL_INVALID_OPERATION,
2449 "glMapBufferRange(ARB_map_buffer_range not supported)");
2450 return NULL;
2451 }
2452
2453 bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
2454 if (!bufObj)
2455 return NULL;
2456
2457 return _mesa_map_buffer_range(ctx, bufObj, offset, length, access,
2458 "glMapBufferRange");
2459 }
2460
2461 void * GLAPIENTRY
2462 _mesa_MapNamedBufferRange(GLuint buffer, GLintptr offset, GLsizeiptr length,
2463 GLbitfield access)
2464 {
2465 GET_CURRENT_CONTEXT(ctx);
2466 struct gl_buffer_object *bufObj;
2467
2468 if (!ctx->Extensions.ARB_map_buffer_range) {
2469 _mesa_error(ctx, GL_INVALID_OPERATION,
2470 "glMapNamedBufferRange("
2471 "ARB_map_buffer_range not supported)");
2472 return NULL;
2473 }
2474
2475 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBufferRange");
2476 if (!bufObj)
2477 return NULL;
2478
2479 return _mesa_map_buffer_range(ctx, bufObj, offset, length, access,
2480 "glMapNamedBufferRange");
2481 }
2482
2483 /**
2484 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2485 * flags for input to _mesa_map_buffer_range.
2486 *
2487 * \return true if the type of requested access is permissible.
2488 */
2489 static bool
2490 get_map_buffer_access_flags(struct gl_context *ctx, GLenum access,
2491 GLbitfield *flags)
2492 {
2493 switch (access) {
2494 case GL_READ_ONLY_ARB:
2495 *flags = GL_MAP_READ_BIT;
2496 return _mesa_is_desktop_gl(ctx);
2497 case GL_WRITE_ONLY_ARB:
2498 *flags = GL_MAP_WRITE_BIT;
2499 return true;
2500 case GL_READ_WRITE_ARB:
2501 *flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
2502 return _mesa_is_desktop_gl(ctx);
2503 default:
2504 return false;
2505 }
2506 }
2507
2508 void * GLAPIENTRY
2509 _mesa_MapBuffer(GLenum target, GLenum access)
2510 {
2511 GET_CURRENT_CONTEXT(ctx);
2512 struct gl_buffer_object *bufObj;
2513 GLbitfield accessFlags;
2514
2515 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
2516 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBuffer(invalid access)");
2517 return NULL;
2518 }
2519
2520 bufObj = get_buffer(ctx, "glMapBuffer", target, GL_INVALID_OPERATION);
2521 if (!bufObj)
2522 return NULL;
2523
2524 return _mesa_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2525 "glMapBuffer");
2526 }
2527
2528 void * GLAPIENTRY
2529 _mesa_MapNamedBuffer(GLuint buffer, GLenum access)
2530 {
2531 GET_CURRENT_CONTEXT(ctx);
2532 struct gl_buffer_object *bufObj;
2533 GLbitfield accessFlags;
2534
2535 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
2536 _mesa_error(ctx, GL_INVALID_ENUM, "glMapNamedBuffer(invalid access)");
2537 return NULL;
2538 }
2539
2540 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBuffer");
2541 if (!bufObj)
2542 return NULL;
2543
2544 return _mesa_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2545 "glMapNamedBuffer");
2546 }
2547
2548
2549 void
2550 _mesa_flush_mapped_buffer_range(struct gl_context *ctx,
2551 struct gl_buffer_object *bufObj,
2552 GLintptr offset, GLsizeiptr length,
2553 const char *func)
2554 {
2555 if (!ctx->Extensions.ARB_map_buffer_range) {
2556 _mesa_error(ctx, GL_INVALID_OPERATION,
2557 "%s(ARB_map_buffer_range not supported)", func);
2558 return;
2559 }
2560
2561 if (offset < 0) {
2562 _mesa_error(ctx, GL_INVALID_VALUE,
2563 "%s(offset %ld < 0)", func, (long) offset);
2564 return;
2565 }
2566
2567 if (length < 0) {
2568 _mesa_error(ctx, GL_INVALID_VALUE,
2569 "%s(length %ld < 0)", func, (long) length);
2570 return;
2571 }
2572
2573 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2574 /* buffer is not mapped */
2575 _mesa_error(ctx, GL_INVALID_OPERATION,
2576 "%s(buffer is not mapped)", func);
2577 return;
2578 }
2579
2580 if ((bufObj->Mappings[MAP_USER].AccessFlags &
2581 GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
2582 _mesa_error(ctx, GL_INVALID_OPERATION,
2583 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func);
2584 return;
2585 }
2586
2587 if (offset + length > bufObj->Mappings[MAP_USER].Length) {
2588 _mesa_error(ctx, GL_INVALID_VALUE,
2589 "%s(offset %ld + length %ld > mapped length %ld)", func,
2590 (long) offset, (long) length,
2591 (long) bufObj->Mappings[MAP_USER].Length);
2592 return;
2593 }
2594
2595 assert(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_WRITE_BIT);
2596
2597 if (ctx->Driver.FlushMappedBufferRange)
2598 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
2599 MAP_USER);
2600 }
2601
2602 void GLAPIENTRY
2603 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset,
2604 GLsizeiptr length)
2605 {
2606 GET_CURRENT_CONTEXT(ctx);
2607 struct gl_buffer_object *bufObj;
2608
2609 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
2610 GL_INVALID_OPERATION);
2611 if (!bufObj)
2612 return;
2613
2614 _mesa_flush_mapped_buffer_range(ctx, bufObj, offset, length,
2615 "glFlushMappedBufferRange");
2616 }
2617
2618 void GLAPIENTRY
2619 _mesa_FlushMappedNamedBufferRange(GLuint buffer, GLintptr offset,
2620 GLsizeiptr length)
2621 {
2622 GET_CURRENT_CONTEXT(ctx);
2623 struct gl_buffer_object *bufObj;
2624
2625 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2626 "glFlushMappedNamedBufferRange");
2627 if (!bufObj)
2628 return;
2629
2630 _mesa_flush_mapped_buffer_range(ctx, bufObj, offset, length,
2631 "glFlushMappedNamedBufferRange");
2632 }
2633
2634
2635 static GLenum
2636 buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2637 {
2638 struct gl_buffer_object *bufObj;
2639 GLenum retval;
2640
2641 bufObj = _mesa_lookup_bufferobj(ctx, name);
2642 if (!bufObj) {
2643 _mesa_error(ctx, GL_INVALID_VALUE,
2644 "glObjectPurgeable(name = 0x%x)", name);
2645 return 0;
2646 }
2647 if (!_mesa_is_bufferobj(bufObj)) {
2648 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
2649 return 0;
2650 }
2651
2652 if (bufObj->Purgeable) {
2653 _mesa_error(ctx, GL_INVALID_OPERATION,
2654 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2655 return GL_VOLATILE_APPLE;
2656 }
2657
2658 bufObj->Purgeable = GL_TRUE;
2659
2660 retval = GL_VOLATILE_APPLE;
2661 if (ctx->Driver.BufferObjectPurgeable)
2662 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
2663
2664 return retval;
2665 }
2666
2667
2668 static GLenum
2669 renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2670 {
2671 struct gl_renderbuffer *bufObj;
2672 GLenum retval;
2673
2674 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2675 if (!bufObj) {
2676 _mesa_error(ctx, GL_INVALID_VALUE,
2677 "glObjectUnpurgeable(name = 0x%x)", name);
2678 return 0;
2679 }
2680
2681 if (bufObj->Purgeable) {
2682 _mesa_error(ctx, GL_INVALID_OPERATION,
2683 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2684 return GL_VOLATILE_APPLE;
2685 }
2686
2687 bufObj->Purgeable = GL_TRUE;
2688
2689 retval = GL_VOLATILE_APPLE;
2690 if (ctx->Driver.RenderObjectPurgeable)
2691 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
2692
2693 return retval;
2694 }
2695
2696
2697 static GLenum
2698 texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2699 {
2700 struct gl_texture_object *bufObj;
2701 GLenum retval;
2702
2703 bufObj = _mesa_lookup_texture(ctx, name);
2704 if (!bufObj) {
2705 _mesa_error(ctx, GL_INVALID_VALUE,
2706 "glObjectPurgeable(name = 0x%x)", name);
2707 return 0;
2708 }
2709
2710 if (bufObj->Purgeable) {
2711 _mesa_error(ctx, GL_INVALID_OPERATION,
2712 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2713 return GL_VOLATILE_APPLE;
2714 }
2715
2716 bufObj->Purgeable = GL_TRUE;
2717
2718 retval = GL_VOLATILE_APPLE;
2719 if (ctx->Driver.TextureObjectPurgeable)
2720 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
2721
2722 return retval;
2723 }
2724
2725
2726 GLenum GLAPIENTRY
2727 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2728 {
2729 GLenum retval;
2730
2731 GET_CURRENT_CONTEXT(ctx);
2732 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2733
2734 if (name == 0) {
2735 _mesa_error(ctx, GL_INVALID_VALUE,
2736 "glObjectPurgeable(name = 0x%x)", name);
2737 return 0;
2738 }
2739
2740 switch (option) {
2741 case GL_VOLATILE_APPLE:
2742 case GL_RELEASED_APPLE:
2743 /* legal */
2744 break;
2745 default:
2746 _mesa_error(ctx, GL_INVALID_ENUM,
2747 "glObjectPurgeable(name = 0x%x) invalid option: %d",
2748 name, option);
2749 return 0;
2750 }
2751
2752 switch (objectType) {
2753 case GL_TEXTURE:
2754 retval = texture_object_purgeable(ctx, name, option);
2755 break;
2756 case GL_RENDERBUFFER_EXT:
2757 retval = renderbuffer_purgeable(ctx, name, option);
2758 break;
2759 case GL_BUFFER_OBJECT_APPLE:
2760 retval = buffer_object_purgeable(ctx, name, option);
2761 break;
2762 default:
2763 _mesa_error(ctx, GL_INVALID_ENUM,
2764 "glObjectPurgeable(name = 0x%x) invalid type: %d",
2765 name, objectType);
2766 return 0;
2767 }
2768
2769 /* In strict conformance to the spec, we must only return VOLATILE when
2770 * when passed the VOLATILE option. Madness.
2771 *
2772 * XXX First fix the spec, then fix me.
2773 */
2774 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
2775 }
2776
2777
2778 static GLenum
2779 buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2780 {
2781 struct gl_buffer_object *bufObj;
2782 GLenum retval;
2783
2784 bufObj = _mesa_lookup_bufferobj(ctx, name);
2785 if (!bufObj) {
2786 _mesa_error(ctx, GL_INVALID_VALUE,
2787 "glObjectUnpurgeable(name = 0x%x)", name);
2788 return 0;
2789 }
2790
2791 if (! bufObj->Purgeable) {
2792 _mesa_error(ctx, GL_INVALID_OPERATION,
2793 "glObjectUnpurgeable(name = 0x%x) object is "
2794 " already \"unpurged\"", name);
2795 return 0;
2796 }
2797
2798 bufObj->Purgeable = GL_FALSE;
2799
2800 retval = option;
2801 if (ctx->Driver.BufferObjectUnpurgeable)
2802 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
2803
2804 return retval;
2805 }
2806
2807
2808 static GLenum
2809 renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2810 {
2811 struct gl_renderbuffer *bufObj;
2812 GLenum retval;
2813
2814 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2815 if (!bufObj) {
2816 _mesa_error(ctx, GL_INVALID_VALUE,
2817 "glObjectUnpurgeable(name = 0x%x)", name);
2818 return 0;
2819 }
2820
2821 if (! bufObj->Purgeable) {
2822 _mesa_error(ctx, GL_INVALID_OPERATION,
2823 "glObjectUnpurgeable(name = 0x%x) object is "
2824 " already \"unpurged\"", name);
2825 return 0;
2826 }
2827
2828 bufObj->Purgeable = GL_FALSE;
2829
2830 retval = option;
2831 if (ctx->Driver.RenderObjectUnpurgeable)
2832 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
2833
2834 return retval;
2835 }
2836
2837
2838 static GLenum
2839 texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2840 {
2841 struct gl_texture_object *bufObj;
2842 GLenum retval;
2843
2844 bufObj = _mesa_lookup_texture(ctx, name);
2845 if (!bufObj) {
2846 _mesa_error(ctx, GL_INVALID_VALUE,
2847 "glObjectUnpurgeable(name = 0x%x)", name);
2848 return 0;
2849 }
2850
2851 if (! bufObj->Purgeable) {
2852 _mesa_error(ctx, GL_INVALID_OPERATION,
2853 "glObjectUnpurgeable(name = 0x%x) object is"
2854 " already \"unpurged\"", name);
2855 return 0;
2856 }
2857
2858 bufObj->Purgeable = GL_FALSE;
2859
2860 retval = option;
2861 if (ctx->Driver.TextureObjectUnpurgeable)
2862 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
2863
2864 return retval;
2865 }
2866
2867
2868 GLenum GLAPIENTRY
2869 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2870 {
2871 GET_CURRENT_CONTEXT(ctx);
2872 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2873
2874 if (name == 0) {
2875 _mesa_error(ctx, GL_INVALID_VALUE,
2876 "glObjectUnpurgeable(name = 0x%x)", name);
2877 return 0;
2878 }
2879
2880 switch (option) {
2881 case GL_RETAINED_APPLE:
2882 case GL_UNDEFINED_APPLE:
2883 /* legal */
2884 break;
2885 default:
2886 _mesa_error(ctx, GL_INVALID_ENUM,
2887 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2888 name, option);
2889 return 0;
2890 }
2891
2892 switch (objectType) {
2893 case GL_BUFFER_OBJECT_APPLE:
2894 return buffer_object_unpurgeable(ctx, name, option);
2895 case GL_TEXTURE:
2896 return texture_object_unpurgeable(ctx, name, option);
2897 case GL_RENDERBUFFER_EXT:
2898 return renderbuffer_unpurgeable(ctx, name, option);
2899 default:
2900 _mesa_error(ctx, GL_INVALID_ENUM,
2901 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2902 name, objectType);
2903 return 0;
2904 }
2905 }
2906
2907
2908 static void
2909 get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
2910 GLenum pname, GLint *params)
2911 {
2912 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
2913 if (!bufObj) {
2914 _mesa_error(ctx, GL_INVALID_VALUE,
2915 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
2916 return;
2917 }
2918
2919 switch (pname) {
2920 case GL_PURGEABLE_APPLE:
2921 *params = bufObj->Purgeable;
2922 break;
2923 default:
2924 _mesa_error(ctx, GL_INVALID_ENUM,
2925 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2926 name, pname);
2927 break;
2928 }
2929 }
2930
2931
2932 static void
2933 get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
2934 GLenum pname, GLint *params)
2935 {
2936 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
2937 if (!rb) {
2938 _mesa_error(ctx, GL_INVALID_VALUE,
2939 "glObjectUnpurgeable(name = 0x%x)", name);
2940 return;
2941 }
2942
2943 switch (pname) {
2944 case GL_PURGEABLE_APPLE:
2945 *params = rb->Purgeable;
2946 break;
2947 default:
2948 _mesa_error(ctx, GL_INVALID_ENUM,
2949 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2950 name, pname);
2951 break;
2952 }
2953 }
2954
2955
2956 static void
2957 get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
2958 GLenum pname, GLint *params)
2959 {
2960 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
2961 if (!texObj) {
2962 _mesa_error(ctx, GL_INVALID_VALUE,
2963 "glObjectUnpurgeable(name = 0x%x)", name);
2964 return;
2965 }
2966
2967 switch (pname) {
2968 case GL_PURGEABLE_APPLE:
2969 *params = texObj->Purgeable;
2970 break;
2971 default:
2972 _mesa_error(ctx, GL_INVALID_ENUM,
2973 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2974 name, pname);
2975 break;
2976 }
2977 }
2978
2979
2980 void GLAPIENTRY
2981 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
2982 GLint *params)
2983 {
2984 GET_CURRENT_CONTEXT(ctx);
2985
2986 if (name == 0) {
2987 _mesa_error(ctx, GL_INVALID_VALUE,
2988 "glGetObjectParameteriv(name = 0x%x)", name);
2989 return;
2990 }
2991
2992 switch (objectType) {
2993 case GL_TEXTURE:
2994 get_texture_object_parameteriv(ctx, name, pname, params);
2995 break;
2996 case GL_BUFFER_OBJECT_APPLE:
2997 get_buffer_object_parameteriv(ctx, name, pname, params);
2998 break;
2999 case GL_RENDERBUFFER_EXT:
3000 get_renderbuffer_parameteriv(ctx, name, pname, params);
3001 break;
3002 default:
3003 _mesa_error(ctx, GL_INVALID_ENUM,
3004 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
3005 name, objectType);
3006 }
3007 }
3008
3009 /**
3010 * Binds a buffer object to a uniform buffer binding point.
3011 *
3012 * The caller is responsible for flushing vertices and updating
3013 * NewDriverState.
3014 */
3015 static void
3016 set_ubo_binding(struct gl_context *ctx,
3017 struct gl_uniform_buffer_binding *binding,
3018 struct gl_buffer_object *bufObj,
3019 GLintptr offset,
3020 GLsizeiptr size,
3021 GLboolean autoSize)
3022 {
3023 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
3024
3025 binding->Offset = offset;
3026 binding->Size = size;
3027 binding->AutomaticSize = autoSize;
3028
3029 /* If this is a real buffer object, mark it has having been used
3030 * at some point as a UBO.
3031 */
3032 if (size >= 0)
3033 bufObj->UsageHistory |= USAGE_UNIFORM_BUFFER;
3034 }
3035
3036 /**
3037 * Binds a buffer object to a shader storage buffer binding point.
3038 *
3039 * The caller is responsible for flushing vertices and updating
3040 * NewDriverState.
3041 */
3042 static void
3043 set_ssbo_binding(struct gl_context *ctx,
3044 struct gl_shader_storage_buffer_binding *binding,
3045 struct gl_buffer_object *bufObj,
3046 GLintptr offset,
3047 GLsizeiptr size,
3048 GLboolean autoSize)
3049 {
3050 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
3051
3052 binding->Offset = offset;
3053 binding->Size = size;
3054 binding->AutomaticSize = autoSize;
3055
3056 /* If this is a real buffer object, mark it has having been used
3057 * at some point as a SSBO.
3058 */
3059 if (size >= 0)
3060 bufObj->UsageHistory |= USAGE_SHADER_STORAGE_BUFFER;
3061 }
3062
3063 /**
3064 * Binds a buffer object to a uniform buffer binding point.
3065 *
3066 * Unlike set_ubo_binding(), this function also flushes vertices
3067 * and updates NewDriverState. It also checks if the binding
3068 * has actually changed before updating it.
3069 */
3070 static void
3071 bind_uniform_buffer(struct gl_context *ctx,
3072 GLuint index,
3073 struct gl_buffer_object *bufObj,
3074 GLintptr offset,
3075 GLsizeiptr size,
3076 GLboolean autoSize)
3077 {
3078 struct gl_uniform_buffer_binding *binding =
3079 &ctx->UniformBufferBindings[index];
3080
3081 if (binding->BufferObject == bufObj &&
3082 binding->Offset == offset &&
3083 binding->Size == size &&
3084 binding->AutomaticSize == autoSize) {
3085 return;
3086 }
3087
3088 FLUSH_VERTICES(ctx, 0);
3089 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3090
3091 set_ubo_binding(ctx, binding, bufObj, offset, size, autoSize);
3092 }
3093
3094 /**
3095 * Binds a buffer object to a shader storage buffer binding point.
3096 *
3097 * Unlike set_ssbo_binding(), this function also flushes vertices
3098 * and updates NewDriverState. It also checks if the binding
3099 * has actually changed before updating it.
3100 */
3101 static void
3102 bind_shader_storage_buffer(struct gl_context *ctx,
3103 GLuint index,
3104 struct gl_buffer_object *bufObj,
3105 GLintptr offset,
3106 GLsizeiptr size,
3107 GLboolean autoSize)
3108 {
3109 struct gl_shader_storage_buffer_binding *binding =
3110 &ctx->ShaderStorageBufferBindings[index];
3111
3112 if (binding->BufferObject == bufObj &&
3113 binding->Offset == offset &&
3114 binding->Size == size &&
3115 binding->AutomaticSize == autoSize) {
3116 return;
3117 }
3118
3119 FLUSH_VERTICES(ctx, 0);
3120 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
3121
3122 set_ssbo_binding(ctx, binding, bufObj, offset, size, autoSize);
3123 }
3124
3125 /**
3126 * Bind a region of a buffer object to a uniform block binding point.
3127 * \param index the uniform buffer binding point index
3128 * \param bufObj the buffer object
3129 * \param offset offset to the start of buffer object region
3130 * \param size size of the buffer object region
3131 */
3132 static void
3133 bind_buffer_range_uniform_buffer(struct gl_context *ctx,
3134 GLuint index,
3135 struct gl_buffer_object *bufObj,
3136 GLintptr offset,
3137 GLsizeiptr size)
3138 {
3139 if (index >= ctx->Const.MaxUniformBufferBindings) {
3140 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
3141 return;
3142 }
3143
3144 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3145 _mesa_error(ctx, GL_INVALID_VALUE,
3146 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
3147 ctx->Const.UniformBufferOffsetAlignment);
3148 return;
3149 }
3150
3151 if (bufObj == ctx->Shared->NullBufferObj) {
3152 offset = -1;
3153 size = -1;
3154 }
3155
3156 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
3157 bind_uniform_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
3158 }
3159
3160
3161 /**
3162 * Bind a buffer object to a uniform block binding point.
3163 * As above, but offset = 0.
3164 */
3165 static void
3166 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
3167 GLuint index,
3168 struct gl_buffer_object *bufObj)
3169 {
3170 if (index >= ctx->Const.MaxUniformBufferBindings) {
3171 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
3172 return;
3173 }
3174
3175 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
3176
3177 if (bufObj == ctx->Shared->NullBufferObj)
3178 bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
3179 else
3180 bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
3181 }
3182
3183 /**
3184 * Bind a buffer object to a shader storage block binding point.
3185 * As above, but offset = 0.
3186 */
3187 static void
3188 bind_buffer_base_shader_storage_buffer(struct gl_context *ctx,
3189 GLuint index,
3190 struct gl_buffer_object *bufObj)
3191 {
3192 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
3193 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
3194 return;
3195 }
3196
3197 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
3198
3199 if (bufObj == ctx->Shared->NullBufferObj)
3200 bind_shader_storage_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
3201 else
3202 bind_shader_storage_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
3203 }
3204
3205 /**
3206 * Binds a buffer object to an atomic buffer binding point.
3207 *
3208 * The caller is responsible for validating the offset,
3209 * flushing the vertices and updating NewDriverState.
3210 */
3211 static void
3212 set_atomic_buffer_binding(struct gl_context *ctx,
3213 struct gl_atomic_buffer_binding *binding,
3214 struct gl_buffer_object *bufObj,
3215 GLintptr offset,
3216 GLsizeiptr size)
3217 {
3218 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
3219
3220 if (bufObj == ctx->Shared->NullBufferObj) {
3221 binding->Offset = -1;
3222 binding->Size = -1;
3223 } else {
3224 binding->Offset = offset;
3225 binding->Size = size;
3226 bufObj->UsageHistory |= USAGE_ATOMIC_COUNTER_BUFFER;
3227 }
3228 }
3229
3230 /**
3231 * Binds a buffer object to an atomic buffer binding point.
3232 *
3233 * Unlike set_atomic_buffer_binding(), this function also validates the
3234 * index and offset, flushes vertices, and updates NewDriverState.
3235 * It also checks if the binding has actually changing before
3236 * updating it.
3237 */
3238 static void
3239 bind_atomic_buffer(struct gl_context *ctx,
3240 unsigned index,
3241 struct gl_buffer_object *bufObj,
3242 GLintptr offset,
3243 GLsizeiptr size,
3244 const char *name)
3245 {
3246 struct gl_atomic_buffer_binding *binding;
3247
3248 if (index >= ctx->Const.MaxAtomicBufferBindings) {
3249 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
3250 return;
3251 }
3252
3253 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
3254 _mesa_error(ctx, GL_INVALID_VALUE,
3255 "%s(offset misaligned %d/%d)", name, (int) offset,
3256 ATOMIC_COUNTER_SIZE);
3257 return;
3258 }
3259
3260 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
3261
3262 binding = &ctx->AtomicBufferBindings[index];
3263 if (binding->BufferObject == bufObj &&
3264 binding->Offset == offset &&
3265 binding->Size == size) {
3266 return;
3267 }
3268
3269 FLUSH_VERTICES(ctx, 0);
3270 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3271
3272 set_atomic_buffer_binding(ctx, binding, bufObj, offset, size);
3273 }
3274
3275 static inline bool
3276 bind_buffers_check_offset_and_size(struct gl_context *ctx,
3277 GLuint index,
3278 const GLintptr *offsets,
3279 const GLsizeiptr *sizes)
3280 {
3281 if (offsets[index] < 0) {
3282 /* The ARB_multi_bind spec says:
3283 *
3284 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3285 * value in <offsets> is less than zero (per binding)."
3286 */
3287 _mesa_error(ctx, GL_INVALID_VALUE,
3288 "glBindBuffersRange(offsets[%u]=%" PRId64 " < 0)",
3289 index, (int64_t) offsets[index]);
3290 return false;
3291 }
3292
3293 if (sizes[index] <= 0) {
3294 /* The ARB_multi_bind spec says:
3295 *
3296 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3297 * value in <sizes> is less than or equal to zero (per binding)."
3298 */
3299 _mesa_error(ctx, GL_INVALID_VALUE,
3300 "glBindBuffersRange(sizes[%u]=%" PRId64 " <= 0)",
3301 index, (int64_t) sizes[index]);
3302 return false;
3303 }
3304
3305 return true;
3306 }
3307
3308 static bool
3309 error_check_bind_uniform_buffers(struct gl_context *ctx,
3310 GLuint first, GLsizei count,
3311 const char *caller)
3312 {
3313 if (!ctx->Extensions.ARB_uniform_buffer_object) {
3314 _mesa_error(ctx, GL_INVALID_ENUM,
3315 "%s(target=GL_UNIFORM_BUFFER)", caller);
3316 return false;
3317 }
3318
3319 /* The ARB_multi_bind_spec says:
3320 *
3321 * "An INVALID_OPERATION error is generated if <first> + <count> is
3322 * greater than the number of target-specific indexed binding points,
3323 * as described in section 6.7.1."
3324 */
3325 if (first + count > ctx->Const.MaxUniformBufferBindings) {
3326 _mesa_error(ctx, GL_INVALID_OPERATION,
3327 "%s(first=%u + count=%d > the value of "
3328 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3329 caller, first, count,
3330 ctx->Const.MaxUniformBufferBindings);
3331 return false;
3332 }
3333
3334 return true;
3335 }
3336
3337 static bool
3338 error_check_bind_shader_storage_buffers(struct gl_context *ctx,
3339 GLuint first, GLsizei count,
3340 const char *caller)
3341 {
3342 if (!ctx->Extensions.ARB_shader_storage_buffer_object) {
3343 _mesa_error(ctx, GL_INVALID_ENUM,
3344 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller);
3345 return false;
3346 }
3347
3348 /* The ARB_multi_bind_spec says:
3349 *
3350 * "An INVALID_OPERATION error is generated if <first> + <count> is
3351 * greater than the number of target-specific indexed binding points,
3352 * as described in section 6.7.1."
3353 */
3354 if (first + count > ctx->Const.MaxShaderStorageBufferBindings) {
3355 _mesa_error(ctx, GL_INVALID_OPERATION,
3356 "%s(first=%u + count=%d > the value of "
3357 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3358 caller, first, count,
3359 ctx->Const.MaxShaderStorageBufferBindings);
3360 return false;
3361 }
3362
3363 return true;
3364 }
3365
3366 /**
3367 * Unbind all uniform buffers in the range
3368 * <first> through <first>+<count>-1
3369 */
3370 static void
3371 unbind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3372 {
3373 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
3374 GLint i;
3375
3376 for (i = 0; i < count; i++)
3377 set_ubo_binding(ctx, &ctx->UniformBufferBindings[first + i],
3378 bufObj, -1, -1, GL_TRUE);
3379 }
3380
3381 /**
3382 * Unbind all shader storage buffers in the range
3383 * <first> through <first>+<count>-1
3384 */
3385 static void
3386 unbind_shader_storage_buffers(struct gl_context *ctx, GLuint first,
3387 GLsizei count)
3388 {
3389 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
3390 GLint i;
3391
3392 for (i = 0; i < count; i++)
3393 set_ssbo_binding(ctx, &ctx->ShaderStorageBufferBindings[first + i],
3394 bufObj, -1, -1, GL_TRUE);
3395 }
3396
3397 static void
3398 bind_uniform_buffers_base(struct gl_context *ctx, GLuint first, GLsizei count,
3399 const GLuint *buffers)
3400 {
3401 GLint i;
3402
3403 if (!error_check_bind_uniform_buffers(ctx, first, count, "glBindBuffersBase"))
3404 return;
3405
3406 /* Assume that at least one binding will be changed */
3407 FLUSH_VERTICES(ctx, 0);
3408 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3409
3410 if (!buffers) {
3411 /* The ARB_multi_bind spec says:
3412 *
3413 * "If <buffers> is NULL, all bindings from <first> through
3414 * <first>+<count>-1 are reset to their unbound (zero) state."
3415 */
3416 unbind_uniform_buffers(ctx, first, count);
3417 return;
3418 }
3419
3420 /* Note that the error semantics for multi-bind commands differ from
3421 * those of other GL commands.
3422 *
3423 * The Issues section in the ARB_multi_bind spec says:
3424 *
3425 * "(11) Typically, OpenGL specifies that if an error is generated by a
3426 * command, that command has no effect. This is somewhat
3427 * unfortunate for multi-bind commands, because it would require a
3428 * first pass to scan the entire list of bound objects for errors
3429 * and then a second pass to actually perform the bindings.
3430 * Should we have different error semantics?
3431 *
3432 * RESOLVED: Yes. In this specification, when the parameters for
3433 * one of the <count> binding points are invalid, that binding point
3434 * is not updated and an error will be generated. However, other
3435 * binding points in the same command will be updated if their
3436 * parameters are valid and no other error occurs."
3437 */
3438
3439 _mesa_begin_bufferobj_lookups(ctx);
3440
3441 for (i = 0; i < count; i++) {
3442 struct gl_uniform_buffer_binding *binding =
3443 &ctx->UniformBufferBindings[first + i];
3444 struct gl_buffer_object *bufObj;
3445
3446 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3447 bufObj = binding->BufferObject;
3448 else
3449 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3450 "glBindBuffersBase");
3451
3452 if (bufObj) {
3453 if (bufObj == ctx->Shared->NullBufferObj)
3454 set_ubo_binding(ctx, binding, bufObj, -1, -1, GL_TRUE);
3455 else
3456 set_ubo_binding(ctx, binding, bufObj, 0, 0, GL_TRUE);
3457 }
3458 }
3459
3460 _mesa_end_bufferobj_lookups(ctx);
3461 }
3462
3463 static void
3464 bind_shader_storage_buffers_base(struct gl_context *ctx, GLuint first,
3465 GLsizei count, const GLuint *buffers)
3466 {
3467 GLint i;
3468
3469 if (!error_check_bind_shader_storage_buffers(ctx, first, count,
3470 "glBindBuffersBase"))
3471 return;
3472
3473 /* Assume that at least one binding will be changed */
3474 FLUSH_VERTICES(ctx, 0);
3475 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
3476
3477 if (!buffers) {
3478 /* The ARB_multi_bind spec says:
3479 *
3480 * "If <buffers> is NULL, all bindings from <first> through
3481 * <first>+<count>-1 are reset to their unbound (zero) state."
3482 */
3483 unbind_shader_storage_buffers(ctx, first, count);
3484 return;
3485 }
3486
3487 /* Note that the error semantics for multi-bind commands differ from
3488 * those of other GL commands.
3489 *
3490 * The Issues section in the ARB_multi_bind spec says:
3491 *
3492 * "(11) Typically, OpenGL specifies that if an error is generated by a
3493 * command, that command has no effect. This is somewhat
3494 * unfortunate for multi-bind commands, because it would require a
3495 * first pass to scan the entire list of bound objects for errors
3496 * and then a second pass to actually perform the bindings.
3497 * Should we have different error semantics?
3498 *
3499 * RESOLVED: Yes. In this specification, when the parameters for
3500 * one of the <count> binding points are invalid, that binding point
3501 * is not updated and an error will be generated. However, other
3502 * binding points in the same command will be updated if their
3503 * parameters are valid and no other error occurs."
3504 */
3505
3506 _mesa_begin_bufferobj_lookups(ctx);
3507
3508 for (i = 0; i < count; i++) {
3509 struct gl_shader_storage_buffer_binding *binding =
3510 &ctx->ShaderStorageBufferBindings[first + i];
3511 struct gl_buffer_object *bufObj;
3512
3513 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3514 bufObj = binding->BufferObject;
3515 else
3516 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3517 "glBindBuffersBase");
3518
3519 if (bufObj) {
3520 if (bufObj == ctx->Shared->NullBufferObj)
3521 set_ssbo_binding(ctx, binding, bufObj, -1, -1, GL_TRUE);
3522 else
3523 set_ssbo_binding(ctx, binding, bufObj, 0, 0, GL_TRUE);
3524 }
3525 }
3526
3527 _mesa_end_bufferobj_lookups(ctx);
3528 }
3529
3530 static void
3531 bind_uniform_buffers_range(struct gl_context *ctx, GLuint first, GLsizei count,
3532 const GLuint *buffers,
3533 const GLintptr *offsets, const GLsizeiptr *sizes)
3534 {
3535 GLint i;
3536
3537 if (!error_check_bind_uniform_buffers(ctx, first, count,
3538 "glBindBuffersRange"))
3539 return;
3540
3541 /* Assume that at least one binding will be changed */
3542 FLUSH_VERTICES(ctx, 0);
3543 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3544
3545 if (!buffers) {
3546 /* The ARB_multi_bind spec says:
3547 *
3548 * "If <buffers> is NULL, all bindings from <first> through
3549 * <first>+<count>-1 are reset to their unbound (zero) state.
3550 * In this case, the offsets and sizes associated with the
3551 * binding points are set to default values, ignoring
3552 * <offsets> and <sizes>."
3553 */
3554 unbind_uniform_buffers(ctx, first, count);
3555 return;
3556 }
3557
3558 /* Note that the error semantics for multi-bind commands differ from
3559 * those of other GL commands.
3560 *
3561 * The Issues section in the ARB_multi_bind spec says:
3562 *
3563 * "(11) Typically, OpenGL specifies that if an error is generated by a
3564 * command, that command has no effect. This is somewhat
3565 * unfortunate for multi-bind commands, because it would require a
3566 * first pass to scan the entire list of bound objects for errors
3567 * and then a second pass to actually perform the bindings.
3568 * Should we have different error semantics?
3569 *
3570 * RESOLVED: Yes. In this specification, when the parameters for
3571 * one of the <count> binding points are invalid, that binding point
3572 * is not updated and an error will be generated. However, other
3573 * binding points in the same command will be updated if their
3574 * parameters are valid and no other error occurs."
3575 */
3576
3577 _mesa_begin_bufferobj_lookups(ctx);
3578
3579 for (i = 0; i < count; i++) {
3580 struct gl_uniform_buffer_binding *binding =
3581 &ctx->UniformBufferBindings[first + i];
3582 struct gl_buffer_object *bufObj;
3583
3584 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3585 continue;
3586
3587 /* The ARB_multi_bind spec says:
3588 *
3589 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3590 * pair of values in <offsets> and <sizes> does not respectively
3591 * satisfy the constraints described for those parameters for the
3592 * specified target, as described in section 6.7.1 (per binding)."
3593 *
3594 * Section 6.7.1 refers to table 6.5, which says:
3595 *
3596 * "┌───────────────────────────────────────────────────────────────┐
3597 * │ Uniform buffer array bindings (see sec. 7.6) │
3598 * ├─────────────────────┬─────────────────────────────────────────┤
3599 * │ ... │ ... │
3600 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3601 * │ │ OFFSET_ALIGNMENT │
3602 * │ ... │ ... │
3603 * │ size restriction │ none │
3604 * └─────────────────────┴─────────────────────────────────────────┘"
3605 */
3606 if (offsets[i] & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3607 _mesa_error(ctx, GL_INVALID_VALUE,
3608 "glBindBuffersRange(offsets[%u]=%" PRId64
3609 " is misaligned; it must be a multiple of the value of "
3610 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3611 "target=GL_UNIFORM_BUFFER)",
3612 i, (int64_t) offsets[i],
3613 ctx->Const.UniformBufferOffsetAlignment);
3614 continue;
3615 }
3616
3617 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3618 bufObj = binding->BufferObject;
3619 else
3620 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3621 "glBindBuffersRange");
3622
3623 if (bufObj) {
3624 if (bufObj == ctx->Shared->NullBufferObj)
3625 set_ubo_binding(ctx, binding, bufObj, -1, -1, GL_FALSE);
3626 else
3627 set_ubo_binding(ctx, binding, bufObj,
3628 offsets[i], sizes[i], GL_FALSE);
3629 }
3630 }
3631
3632 _mesa_end_bufferobj_lookups(ctx);
3633 }
3634
3635 static void
3636 bind_shader_storage_buffers_range(struct gl_context *ctx, GLuint first,
3637 GLsizei count, const GLuint *buffers,
3638 const GLintptr *offsets,
3639 const GLsizeiptr *sizes)
3640 {
3641 GLint i;
3642
3643 if (!error_check_bind_shader_storage_buffers(ctx, first, count,
3644 "glBindBuffersRange"))
3645 return;
3646
3647 /* Assume that at least one binding will be changed */
3648 FLUSH_VERTICES(ctx, 0);
3649 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
3650
3651 if (!buffers) {
3652 /* The ARB_multi_bind spec says:
3653 *
3654 * "If <buffers> is NULL, all bindings from <first> through
3655 * <first>+<count>-1 are reset to their unbound (zero) state.
3656 * In this case, the offsets and sizes associated with the
3657 * binding points are set to default values, ignoring
3658 * <offsets> and <sizes>."
3659 */
3660 unbind_shader_storage_buffers(ctx, first, count);
3661 return;
3662 }
3663
3664 /* Note that the error semantics for multi-bind commands differ from
3665 * those of other GL commands.
3666 *
3667 * The Issues section in the ARB_multi_bind spec says:
3668 *
3669 * "(11) Typically, OpenGL specifies that if an error is generated by a
3670 * command, that command has no effect. This is somewhat
3671 * unfortunate for multi-bind commands, because it would require a
3672 * first pass to scan the entire list of bound objects for errors
3673 * and then a second pass to actually perform the bindings.
3674 * Should we have different error semantics?
3675 *
3676 * RESOLVED: Yes. In this specification, when the parameters for
3677 * one of the <count> binding points are invalid, that binding point
3678 * is not updated and an error will be generated. However, other
3679 * binding points in the same command will be updated if their
3680 * parameters are valid and no other error occurs."
3681 */
3682
3683 _mesa_begin_bufferobj_lookups(ctx);
3684
3685 for (i = 0; i < count; i++) {
3686 struct gl_shader_storage_buffer_binding *binding =
3687 &ctx->ShaderStorageBufferBindings[first + i];
3688 struct gl_buffer_object *bufObj;
3689
3690 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3691 continue;
3692
3693 /* The ARB_multi_bind spec says:
3694 *
3695 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3696 * pair of values in <offsets> and <sizes> does not respectively
3697 * satisfy the constraints described for those parameters for the
3698 * specified target, as described in section 6.7.1 (per binding)."
3699 *
3700 * Section 6.7.1 refers to table 6.5, which says:
3701 *
3702 * "┌───────────────────────────────────────────────────────────────┐
3703 * │ Shader storage buffer array bindings (see sec. 7.8) │
3704 * ├─────────────────────┬─────────────────────────────────────────┤
3705 * │ ... │ ... │
3706 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3707 * │ │ BUFFER_OFFSET_ALIGNMENT │
3708 * │ ... │ ... │
3709 * │ size restriction │ none │
3710 * └─────────────────────┴─────────────────────────────────────────┘"
3711 */
3712 if (offsets[i] & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
3713 _mesa_error(ctx, GL_INVALID_VALUE,
3714 "glBindBuffersRange(offsets[%u]=%" PRId64
3715 " is misaligned; it must be a multiple of the value of "
3716 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3717 "target=GL_SHADER_STORAGE_BUFFER)",
3718 i, (int64_t) offsets[i],
3719 ctx->Const.ShaderStorageBufferOffsetAlignment);
3720 continue;
3721 }
3722
3723 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3724 bufObj = binding->BufferObject;
3725 else
3726 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3727 "glBindBuffersRange");
3728
3729 if (bufObj) {
3730 if (bufObj == ctx->Shared->NullBufferObj)
3731 set_ssbo_binding(ctx, binding, bufObj, -1, -1, GL_FALSE);
3732 else
3733 set_ssbo_binding(ctx, binding, bufObj,
3734 offsets[i], sizes[i], GL_FALSE);
3735 }
3736 }
3737
3738 _mesa_end_bufferobj_lookups(ctx);
3739 }
3740
3741 static bool
3742 error_check_bind_xfb_buffers(struct gl_context *ctx,
3743 struct gl_transform_feedback_object *tfObj,
3744 GLuint first, GLsizei count, const char *caller)
3745 {
3746 if (!ctx->Extensions.EXT_transform_feedback) {
3747 _mesa_error(ctx, GL_INVALID_ENUM,
3748 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller);
3749 return false;
3750 }
3751
3752 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3753 *
3754 * "An INVALID_OPERATION error is generated :
3755 *
3756 * ...
3757 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3758 * FEEDBACK_BUFFER and transform feedback is currently active."
3759 *
3760 * We assume that this is also meant to apply to BindBuffersRange
3761 * and BindBuffersBase.
3762 */
3763 if (tfObj->Active) {
3764 _mesa_error(ctx, GL_INVALID_OPERATION,
3765 "%s(Changing transform feedback buffers while "
3766 "transform feedback is active)", caller);
3767 return false;
3768 }
3769
3770 /* The ARB_multi_bind_spec says:
3771 *
3772 * "An INVALID_OPERATION error is generated if <first> + <count> is
3773 * greater than the number of target-specific indexed binding points,
3774 * as described in section 6.7.1."
3775 */
3776 if (first + count > ctx->Const.MaxTransformFeedbackBuffers) {
3777 _mesa_error(ctx, GL_INVALID_OPERATION,
3778 "%s(first=%u + count=%d > the value of "
3779 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3780 caller, first, count,
3781 ctx->Const.MaxTransformFeedbackBuffers);
3782 return false;
3783 }
3784
3785 return true;
3786 }
3787
3788 /**
3789 * Unbind all transform feedback buffers in the range
3790 * <first> through <first>+<count>-1
3791 */
3792 static void
3793 unbind_xfb_buffers(struct gl_context *ctx,
3794 struct gl_transform_feedback_object *tfObj,
3795 GLuint first, GLsizei count)
3796 {
3797 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3798 GLint i;
3799
3800 for (i = 0; i < count; i++)
3801 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
3802 bufObj, 0, 0);
3803 }
3804
3805 static void
3806 bind_xfb_buffers_base(struct gl_context *ctx,
3807 GLuint first, GLsizei count,
3808 const GLuint *buffers)
3809 {
3810 struct gl_transform_feedback_object *tfObj =
3811 ctx->TransformFeedback.CurrentObject;
3812 GLint i;
3813
3814 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count,
3815 "glBindBuffersBase"))
3816 return;
3817
3818 /* Assume that at least one binding will be changed */
3819 FLUSH_VERTICES(ctx, 0);
3820 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
3821
3822 if (!buffers) {
3823 /* The ARB_multi_bind spec says:
3824 *
3825 * "If <buffers> is NULL, all bindings from <first> through
3826 * <first>+<count>-1 are reset to their unbound (zero) state."
3827 */
3828 unbind_xfb_buffers(ctx, tfObj, first, count);
3829 return;
3830 }
3831
3832 /* Note that the error semantics for multi-bind commands differ from
3833 * those of other GL commands.
3834 *
3835 * The Issues section in the ARB_multi_bind spec says:
3836 *
3837 * "(11) Typically, OpenGL specifies that if an error is generated by a
3838 * command, that command has no effect. This is somewhat
3839 * unfortunate for multi-bind commands, because it would require a
3840 * first pass to scan the entire list of bound objects for errors
3841 * and then a second pass to actually perform the bindings.
3842 * Should we have different error semantics?
3843 *
3844 * RESOLVED: Yes. In this specification, when the parameters for
3845 * one of the <count> binding points are invalid, that binding point
3846 * is not updated and an error will be generated. However, other
3847 * binding points in the same command will be updated if their
3848 * parameters are valid and no other error occurs."
3849 */
3850
3851 _mesa_begin_bufferobj_lookups(ctx);
3852
3853 for (i = 0; i < count; i++) {
3854 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[first + i];
3855 struct gl_buffer_object *bufObj;
3856
3857 if (boundBufObj && boundBufObj->Name == buffers[i])
3858 bufObj = boundBufObj;
3859 else
3860 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3861 "glBindBuffersBase");
3862
3863 if (bufObj)
3864 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
3865 bufObj, 0, 0);
3866 }
3867
3868 _mesa_end_bufferobj_lookups(ctx);
3869 }
3870
3871 static void
3872 bind_xfb_buffers_range(struct gl_context *ctx,
3873 GLuint first, GLsizei count,
3874 const GLuint *buffers,
3875 const GLintptr *offsets,
3876 const GLsizeiptr *sizes)
3877 {
3878 struct gl_transform_feedback_object *tfObj =
3879 ctx->TransformFeedback.CurrentObject;
3880 GLint i;
3881
3882 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count,
3883 "glBindBuffersRange"))
3884 return;
3885
3886 /* Assume that at least one binding will be changed */
3887 FLUSH_VERTICES(ctx, 0);
3888 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
3889
3890 if (!buffers) {
3891 /* The ARB_multi_bind spec says:
3892 *
3893 * "If <buffers> is NULL, all bindings from <first> through
3894 * <first>+<count>-1 are reset to their unbound (zero) state.
3895 * In this case, the offsets and sizes associated with the
3896 * binding points are set to default values, ignoring
3897 * <offsets> and <sizes>."
3898 */
3899 unbind_xfb_buffers(ctx, tfObj, first, count);
3900 return;
3901 }
3902
3903 /* Note that the error semantics for multi-bind commands differ from
3904 * those of other GL commands.
3905 *
3906 * The Issues section in the ARB_multi_bind spec says:
3907 *
3908 * "(11) Typically, OpenGL specifies that if an error is generated by a
3909 * command, that command has no effect. This is somewhat
3910 * unfortunate for multi-bind commands, because it would require a
3911 * first pass to scan the entire list of bound objects for errors
3912 * and then a second pass to actually perform the bindings.
3913 * Should we have different error semantics?
3914 *
3915 * RESOLVED: Yes. In this specification, when the parameters for
3916 * one of the <count> binding points are invalid, that binding point
3917 * is not updated and an error will be generated. However, other
3918 * binding points in the same command will be updated if their
3919 * parameters are valid and no other error occurs."
3920 */
3921
3922 _mesa_begin_bufferobj_lookups(ctx);
3923
3924 for (i = 0; i < count; i++) {
3925 const GLuint index = first + i;
3926 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[index];
3927 struct gl_buffer_object *bufObj;
3928
3929 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3930 continue;
3931
3932 /* The ARB_multi_bind spec says:
3933 *
3934 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3935 * pair of values in <offsets> and <sizes> does not respectively
3936 * satisfy the constraints described for those parameters for the
3937 * specified target, as described in section 6.7.1 (per binding)."
3938 *
3939 * Section 6.7.1 refers to table 6.5, which says:
3940 *
3941 * "┌───────────────────────────────────────────────────────────────┐
3942 * │ Transform feedback array bindings (see sec. 13.2.2) │
3943 * ├───────────────────────┬───────────────────────────────────────┤
3944 * │ ... │ ... │
3945 * │ offset restriction │ multiple of 4 │
3946 * │ ... │ ... │
3947 * │ size restriction │ multiple of 4 │
3948 * └───────────────────────┴───────────────────────────────────────┘"
3949 */
3950 if (offsets[i] & 0x3) {
3951 _mesa_error(ctx, GL_INVALID_VALUE,
3952 "glBindBuffersRange(offsets[%u]=%" PRId64
3953 " is misaligned; it must be a multiple of 4 when "
3954 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3955 i, (int64_t) offsets[i]);
3956 continue;
3957 }
3958
3959 if (sizes[i] & 0x3) {
3960 _mesa_error(ctx, GL_INVALID_VALUE,
3961 "glBindBuffersRange(sizes[%u]=%" PRId64
3962 " is misaligned; it must be a multiple of 4 when "
3963 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3964 i, (int64_t) sizes[i]);
3965 continue;
3966 }
3967
3968 if (boundBufObj && boundBufObj->Name == buffers[i])
3969 bufObj = boundBufObj;
3970 else
3971 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3972 "glBindBuffersRange");
3973
3974 if (bufObj)
3975 _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj,
3976 offsets[i], sizes[i]);
3977 }
3978
3979 _mesa_end_bufferobj_lookups(ctx);
3980 }
3981
3982 static bool
3983 error_check_bind_atomic_buffers(struct gl_context *ctx,
3984 GLuint first, GLsizei count,
3985 const char *caller)
3986 {
3987 if (!ctx->Extensions.ARB_shader_atomic_counters) {
3988 _mesa_error(ctx, GL_INVALID_ENUM,
3989 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller);
3990 return false;
3991 }
3992
3993 /* The ARB_multi_bind_spec says:
3994 *
3995 * "An INVALID_OPERATION error is generated if <first> + <count> is
3996 * greater than the number of target-specific indexed binding points,
3997 * as described in section 6.7.1."
3998 */
3999 if (first + count > ctx->Const.MaxAtomicBufferBindings) {
4000 _mesa_error(ctx, GL_INVALID_OPERATION,
4001 "%s(first=%u + count=%d > the value of "
4002 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4003 caller, first, count, ctx->Const.MaxAtomicBufferBindings);
4004 return false;
4005 }
4006
4007 return true;
4008 }
4009
4010 /**
4011 * Unbind all atomic counter buffers in the range
4012 * <first> through <first>+<count>-1
4013 */
4014 static void
4015 unbind_atomic_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
4016 {
4017 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
4018 GLint i;
4019
4020 for (i = 0; i < count; i++)
4021 set_atomic_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i],
4022 bufObj, -1, -1);
4023 }
4024
4025 static void
4026 bind_atomic_buffers_base(struct gl_context *ctx,
4027 GLuint first,
4028 GLsizei count,
4029 const GLuint *buffers)
4030 {
4031 GLint i;
4032
4033 if (!error_check_bind_atomic_buffers(ctx, first, count,
4034 "glBindBuffersBase"))
4035 return;
4036
4037 /* Assume that at least one binding will be changed */
4038 FLUSH_VERTICES(ctx, 0);
4039 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
4040
4041 if (!buffers) {
4042 /* The ARB_multi_bind spec says:
4043 *
4044 * "If <buffers> is NULL, all bindings from <first> through
4045 * <first>+<count>-1 are reset to their unbound (zero) state."
4046 */
4047 unbind_atomic_buffers(ctx, first, count);
4048 return;
4049 }
4050
4051 /* Note that the error semantics for multi-bind commands differ from
4052 * those of other GL commands.
4053 *
4054 * The Issues section in the ARB_multi_bind spec says:
4055 *
4056 * "(11) Typically, OpenGL specifies that if an error is generated by a
4057 * command, that command has no effect. This is somewhat
4058 * unfortunate for multi-bind commands, because it would require a
4059 * first pass to scan the entire list of bound objects for errors
4060 * and then a second pass to actually perform the bindings.
4061 * Should we have different error semantics?
4062 *
4063 * RESOLVED: Yes. In this specification, when the parameters for
4064 * one of the <count> binding points are invalid, that binding point
4065 * is not updated and an error will be generated. However, other
4066 * binding points in the same command will be updated if their
4067 * parameters are valid and no other error occurs."
4068 */
4069
4070 _mesa_begin_bufferobj_lookups(ctx);
4071
4072 for (i = 0; i < count; i++) {
4073 struct gl_atomic_buffer_binding *binding =
4074 &ctx->AtomicBufferBindings[first + i];
4075 struct gl_buffer_object *bufObj;
4076
4077 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
4078 bufObj = binding->BufferObject;
4079 else
4080 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
4081 "glBindBuffersBase");
4082
4083 if (bufObj)
4084 set_atomic_buffer_binding(ctx, binding, bufObj, 0, 0);
4085 }
4086
4087 _mesa_end_bufferobj_lookups(ctx);
4088 }
4089
4090 static void
4091 bind_atomic_buffers_range(struct gl_context *ctx,
4092 GLuint first,
4093 GLsizei count,
4094 const GLuint *buffers,
4095 const GLintptr *offsets,
4096 const GLsizeiptr *sizes)
4097 {
4098 GLint i;
4099
4100 if (!error_check_bind_atomic_buffers(ctx, first, count,
4101 "glBindBuffersRange"))
4102 return;
4103
4104 /* Assume that at least one binding will be changed */
4105 FLUSH_VERTICES(ctx, 0);
4106 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
4107
4108 if (!buffers) {
4109 /* The ARB_multi_bind spec says:
4110 *
4111 * "If <buffers> is NULL, all bindings from <first> through
4112 * <first>+<count>-1 are reset to their unbound (zero) state.
4113 * In this case, the offsets and sizes associated with the
4114 * binding points are set to default values, ignoring
4115 * <offsets> and <sizes>."
4116 */
4117 unbind_atomic_buffers(ctx, first, count);
4118 return;
4119 }
4120
4121 /* Note that the error semantics for multi-bind commands differ from
4122 * those of other GL commands.
4123 *
4124 * The Issues section in the ARB_multi_bind spec says:
4125 *
4126 * "(11) Typically, OpenGL specifies that if an error is generated by a
4127 * command, that command has no effect. This is somewhat
4128 * unfortunate for multi-bind commands, because it would require a
4129 * first pass to scan the entire list of bound objects for errors
4130 * and then a second pass to actually perform the bindings.
4131 * Should we have different error semantics?
4132 *
4133 * RESOLVED: Yes. In this specification, when the parameters for
4134 * one of the <count> binding points are invalid, that binding point
4135 * is not updated and an error will be generated. However, other
4136 * binding points in the same command will be updated if their
4137 * parameters are valid and no other error occurs."
4138 */
4139
4140 _mesa_begin_bufferobj_lookups(ctx);
4141
4142 for (i = 0; i < count; i++) {
4143 struct gl_atomic_buffer_binding *binding =
4144 &ctx->AtomicBufferBindings[first + i];
4145 struct gl_buffer_object *bufObj;
4146
4147 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
4148 continue;
4149
4150 /* The ARB_multi_bind spec says:
4151 *
4152 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4153 * pair of values in <offsets> and <sizes> does not respectively
4154 * satisfy the constraints described for those parameters for the
4155 * specified target, as described in section 6.7.1 (per binding)."
4156 *
4157 * Section 6.7.1 refers to table 6.5, which says:
4158 *
4159 * "┌───────────────────────────────────────────────────────────────┐
4160 * │ Atomic counter array bindings (see sec. 7.7.2) │
4161 * ├───────────────────────┬───────────────────────────────────────┤
4162 * │ ... │ ... │
4163 * │ offset restriction │ multiple of 4 │
4164 * │ ... │ ... │
4165 * │ size restriction │ none │
4166 * └───────────────────────┴───────────────────────────────────────┘"
4167 */
4168 if (offsets[i] & (ATOMIC_COUNTER_SIZE - 1)) {
4169 _mesa_error(ctx, GL_INVALID_VALUE,
4170 "glBindBuffersRange(offsets[%u]=%" PRId64
4171 " is misaligned; it must be a multiple of %d when "
4172 "target=GL_ATOMIC_COUNTER_BUFFER)",
4173 i, (int64_t) offsets[i], ATOMIC_COUNTER_SIZE);
4174 continue;
4175 }
4176
4177 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
4178 bufObj = binding->BufferObject;
4179 else
4180 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
4181 "glBindBuffersRange");
4182
4183 if (bufObj)
4184 set_atomic_buffer_binding(ctx, binding, bufObj, offsets[i], sizes[i]);
4185 }
4186
4187 _mesa_end_bufferobj_lookups(ctx);
4188 }
4189
4190 void GLAPIENTRY
4191 _mesa_BindBufferRange(GLenum target, GLuint index,
4192 GLuint buffer, GLintptr offset, GLsizeiptr size)
4193 {
4194 GET_CURRENT_CONTEXT(ctx);
4195 struct gl_buffer_object *bufObj;
4196
4197 if (buffer == 0) {
4198 bufObj = ctx->Shared->NullBufferObj;
4199 } else {
4200 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4201 }
4202 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
4203 &bufObj, "glBindBufferRange"))
4204 return;
4205
4206 if (!bufObj) {
4207 _mesa_error(ctx, GL_INVALID_OPERATION,
4208 "glBindBufferRange(invalid buffer=%u)", buffer);
4209 return;
4210 }
4211
4212 if (buffer != 0) {
4213 if (size <= 0) {
4214 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
4215 (int) size);
4216 return;
4217 }
4218 }
4219
4220 switch (target) {
4221 case GL_TRANSFORM_FEEDBACK_BUFFER:
4222 _mesa_bind_buffer_range_transform_feedback(ctx,
4223 ctx->TransformFeedback.CurrentObject,
4224 index, bufObj, offset, size,
4225 false);
4226 return;
4227 case GL_UNIFORM_BUFFER:
4228 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
4229 return;
4230 case GL_ATOMIC_COUNTER_BUFFER:
4231 bind_atomic_buffer(ctx, index, bufObj, offset, size,
4232 "glBindBufferRange");
4233 return;
4234 default:
4235 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
4236 return;
4237 }
4238 }
4239
4240 void GLAPIENTRY
4241 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
4242 {
4243 GET_CURRENT_CONTEXT(ctx);
4244 struct gl_buffer_object *bufObj;
4245
4246 if (buffer == 0) {
4247 bufObj = ctx->Shared->NullBufferObj;
4248 } else {
4249 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4250 }
4251 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
4252 &bufObj, "glBindBufferBase"))
4253 return;
4254
4255 if (!bufObj) {
4256 _mesa_error(ctx, GL_INVALID_OPERATION,
4257 "glBindBufferBase(invalid buffer=%u)", buffer);
4258 return;
4259 }
4260
4261 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4262 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4263 *
4264 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4265 * zero and size equal to the size of buffer."
4266 *
4267 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4268 *
4269 * "If the parameter (starting offset or size) was not specified when the
4270 * buffer object was bound, zero is returned."
4271 *
4272 * What happens if the size of the buffer changes? Does the size of the
4273 * buffer at the moment glBindBufferBase was called still play a role, like
4274 * the first quote would imply, or is the size meaningless in the
4275 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4276 * core spec page 45 says:
4277 *
4278 * "It is equivalent to calling BindBufferRange with offset zero, while
4279 * size is determined by the size of the bound buffer at the time the
4280 * binding is used."
4281 *
4282 * My interpretation is that the GL 4.1 spec was a clarification of the
4283 * behavior, not a change. In particular, this choice will only make
4284 * rendering work in cases where it would have had undefined results.
4285 */
4286
4287 switch (target) {
4288 case GL_TRANSFORM_FEEDBACK_BUFFER:
4289 _mesa_bind_buffer_base_transform_feedback(ctx,
4290 ctx->TransformFeedback.CurrentObject,
4291 index, bufObj, false);
4292 return;
4293 case GL_UNIFORM_BUFFER:
4294 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
4295 return;
4296 case GL_SHADER_STORAGE_BUFFER:
4297 bind_buffer_base_shader_storage_buffer(ctx, index, bufObj);
4298 return;
4299 case GL_ATOMIC_COUNTER_BUFFER:
4300 bind_atomic_buffer(ctx, index, bufObj, 0, 0,
4301 "glBindBufferBase");
4302 return;
4303 default:
4304 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
4305 return;
4306 }
4307 }
4308
4309 void GLAPIENTRY
4310 _mesa_BindBuffersRange(GLenum target, GLuint first, GLsizei count,
4311 const GLuint *buffers,
4312 const GLintptr *offsets, const GLsizeiptr *sizes)
4313 {
4314 GET_CURRENT_CONTEXT(ctx);
4315
4316 switch (target) {
4317 case GL_TRANSFORM_FEEDBACK_BUFFER:
4318 bind_xfb_buffers_range(ctx, first, count, buffers, offsets, sizes);
4319 return;
4320 case GL_UNIFORM_BUFFER:
4321 bind_uniform_buffers_range(ctx, first, count, buffers, offsets, sizes);
4322 return;
4323 case GL_SHADER_STORAGE_BUFFER:
4324 bind_shader_storage_buffers_range(ctx, first, count, buffers, offsets,
4325 sizes);
4326 return;
4327 case GL_ATOMIC_COUNTER_BUFFER:
4328 bind_atomic_buffers_range(ctx, first, count, buffers,
4329 offsets, sizes);
4330 return;
4331 default:
4332 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersRange(target=%s)",
4333 _mesa_lookup_enum_by_nr(target));
4334 break;
4335 }
4336 }
4337
4338 void GLAPIENTRY
4339 _mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count,
4340 const GLuint *buffers)
4341 {
4342 GET_CURRENT_CONTEXT(ctx);
4343
4344 switch (target) {
4345 case GL_TRANSFORM_FEEDBACK_BUFFER:
4346 bind_xfb_buffers_base(ctx, first, count, buffers);
4347 return;
4348 case GL_UNIFORM_BUFFER:
4349 bind_uniform_buffers_base(ctx, first, count, buffers);
4350 return;
4351 case GL_SHADER_STORAGE_BUFFER:
4352 bind_shader_storage_buffers_base(ctx, first, count, buffers);
4353 return;
4354 case GL_ATOMIC_COUNTER_BUFFER:
4355 bind_atomic_buffers_base(ctx, first, count, buffers);
4356 return;
4357 default:
4358 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersBase(target=%s)",
4359 _mesa_lookup_enum_by_nr(target));
4360 break;
4361 }
4362 }
4363
4364 void GLAPIENTRY
4365 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
4366 GLsizeiptr length)
4367 {
4368 GET_CURRENT_CONTEXT(ctx);
4369 struct gl_buffer_object *bufObj;
4370 const GLintptr end = offset + length;
4371
4372 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4373 if (!bufObj) {
4374 _mesa_error(ctx, GL_INVALID_VALUE,
4375 "glInvalidateBufferSubData(name = 0x%x) invalid object",
4376 buffer);
4377 return;
4378 }
4379
4380 /* The GL_ARB_invalidate_subdata spec says:
4381 *
4382 * "An INVALID_VALUE error is generated if <offset> or <length> is
4383 * negative, or if <offset> + <length> is greater than the value of
4384 * BUFFER_SIZE."
4385 */
4386 if (end < 0 || end > bufObj->Size) {
4387 _mesa_error(ctx, GL_INVALID_VALUE,
4388 "glInvalidateBufferSubData(invalid offset or length)");
4389 return;
4390 }
4391
4392 /* The OpenGL 4.4 (Core Profile) spec says:
4393 *
4394 * "An INVALID_OPERATION error is generated if buffer is currently
4395 * mapped by MapBuffer or if the invalidate range intersects the range
4396 * currently mapped by MapBufferRange, unless it was mapped
4397 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4398 */
4399 if (!(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) &&
4400 bufferobj_range_mapped(bufObj, offset, length)) {
4401 _mesa_error(ctx, GL_INVALID_OPERATION,
4402 "glInvalidateBufferSubData(intersection with mapped "
4403 "range)");
4404 return;
4405 }
4406
4407 /* We don't actually do anything for this yet. Just return after
4408 * validating the parameters and generating the required errors.
4409 */
4410 return;
4411 }
4412
4413 void GLAPIENTRY
4414 _mesa_InvalidateBufferData(GLuint buffer)
4415 {
4416 GET_CURRENT_CONTEXT(ctx);
4417 struct gl_buffer_object *bufObj;
4418
4419 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4420 if (!bufObj) {
4421 _mesa_error(ctx, GL_INVALID_VALUE,
4422 "glInvalidateBufferData(name = 0x%x) invalid object",
4423 buffer);
4424 return;
4425 }
4426
4427 /* The OpenGL 4.4 (Core Profile) spec says:
4428 *
4429 * "An INVALID_OPERATION error is generated if buffer is currently
4430 * mapped by MapBuffer or if the invalidate range intersects the range
4431 * currently mapped by MapBufferRange, unless it was mapped
4432 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4433 */
4434 if (_mesa_check_disallowed_mapping(bufObj)) {
4435 _mesa_error(ctx, GL_INVALID_OPERATION,
4436 "glInvalidateBufferData(intersection with mapped "
4437 "range)");
4438 return;
4439 }
4440
4441 /* We don't actually do anything for this yet. Just return after
4442 * validating the parameters and generating the required errors.
4443 */
4444 return;
4445 }