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