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