mesa: add KHR_no_error support for glInvalidateBufferSubData()
[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 bool
1506 validate_buffer_storage(struct gl_context *ctx,
1507 struct gl_buffer_object *bufObj, GLsizeiptr size,
1508 GLbitfield flags, const char *func)
1509 {
1510 if (size <= 0) {
1511 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size <= 0)", func);
1512 return false;
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 false;
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 false;
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 false;
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 false;
1553 }
1554
1555 if (bufObj->Immutable) {
1556 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
1557 return false;
1558 }
1559
1560 return true;
1561 }
1562
1563
1564 static void
1565 buffer_storage(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1566 GLenum target, GLsizeiptr size, const GLvoid *data,
1567 GLbitfield flags, const char *func)
1568 {
1569 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1570 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1571
1572 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1573
1574 bufObj->Written = GL_TRUE;
1575 bufObj->Immutable = GL_TRUE;
1576 bufObj->MinMaxCacheDirty = true;
1577
1578 assert(ctx->Driver.BufferData);
1579 if (!ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
1580 flags, bufObj)) {
1581 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
1582 /* Even though the interaction between AMD_pinned_memory and
1583 * glBufferStorage is not described in the spec, Graham Sellers
1584 * said that it should behave the same as glBufferData.
1585 */
1586 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1587 }
1588 else {
1589 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1590 }
1591 }
1592 }
1593
1594
1595 static ALWAYS_INLINE void
1596 inlined_buffer_storage(GLenum target, GLuint buffer, GLsizeiptr size,
1597 const GLvoid *data, GLbitfield flags, bool dsa,
1598 bool no_error, const char *func)
1599 {
1600 GET_CURRENT_CONTEXT(ctx);
1601 struct gl_buffer_object *bufObj;
1602
1603 if (dsa) {
1604 if (no_error) {
1605 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
1606 } else {
1607 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, func);
1608 if (!bufObj)
1609 return;
1610 }
1611 } else {
1612 if (no_error) {
1613 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
1614 bufObj = *bufObjPtr;
1615 } else {
1616 bufObj = get_buffer(ctx, func, target, GL_INVALID_OPERATION);
1617 if (!bufObj)
1618 return;
1619 }
1620 }
1621
1622 if (no_error || validate_buffer_storage(ctx, bufObj, size, flags, func))
1623 buffer_storage(ctx, bufObj, target, size, data, flags, func);
1624 }
1625
1626
1627 void GLAPIENTRY
1628 _mesa_BufferStorage_no_error(GLenum target, GLsizeiptr size,
1629 const GLvoid *data, GLbitfield flags)
1630 {
1631 inlined_buffer_storage(target, 0, size, data, flags, false, true,
1632 "glBufferStorage");
1633 }
1634
1635
1636 void GLAPIENTRY
1637 _mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data,
1638 GLbitfield flags)
1639 {
1640 inlined_buffer_storage(target, 0, size, data, flags, false, false,
1641 "glBufferStorage");
1642 }
1643
1644
1645 void GLAPIENTRY
1646 _mesa_NamedBufferStorage_no_error(GLuint buffer, GLsizeiptr size,
1647 const GLvoid *data, GLbitfield flags)
1648 {
1649 /* In direct state access, buffer objects have an unspecified target
1650 * since they are not required to be bound.
1651 */
1652 inlined_buffer_storage(GL_NONE, buffer, size, data, flags, true, true,
1653 "glNamedBufferStorage");
1654 }
1655
1656
1657 void GLAPIENTRY
1658 _mesa_NamedBufferStorage(GLuint buffer, GLsizeiptr size, const GLvoid *data,
1659 GLbitfield flags)
1660 {
1661 /* In direct state access, buffer objects have an unspecified target
1662 * since they are not required to be bound.
1663 */
1664 inlined_buffer_storage(GL_NONE, buffer, size, data, flags, true, false,
1665 "glNamedBufferStorage");
1666 }
1667
1668
1669 void
1670 _mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1671 GLenum target, GLsizeiptr size, const GLvoid *data,
1672 GLenum usage, const char *func)
1673 {
1674 bool valid_usage;
1675
1676 if (MESA_VERBOSE & VERBOSE_API) {
1677 _mesa_debug(ctx, "%s(%s, %ld, %p, %s)\n",
1678 func,
1679 _mesa_enum_to_string(target),
1680 (long int) size, data,
1681 _mesa_enum_to_string(usage));
1682 }
1683
1684 if (size < 0) {
1685 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", func);
1686 return;
1687 }
1688
1689 switch (usage) {
1690 case GL_STREAM_DRAW_ARB:
1691 valid_usage = (ctx->API != API_OPENGLES);
1692 break;
1693
1694 case GL_STATIC_DRAW_ARB:
1695 case GL_DYNAMIC_DRAW_ARB:
1696 valid_usage = true;
1697 break;
1698
1699 case GL_STREAM_READ_ARB:
1700 case GL_STREAM_COPY_ARB:
1701 case GL_STATIC_READ_ARB:
1702 case GL_STATIC_COPY_ARB:
1703 case GL_DYNAMIC_READ_ARB:
1704 case GL_DYNAMIC_COPY_ARB:
1705 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1706 break;
1707
1708 default:
1709 valid_usage = false;
1710 break;
1711 }
1712
1713 if (!valid_usage) {
1714 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid usage: %s)", func,
1715 _mesa_enum_to_string(usage));
1716 return;
1717 }
1718
1719 if (bufObj->Immutable) {
1720 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
1721 return;
1722 }
1723
1724 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1725 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1726
1727 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1728
1729 bufObj->Written = GL_TRUE;
1730 bufObj->MinMaxCacheDirty = true;
1731
1732 #ifdef VBO_DEBUG
1733 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1734 bufObj->Name, size, data, usage);
1735 #endif
1736
1737 #ifdef BOUNDS_CHECK
1738 size += 100;
1739 #endif
1740
1741 assert(ctx->Driver.BufferData);
1742 if (!ctx->Driver.BufferData(ctx, target, size, data, usage,
1743 GL_MAP_READ_BIT |
1744 GL_MAP_WRITE_BIT |
1745 GL_DYNAMIC_STORAGE_BIT,
1746 bufObj)) {
1747 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
1748 /* From GL_AMD_pinned_memory:
1749 *
1750 * INVALID_OPERATION is generated by BufferData if <target> is
1751 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1752 * mapped to the GPU address space.
1753 */
1754 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1755 }
1756 else {
1757 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1758 }
1759 }
1760 }
1761
1762 void GLAPIENTRY
1763 _mesa_BufferData(GLenum target, GLsizeiptr size,
1764 const GLvoid *data, GLenum usage)
1765 {
1766 GET_CURRENT_CONTEXT(ctx);
1767 struct gl_buffer_object *bufObj;
1768
1769 bufObj = get_buffer(ctx, "glBufferData", target, GL_INVALID_OPERATION);
1770 if (!bufObj)
1771 return;
1772
1773 _mesa_buffer_data(ctx, bufObj, target, size, data, usage,
1774 "glBufferData");
1775 }
1776
1777 void GLAPIENTRY
1778 _mesa_NamedBufferData(GLuint buffer, GLsizeiptr size, const GLvoid *data,
1779 GLenum usage)
1780 {
1781 GET_CURRENT_CONTEXT(ctx);
1782 struct gl_buffer_object *bufObj;
1783
1784 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferData");
1785 if (!bufObj)
1786 return;
1787
1788 /* In direct state access, buffer objects have an unspecified target since
1789 * they are not required to be bound.
1790 */
1791 _mesa_buffer_data(ctx, bufObj, GL_NONE, size, data, usage,
1792 "glNamedBufferData");
1793 }
1794
1795
1796 static bool
1797 validate_buffer_sub_data(struct gl_context *ctx,
1798 struct gl_buffer_object *bufObj,
1799 GLintptr offset, GLsizeiptr size,
1800 const char *func)
1801 {
1802 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
1803 true, func)) {
1804 /* error already recorded */
1805 return false;
1806 }
1807
1808 if (bufObj->Immutable &&
1809 !(bufObj->StorageFlags & GL_DYNAMIC_STORAGE_BIT)) {
1810 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1811 return false;
1812 }
1813
1814 if ((bufObj->Usage == GL_STATIC_DRAW ||
1815 bufObj->Usage == GL_STATIC_COPY) &&
1816 bufObj->NumSubDataCalls >= BUFFER_WARNING_CALL_COUNT - 1) {
1817 /* If the application declared the buffer as static draw/copy or stream
1818 * draw, it should not be frequently modified with glBufferSubData.
1819 */
1820 BUFFER_USAGE_WARNING(ctx,
1821 "using %s(buffer %u, offset %u, size %u) to "
1822 "update a %s buffer",
1823 func, bufObj->Name, offset, size,
1824 _mesa_enum_to_string(bufObj->Usage));
1825 }
1826
1827 return true;
1828 }
1829
1830
1831 /**
1832 * Implementation for glBufferSubData and glNamedBufferSubData.
1833 *
1834 * \param ctx GL context.
1835 * \param bufObj The buffer object.
1836 * \param offset Offset of the first byte of the subdata range.
1837 * \param size Size, in bytes, of the subdata range.
1838 * \param data The data store.
1839 * \param func Name of calling function for recording errors.
1840 *
1841 */
1842 void
1843 _mesa_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1844 GLintptr offset, GLsizeiptr size, const GLvoid *data)
1845 {
1846 if (size == 0)
1847 return;
1848
1849 bufObj->NumSubDataCalls++;
1850 bufObj->Written = GL_TRUE;
1851 bufObj->MinMaxCacheDirty = true;
1852
1853 assert(ctx->Driver.BufferSubData);
1854 ctx->Driver.BufferSubData(ctx, offset, size, data, bufObj);
1855 }
1856
1857
1858 static ALWAYS_INLINE void
1859 buffer_sub_data(GLenum target, GLuint buffer, GLintptr offset,
1860 GLsizeiptr size, const GLvoid *data,
1861 bool dsa, bool no_error, const char *func)
1862 {
1863 GET_CURRENT_CONTEXT(ctx);
1864 struct gl_buffer_object *bufObj;
1865
1866 if (dsa) {
1867 if (no_error) {
1868 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
1869 } else {
1870 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, func);
1871 if (!bufObj)
1872 return;
1873 }
1874 } else {
1875 if (no_error) {
1876 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
1877 bufObj = *bufObjPtr;
1878 } else {
1879 bufObj = get_buffer(ctx, func, target, GL_INVALID_OPERATION);
1880 if (!bufObj)
1881 return;
1882 }
1883 }
1884
1885 if (no_error || validate_buffer_sub_data(ctx, bufObj, offset, size, func))
1886 _mesa_buffer_sub_data(ctx, bufObj, offset, size, data);
1887 }
1888
1889
1890 void GLAPIENTRY
1891 _mesa_BufferSubData_no_error(GLenum target, GLintptr offset,
1892 GLsizeiptr size, const GLvoid *data)
1893 {
1894 buffer_sub_data(target, 0, offset, size, data, false, true,
1895 "glBufferSubData");
1896 }
1897
1898
1899 void GLAPIENTRY
1900 _mesa_BufferSubData(GLenum target, GLintptr offset,
1901 GLsizeiptr size, const GLvoid *data)
1902 {
1903 buffer_sub_data(target, 0, offset, size, data, false, false,
1904 "glBufferSubData");
1905 }
1906
1907 void GLAPIENTRY
1908 _mesa_NamedBufferSubData_no_error(GLuint buffer, GLintptr offset,
1909 GLsizeiptr size, const GLvoid *data)
1910 {
1911 buffer_sub_data(0, buffer, offset, size, data, true, true,
1912 "glNamedBufferSubData");
1913 }
1914
1915 void GLAPIENTRY
1916 _mesa_NamedBufferSubData(GLuint buffer, GLintptr offset,
1917 GLsizeiptr size, const GLvoid *data)
1918 {
1919 buffer_sub_data(0, buffer, offset, size, data, true, false,
1920 "glNamedBufferSubData");
1921 }
1922
1923
1924 void GLAPIENTRY
1925 _mesa_GetBufferSubData(GLenum target, GLintptr offset,
1926 GLsizeiptr size, GLvoid *data)
1927 {
1928 GET_CURRENT_CONTEXT(ctx);
1929 struct gl_buffer_object *bufObj;
1930
1931 bufObj = get_buffer(ctx, "glGetBufferSubData", target,
1932 GL_INVALID_OPERATION);
1933 if (!bufObj)
1934 return;
1935
1936 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
1937 "glGetBufferSubData")) {
1938 return;
1939 }
1940
1941 assert(ctx->Driver.GetBufferSubData);
1942 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
1943 }
1944
1945 void GLAPIENTRY
1946 _mesa_GetNamedBufferSubData(GLuint buffer, GLintptr offset,
1947 GLsizeiptr size, GLvoid *data)
1948 {
1949 GET_CURRENT_CONTEXT(ctx);
1950 struct gl_buffer_object *bufObj;
1951
1952 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
1953 "glGetNamedBufferSubData");
1954 if (!bufObj)
1955 return;
1956
1957 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
1958 "glGetNamedBufferSubData")) {
1959 return;
1960 }
1961
1962 assert(ctx->Driver.GetBufferSubData);
1963 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
1964 }
1965
1966
1967 /**
1968 * \param subdata true if caller is *SubData, false if *Data
1969 */
1970 static void
1971 clear_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1972 GLenum internalformat, GLintptr offset, GLsizeiptr size,
1973 GLenum format, GLenum type, const GLvoid *data,
1974 const char *func, bool subdata)
1975 {
1976 mesa_format mesaFormat;
1977 GLubyte clearValue[MAX_PIXEL_BYTES];
1978 GLsizeiptr clearValueSize;
1979
1980 /* This checks for disallowed mappings. */
1981 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
1982 subdata, func)) {
1983 return;
1984 }
1985
1986 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1987 format, type, func);
1988
1989 if (mesaFormat == MESA_FORMAT_NONE) {
1990 return;
1991 }
1992
1993 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1994 if (offset % clearValueSize != 0 || size % clearValueSize != 0) {
1995 _mesa_error(ctx, GL_INVALID_VALUE,
1996 "%s(offset or size is not a multiple of "
1997 "internalformat size)", func);
1998 return;
1999 }
2000
2001 /* Bail early. Negative size has already been checked. */
2002 if (size == 0)
2003 return;
2004
2005 bufObj->MinMaxCacheDirty = true;
2006
2007 if (data == NULL) {
2008 /* clear to zeros, per the spec */
2009 ctx->Driver.ClearBufferSubData(ctx, offset, size,
2010 NULL, clearValueSize, bufObj);
2011 return;
2012 }
2013
2014 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
2015 format, type, data, func)) {
2016 return;
2017 }
2018
2019 ctx->Driver.ClearBufferSubData(ctx, offset, size,
2020 clearValue, clearValueSize, bufObj);
2021 }
2022
2023 void GLAPIENTRY
2024 _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
2025 GLenum type, const GLvoid *data)
2026 {
2027 GET_CURRENT_CONTEXT(ctx);
2028 struct gl_buffer_object *bufObj;
2029
2030 bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
2031 if (!bufObj)
2032 return;
2033
2034 clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size,
2035 format, type, data, "glClearBufferData", false);
2036 }
2037
2038 void GLAPIENTRY
2039 _mesa_ClearNamedBufferData(GLuint buffer, GLenum internalformat,
2040 GLenum format, GLenum type, const GLvoid *data)
2041 {
2042 GET_CURRENT_CONTEXT(ctx);
2043 struct gl_buffer_object *bufObj;
2044
2045 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glClearNamedBufferData");
2046 if (!bufObj)
2047 return;
2048
2049 clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size,
2050 format, type, data, "glClearNamedBufferData", false);
2051 }
2052
2053
2054 void GLAPIENTRY
2055 _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
2056 GLintptr offset, GLsizeiptr size,
2057 GLenum format, GLenum type,
2058 const GLvoid *data)
2059 {
2060 GET_CURRENT_CONTEXT(ctx);
2061 struct gl_buffer_object *bufObj;
2062
2063 bufObj = get_buffer(ctx, "glClearBufferSubData", target, GL_INVALID_VALUE);
2064 if (!bufObj)
2065 return;
2066
2067 clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size,
2068 format, type, data, "glClearBufferSubData", true);
2069 }
2070
2071 void GLAPIENTRY
2072 _mesa_ClearNamedBufferSubData(GLuint buffer, GLenum internalformat,
2073 GLintptr offset, GLsizeiptr size,
2074 GLenum format, GLenum type,
2075 const GLvoid *data)
2076 {
2077 GET_CURRENT_CONTEXT(ctx);
2078 struct gl_buffer_object *bufObj;
2079
2080 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2081 "glClearNamedBufferSubData");
2082 if (!bufObj)
2083 return;
2084
2085 clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size, format,
2086 type, data, "glClearNamedBufferSubData", true);
2087 }
2088
2089 static GLboolean
2090 unmap_buffer(struct gl_context *ctx, struct gl_buffer_object *bufObj)
2091 {
2092 GLboolean status = ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_USER);
2093 bufObj->Mappings[MAP_USER].AccessFlags = 0;
2094 assert(bufObj->Mappings[MAP_USER].Pointer == NULL);
2095 assert(bufObj->Mappings[MAP_USER].Offset == 0);
2096 assert(bufObj->Mappings[MAP_USER].Length == 0);
2097
2098 return status;
2099 }
2100
2101 static GLboolean
2102 validate_and_unmap_buffer(struct gl_context *ctx,
2103 struct gl_buffer_object *bufObj,
2104 const char *func)
2105 {
2106 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
2107
2108 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2109 _mesa_error(ctx, GL_INVALID_OPERATION,
2110 "%s(buffer is not mapped)", func);
2111 return GL_FALSE;
2112 }
2113
2114 #ifdef BOUNDS_CHECK
2115 if (bufObj->Access != GL_READ_ONLY_ARB) {
2116 GLubyte *buf = (GLubyte *) bufObj->Pointer;
2117 GLuint i;
2118 /* check that last 100 bytes are still = magic value */
2119 for (i = 0; i < 100; i++) {
2120 GLuint pos = bufObj->Size - i - 1;
2121 if (buf[pos] != 123) {
2122 _mesa_warning(ctx, "Out of bounds buffer object write detected"
2123 " at position %d (value = %u)\n",
2124 pos, buf[pos]);
2125 }
2126 }
2127 }
2128 #endif
2129
2130 #ifdef VBO_DEBUG
2131 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
2132 GLuint i, unchanged = 0;
2133 GLubyte *b = (GLubyte *) bufObj->Pointer;
2134 GLint pos = -1;
2135 /* check which bytes changed */
2136 for (i = 0; i < bufObj->Size - 1; i++) {
2137 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
2138 unchanged++;
2139 if (pos == -1)
2140 pos = i;
2141 }
2142 }
2143 if (unchanged) {
2144 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2145 bufObj->Name, unchanged, bufObj->Size, pos);
2146 }
2147 }
2148 #endif
2149
2150 return unmap_buffer(ctx, bufObj);
2151 }
2152
2153 GLboolean GLAPIENTRY
2154 _mesa_UnmapBuffer_no_error(GLenum target)
2155 {
2156 GET_CURRENT_CONTEXT(ctx);
2157 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
2158 struct gl_buffer_object *bufObj = *bufObjPtr;
2159
2160 return unmap_buffer(ctx, bufObj);
2161 }
2162
2163 GLboolean GLAPIENTRY
2164 _mesa_UnmapBuffer(GLenum target)
2165 {
2166 GET_CURRENT_CONTEXT(ctx);
2167 struct gl_buffer_object *bufObj;
2168
2169 bufObj = get_buffer(ctx, "glUnmapBuffer", target, GL_INVALID_OPERATION);
2170 if (!bufObj)
2171 return GL_FALSE;
2172
2173 return validate_and_unmap_buffer(ctx, bufObj, "glUnmapBuffer");
2174 }
2175
2176 GLboolean GLAPIENTRY
2177 _mesa_UnmapNamedBuffer_no_error(GLuint buffer)
2178 {
2179 GET_CURRENT_CONTEXT(ctx);
2180 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2181
2182 return unmap_buffer(ctx, bufObj);
2183 }
2184
2185 GLboolean GLAPIENTRY
2186 _mesa_UnmapNamedBuffer(GLuint buffer)
2187 {
2188 GET_CURRENT_CONTEXT(ctx);
2189 struct gl_buffer_object *bufObj;
2190
2191 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glUnmapNamedBuffer");
2192 if (!bufObj)
2193 return GL_FALSE;
2194
2195 return validate_and_unmap_buffer(ctx, bufObj, "glUnmapNamedBuffer");
2196 }
2197
2198
2199 static bool
2200 get_buffer_parameter(struct gl_context *ctx,
2201 struct gl_buffer_object *bufObj, GLenum pname,
2202 GLint64 *params, const char *func)
2203 {
2204 switch (pname) {
2205 case GL_BUFFER_SIZE_ARB:
2206 *params = bufObj->Size;
2207 break;
2208 case GL_BUFFER_USAGE_ARB:
2209 *params = bufObj->Usage;
2210 break;
2211 case GL_BUFFER_ACCESS_ARB:
2212 *params = simplified_access_mode(ctx,
2213 bufObj->Mappings[MAP_USER].AccessFlags);
2214 break;
2215 case GL_BUFFER_MAPPED_ARB:
2216 *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
2217 break;
2218 case GL_BUFFER_ACCESS_FLAGS:
2219 if (!ctx->Extensions.ARB_map_buffer_range)
2220 goto invalid_pname;
2221 *params = bufObj->Mappings[MAP_USER].AccessFlags;
2222 break;
2223 case GL_BUFFER_MAP_OFFSET:
2224 if (!ctx->Extensions.ARB_map_buffer_range)
2225 goto invalid_pname;
2226 *params = bufObj->Mappings[MAP_USER].Offset;
2227 break;
2228 case GL_BUFFER_MAP_LENGTH:
2229 if (!ctx->Extensions.ARB_map_buffer_range)
2230 goto invalid_pname;
2231 *params = bufObj->Mappings[MAP_USER].Length;
2232 break;
2233 case GL_BUFFER_IMMUTABLE_STORAGE:
2234 if (!ctx->Extensions.ARB_buffer_storage)
2235 goto invalid_pname;
2236 *params = bufObj->Immutable;
2237 break;
2238 case GL_BUFFER_STORAGE_FLAGS:
2239 if (!ctx->Extensions.ARB_buffer_storage)
2240 goto invalid_pname;
2241 *params = bufObj->StorageFlags;
2242 break;
2243 default:
2244 goto invalid_pname;
2245 }
2246
2247 return true;
2248
2249 invalid_pname:
2250 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname: %s)", func,
2251 _mesa_enum_to_string(pname));
2252 return false;
2253 }
2254
2255 void GLAPIENTRY
2256 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
2257 {
2258 GET_CURRENT_CONTEXT(ctx);
2259 struct gl_buffer_object *bufObj;
2260 GLint64 parameter;
2261
2262 bufObj = get_buffer(ctx, "glGetBufferParameteriv", target,
2263 GL_INVALID_OPERATION);
2264 if (!bufObj)
2265 return;
2266
2267 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2268 "glGetBufferParameteriv"))
2269 return; /* Error already recorded. */
2270
2271 *params = (GLint) parameter;
2272 }
2273
2274 void GLAPIENTRY
2275 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
2276 {
2277 GET_CURRENT_CONTEXT(ctx);
2278 struct gl_buffer_object *bufObj;
2279 GLint64 parameter;
2280
2281 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
2282 GL_INVALID_OPERATION);
2283 if (!bufObj)
2284 return;
2285
2286 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2287 "glGetBufferParameteri64v"))
2288 return; /* Error already recorded. */
2289
2290 *params = parameter;
2291 }
2292
2293 void GLAPIENTRY
2294 _mesa_GetNamedBufferParameteriv(GLuint buffer, GLenum pname, GLint *params)
2295 {
2296 GET_CURRENT_CONTEXT(ctx);
2297 struct gl_buffer_object *bufObj;
2298 GLint64 parameter;
2299
2300 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2301 "glGetNamedBufferParameteriv");
2302 if (!bufObj)
2303 return;
2304
2305 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2306 "glGetNamedBufferParameteriv"))
2307 return; /* Error already recorded. */
2308
2309 *params = (GLint) parameter;
2310 }
2311
2312 void GLAPIENTRY
2313 _mesa_GetNamedBufferParameteri64v(GLuint buffer, GLenum pname,
2314 GLint64 *params)
2315 {
2316 GET_CURRENT_CONTEXT(ctx);
2317 struct gl_buffer_object *bufObj;
2318 GLint64 parameter;
2319
2320 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2321 "glGetNamedBufferParameteri64v");
2322 if (!bufObj)
2323 return;
2324
2325 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2326 "glGetNamedBufferParameteri64v"))
2327 return; /* Error already recorded. */
2328
2329 *params = parameter;
2330 }
2331
2332
2333 void GLAPIENTRY
2334 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
2335 {
2336 GET_CURRENT_CONTEXT(ctx);
2337 struct gl_buffer_object *bufObj;
2338
2339 if (pname != GL_BUFFER_MAP_POINTER) {
2340 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointerv(pname != "
2341 "GL_BUFFER_MAP_POINTER)");
2342 return;
2343 }
2344
2345 bufObj = get_buffer(ctx, "glGetBufferPointerv", target,
2346 GL_INVALID_OPERATION);
2347 if (!bufObj)
2348 return;
2349
2350 *params = bufObj->Mappings[MAP_USER].Pointer;
2351 }
2352
2353 void GLAPIENTRY
2354 _mesa_GetNamedBufferPointerv(GLuint buffer, GLenum pname, GLvoid **params)
2355 {
2356 GET_CURRENT_CONTEXT(ctx);
2357 struct gl_buffer_object *bufObj;
2358
2359 if (pname != GL_BUFFER_MAP_POINTER) {
2360 _mesa_error(ctx, GL_INVALID_ENUM, "glGetNamedBufferPointerv(pname != "
2361 "GL_BUFFER_MAP_POINTER)");
2362 return;
2363 }
2364
2365 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2366 "glGetNamedBufferPointerv");
2367 if (!bufObj)
2368 return;
2369
2370 *params = bufObj->Mappings[MAP_USER].Pointer;
2371 }
2372
2373
2374 static void
2375 copy_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *src,
2376 struct gl_buffer_object *dst, GLintptr readOffset,
2377 GLintptr writeOffset, GLsizeiptr size, const char *func)
2378 {
2379 if (_mesa_check_disallowed_mapping(src)) {
2380 _mesa_error(ctx, GL_INVALID_OPERATION,
2381 "%s(readBuffer is mapped)", func);
2382 return;
2383 }
2384
2385 if (_mesa_check_disallowed_mapping(dst)) {
2386 _mesa_error(ctx, GL_INVALID_OPERATION,
2387 "%s(writeBuffer is mapped)", func);
2388 return;
2389 }
2390
2391 if (readOffset < 0) {
2392 _mesa_error(ctx, GL_INVALID_VALUE,
2393 "%s(readOffset %d < 0)", func, (int) readOffset);
2394 return;
2395 }
2396
2397 if (writeOffset < 0) {
2398 _mesa_error(ctx, GL_INVALID_VALUE,
2399 "%s(writeOffset %d < 0)", func, (int) writeOffset);
2400 return;
2401 }
2402
2403 if (size < 0) {
2404 _mesa_error(ctx, GL_INVALID_VALUE,
2405 "%s(size %d < 0)", func, (int) size);
2406 return;
2407 }
2408
2409 if (readOffset + size > src->Size) {
2410 _mesa_error(ctx, GL_INVALID_VALUE,
2411 "%s(readOffset %d + size %d > src_buffer_size %d)", func,
2412 (int) readOffset, (int) size, (int) src->Size);
2413 return;
2414 }
2415
2416 if (writeOffset + size > dst->Size) {
2417 _mesa_error(ctx, GL_INVALID_VALUE,
2418 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func,
2419 (int) writeOffset, (int) size, (int) dst->Size);
2420 return;
2421 }
2422
2423 if (src == dst) {
2424 if (readOffset + size <= writeOffset) {
2425 /* OK */
2426 }
2427 else if (writeOffset + size <= readOffset) {
2428 /* OK */
2429 }
2430 else {
2431 /* overlapping src/dst is illegal */
2432 _mesa_error(ctx, GL_INVALID_VALUE,
2433 "%s(overlapping src/dst)", func);
2434 return;
2435 }
2436 }
2437
2438 dst->MinMaxCacheDirty = true;
2439
2440 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
2441 }
2442
2443 void GLAPIENTRY
2444 _mesa_CopyBufferSubData_no_error(GLenum readTarget, GLenum writeTarget,
2445 GLintptr readOffset, GLintptr writeOffset,
2446 GLsizeiptr size)
2447 {
2448 GET_CURRENT_CONTEXT(ctx);
2449
2450 struct gl_buffer_object **src_ptr = get_buffer_target(ctx, readTarget);
2451 struct gl_buffer_object *src = *src_ptr;
2452
2453 struct gl_buffer_object **dst_ptr = get_buffer_target(ctx, writeTarget);
2454 struct gl_buffer_object *dst = *dst_ptr;
2455
2456 dst->MinMaxCacheDirty = true;
2457 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset,
2458 size);
2459 }
2460
2461 void GLAPIENTRY
2462 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
2463 GLintptr readOffset, GLintptr writeOffset,
2464 GLsizeiptr size)
2465 {
2466 GET_CURRENT_CONTEXT(ctx);
2467 struct gl_buffer_object *src, *dst;
2468
2469 src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
2470 GL_INVALID_OPERATION);
2471 if (!src)
2472 return;
2473
2474 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
2475 GL_INVALID_OPERATION);
2476 if (!dst)
2477 return;
2478
2479 copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
2480 "glCopyBufferSubData");
2481 }
2482
2483 void GLAPIENTRY
2484 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer, GLuint writeBuffer,
2485 GLintptr readOffset,
2486 GLintptr writeOffset, GLsizeiptr size)
2487 {
2488 GET_CURRENT_CONTEXT(ctx);
2489
2490 struct gl_buffer_object *src = _mesa_lookup_bufferobj(ctx, readBuffer);
2491 struct gl_buffer_object *dst = _mesa_lookup_bufferobj(ctx, writeBuffer);
2492
2493 dst->MinMaxCacheDirty = true;
2494 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset,
2495 size);
2496 }
2497
2498 void GLAPIENTRY
2499 _mesa_CopyNamedBufferSubData(GLuint readBuffer, GLuint writeBuffer,
2500 GLintptr readOffset, GLintptr writeOffset,
2501 GLsizeiptr size)
2502 {
2503 GET_CURRENT_CONTEXT(ctx);
2504 struct gl_buffer_object *src, *dst;
2505
2506 src = _mesa_lookup_bufferobj_err(ctx, readBuffer,
2507 "glCopyNamedBufferSubData");
2508 if (!src)
2509 return;
2510
2511 dst = _mesa_lookup_bufferobj_err(ctx, writeBuffer,
2512 "glCopyNamedBufferSubData");
2513 if (!dst)
2514 return;
2515
2516 copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
2517 "glCopyNamedBufferSubData");
2518 }
2519
2520 static bool
2521 validate_map_buffer_range(struct gl_context *ctx,
2522 struct gl_buffer_object *bufObj, GLintptr offset,
2523 GLsizeiptr length, GLbitfield access,
2524 const char *func)
2525 {
2526 GLbitfield allowed_access;
2527
2528 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, false);
2529
2530 if (offset < 0) {
2531 _mesa_error(ctx, GL_INVALID_VALUE,
2532 "%s(offset %ld < 0)", func, (long) offset);
2533 return false;
2534 }
2535
2536 if (length < 0) {
2537 _mesa_error(ctx, GL_INVALID_VALUE,
2538 "%s(length %ld < 0)", func, (long) length);
2539 return false;
2540 }
2541
2542 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2543 *
2544 * "An INVALID_OPERATION error is generated for any of the following
2545 * conditions:
2546 *
2547 * * <length> is zero."
2548 *
2549 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2550 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2551 * either.
2552 */
2553 if (length == 0) {
2554 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(length = 0)", func);
2555 return false;
2556 }
2557
2558 allowed_access = GL_MAP_READ_BIT |
2559 GL_MAP_WRITE_BIT |
2560 GL_MAP_INVALIDATE_RANGE_BIT |
2561 GL_MAP_INVALIDATE_BUFFER_BIT |
2562 GL_MAP_FLUSH_EXPLICIT_BIT |
2563 GL_MAP_UNSYNCHRONIZED_BIT;
2564
2565 if (ctx->Extensions.ARB_buffer_storage) {
2566 allowed_access |= GL_MAP_PERSISTENT_BIT |
2567 GL_MAP_COHERENT_BIT;
2568 }
2569
2570 if (access & ~allowed_access) {
2571 /* generate an error if any bits other than those allowed are set */
2572 _mesa_error(ctx, GL_INVALID_VALUE,
2573 "%s(access has undefined bits set)", func);
2574 return false;
2575 }
2576
2577 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
2578 _mesa_error(ctx, GL_INVALID_OPERATION,
2579 "%s(access indicates neither read or write)", func);
2580 return false;
2581 }
2582
2583 if ((access & GL_MAP_READ_BIT) &&
2584 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
2585 GL_MAP_INVALIDATE_BUFFER_BIT |
2586 GL_MAP_UNSYNCHRONIZED_BIT))) {
2587 _mesa_error(ctx, GL_INVALID_OPERATION,
2588 "%s(read access with disallowed bits)", func);
2589 return false;
2590 }
2591
2592 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
2593 ((access & GL_MAP_WRITE_BIT) == 0)) {
2594 _mesa_error(ctx, GL_INVALID_OPERATION,
2595 "%s(access has flush explicit without write)", func);
2596 return false;
2597 }
2598
2599 if (access & GL_MAP_READ_BIT &&
2600 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
2601 _mesa_error(ctx, GL_INVALID_OPERATION,
2602 "%s(buffer does not allow read access)", func);
2603 return false;
2604 }
2605
2606 if (access & GL_MAP_WRITE_BIT &&
2607 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
2608 _mesa_error(ctx, GL_INVALID_OPERATION,
2609 "%s(buffer does not allow write access)", func);
2610 return false;
2611 }
2612
2613 if (access & GL_MAP_COHERENT_BIT &&
2614 !(bufObj->StorageFlags & GL_MAP_COHERENT_BIT)) {
2615 _mesa_error(ctx, GL_INVALID_OPERATION,
2616 "%s(buffer does not allow coherent access)", func);
2617 return false;
2618 }
2619
2620 if (access & GL_MAP_PERSISTENT_BIT &&
2621 !(bufObj->StorageFlags & GL_MAP_PERSISTENT_BIT)) {
2622 _mesa_error(ctx, GL_INVALID_OPERATION,
2623 "%s(buffer does not allow persistent access)", func);
2624 return false;
2625 }
2626
2627 if (offset + length > bufObj->Size) {
2628 _mesa_error(ctx, GL_INVALID_VALUE,
2629 "%s(offset %lu + length %lu > buffer_size %lu)", func,
2630 (unsigned long) offset, (unsigned long) length,
2631 (unsigned long) bufObj->Size);
2632 return false;
2633 }
2634
2635 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2636 _mesa_error(ctx, GL_INVALID_OPERATION,
2637 "%s(buffer already mapped)", func);
2638 return false;
2639 }
2640
2641 if (access & GL_MAP_WRITE_BIT) {
2642 bufObj->NumMapBufferWriteCalls++;
2643 if ((bufObj->Usage == GL_STATIC_DRAW ||
2644 bufObj->Usage == GL_STATIC_COPY) &&
2645 bufObj->NumMapBufferWriteCalls >= BUFFER_WARNING_CALL_COUNT) {
2646 BUFFER_USAGE_WARNING(ctx,
2647 "using %s(buffer %u, offset %u, length %u) to "
2648 "update a %s buffer",
2649 func, bufObj->Name, offset, length,
2650 _mesa_enum_to_string(bufObj->Usage));
2651 }
2652 }
2653
2654 return true;
2655 }
2656
2657 static void *
2658 map_buffer_range(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2659 GLintptr offset, GLsizeiptr length, GLbitfield access,
2660 const char *func)
2661 {
2662 if (!bufObj->Size) {
2663 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(buffer size = 0)", func);
2664 return NULL;
2665 }
2666
2667 assert(ctx->Driver.MapBufferRange);
2668 void *map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj,
2669 MAP_USER);
2670 if (!map) {
2671 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(map failed)", func);
2672 }
2673 else {
2674 /* The driver callback should have set all these fields.
2675 * This is important because other modules (like VBO) might call
2676 * the driver function directly.
2677 */
2678 assert(bufObj->Mappings[MAP_USER].Pointer == map);
2679 assert(bufObj->Mappings[MAP_USER].Length == length);
2680 assert(bufObj->Mappings[MAP_USER].Offset == offset);
2681 assert(bufObj->Mappings[MAP_USER].AccessFlags == access);
2682 }
2683
2684 if (access & GL_MAP_WRITE_BIT) {
2685 bufObj->Written = GL_TRUE;
2686 bufObj->MinMaxCacheDirty = true;
2687 }
2688
2689 #ifdef VBO_DEBUG
2690 if (strstr(func, "Range") == NULL) { /* If not MapRange */
2691 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2692 bufObj->Name, bufObj->Size, access);
2693 /* Access must be write only */
2694 if ((access & GL_MAP_WRITE_BIT) && (!(access & ~GL_MAP_WRITE_BIT))) {
2695 GLuint i;
2696 GLubyte *b = (GLubyte *) bufObj->Pointer;
2697 for (i = 0; i < bufObj->Size; i++)
2698 b[i] = i & 0xff;
2699 }
2700 }
2701 #endif
2702
2703 #ifdef BOUNDS_CHECK
2704 if (strstr(func, "Range") == NULL) { /* If not MapRange */
2705 GLubyte *buf = (GLubyte *) bufObj->Pointer;
2706 GLuint i;
2707 /* buffer is 100 bytes larger than requested, fill with magic value */
2708 for (i = 0; i < 100; i++) {
2709 buf[bufObj->Size - i - 1] = 123;
2710 }
2711 }
2712 #endif
2713
2714 return map;
2715 }
2716
2717 void * GLAPIENTRY
2718 _mesa_MapBufferRange_no_error(GLenum target, GLintptr offset,
2719 GLsizeiptr length, GLbitfield access)
2720 {
2721 GET_CURRENT_CONTEXT(ctx);
2722
2723 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
2724 struct gl_buffer_object *bufObj = *bufObjPtr;
2725
2726 return map_buffer_range(ctx, bufObj, offset, length, access,
2727 "glMapBufferRange");
2728 }
2729
2730 void * GLAPIENTRY
2731 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
2732 GLbitfield access)
2733 {
2734 GET_CURRENT_CONTEXT(ctx);
2735 struct gl_buffer_object *bufObj;
2736
2737 if (!ctx->Extensions.ARB_map_buffer_range) {
2738 _mesa_error(ctx, GL_INVALID_OPERATION,
2739 "glMapBufferRange(ARB_map_buffer_range not supported)");
2740 return NULL;
2741 }
2742
2743 bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
2744 if (!bufObj)
2745 return NULL;
2746
2747 if (!validate_map_buffer_range(ctx, bufObj, offset, length, access,
2748 "glMapBufferRange"))
2749 return NULL;
2750
2751 return map_buffer_range(ctx, bufObj, offset, length, access,
2752 "glMapBufferRange");
2753 }
2754
2755 void * GLAPIENTRY
2756 _mesa_MapNamedBufferRange_no_error(GLuint buffer, GLintptr offset,
2757 GLsizeiptr length, GLbitfield access)
2758 {
2759 GET_CURRENT_CONTEXT(ctx);
2760 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2761
2762 return map_buffer_range(ctx, bufObj, offset, length, access,
2763 "glMapNamedBufferRange");
2764 }
2765
2766 void * GLAPIENTRY
2767 _mesa_MapNamedBufferRange(GLuint buffer, GLintptr offset, GLsizeiptr length,
2768 GLbitfield access)
2769 {
2770 GET_CURRENT_CONTEXT(ctx);
2771 struct gl_buffer_object *bufObj;
2772
2773 if (!ctx->Extensions.ARB_map_buffer_range) {
2774 _mesa_error(ctx, GL_INVALID_OPERATION,
2775 "glMapNamedBufferRange("
2776 "ARB_map_buffer_range not supported)");
2777 return NULL;
2778 }
2779
2780 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBufferRange");
2781 if (!bufObj)
2782 return NULL;
2783
2784 if (!validate_map_buffer_range(ctx, bufObj, offset, length, access,
2785 "glMapNamedBufferRange"))
2786 return NULL;
2787
2788 return map_buffer_range(ctx, bufObj, offset, length, access,
2789 "glMapNamedBufferRange");
2790 }
2791
2792 /**
2793 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2794 * flags for input to map_buffer_range.
2795 *
2796 * \return true if the type of requested access is permissible.
2797 */
2798 static bool
2799 get_map_buffer_access_flags(struct gl_context *ctx, GLenum access,
2800 GLbitfield *flags)
2801 {
2802 switch (access) {
2803 case GL_READ_ONLY_ARB:
2804 *flags = GL_MAP_READ_BIT;
2805 return _mesa_is_desktop_gl(ctx);
2806 case GL_WRITE_ONLY_ARB:
2807 *flags = GL_MAP_WRITE_BIT;
2808 return true;
2809 case GL_READ_WRITE_ARB:
2810 *flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
2811 return _mesa_is_desktop_gl(ctx);
2812 default:
2813 return false;
2814 }
2815 }
2816
2817 void * GLAPIENTRY
2818 _mesa_MapBuffer_no_error(GLenum target, GLenum access)
2819 {
2820 GET_CURRENT_CONTEXT(ctx);
2821
2822 GLbitfield accessFlags;
2823 get_map_buffer_access_flags(ctx, access, &accessFlags);
2824
2825 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
2826 struct gl_buffer_object *bufObj = *bufObjPtr;
2827
2828 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2829 "glMapBuffer");
2830 }
2831
2832 void * GLAPIENTRY
2833 _mesa_MapBuffer(GLenum target, GLenum access)
2834 {
2835 GET_CURRENT_CONTEXT(ctx);
2836 struct gl_buffer_object *bufObj;
2837 GLbitfield accessFlags;
2838
2839 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
2840 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBuffer(invalid access)");
2841 return NULL;
2842 }
2843
2844 bufObj = get_buffer(ctx, "glMapBuffer", target, GL_INVALID_OPERATION);
2845 if (!bufObj)
2846 return NULL;
2847
2848 if (!validate_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2849 "glMapBuffer"))
2850 return NULL;
2851
2852 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2853 "glMapBuffer");
2854 }
2855
2856 void * GLAPIENTRY
2857 _mesa_MapNamedBuffer_no_error(GLuint buffer, GLenum access)
2858 {
2859 GET_CURRENT_CONTEXT(ctx);
2860
2861 GLbitfield accessFlags;
2862 get_map_buffer_access_flags(ctx, access, &accessFlags);
2863
2864 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2865
2866 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2867 "glMapNamedBuffer");
2868 }
2869
2870 void * GLAPIENTRY
2871 _mesa_MapNamedBuffer(GLuint buffer, GLenum access)
2872 {
2873 GET_CURRENT_CONTEXT(ctx);
2874 struct gl_buffer_object *bufObj;
2875 GLbitfield accessFlags;
2876
2877 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
2878 _mesa_error(ctx, GL_INVALID_ENUM, "glMapNamedBuffer(invalid access)");
2879 return NULL;
2880 }
2881
2882 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBuffer");
2883 if (!bufObj)
2884 return NULL;
2885
2886 if (!validate_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2887 "glMapNamedBuffer"))
2888 return NULL;
2889
2890 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2891 "glMapNamedBuffer");
2892 }
2893
2894
2895 static void
2896 flush_mapped_buffer_range(struct gl_context *ctx,
2897 struct gl_buffer_object *bufObj,
2898 GLintptr offset, GLsizeiptr length,
2899 const char *func)
2900 {
2901 if (!ctx->Extensions.ARB_map_buffer_range) {
2902 _mesa_error(ctx, GL_INVALID_OPERATION,
2903 "%s(ARB_map_buffer_range not supported)", func);
2904 return;
2905 }
2906
2907 if (offset < 0) {
2908 _mesa_error(ctx, GL_INVALID_VALUE,
2909 "%s(offset %ld < 0)", func, (long) offset);
2910 return;
2911 }
2912
2913 if (length < 0) {
2914 _mesa_error(ctx, GL_INVALID_VALUE,
2915 "%s(length %ld < 0)", func, (long) length);
2916 return;
2917 }
2918
2919 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2920 /* buffer is not mapped */
2921 _mesa_error(ctx, GL_INVALID_OPERATION,
2922 "%s(buffer is not mapped)", func);
2923 return;
2924 }
2925
2926 if ((bufObj->Mappings[MAP_USER].AccessFlags &
2927 GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
2928 _mesa_error(ctx, GL_INVALID_OPERATION,
2929 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func);
2930 return;
2931 }
2932
2933 if (offset + length > bufObj->Mappings[MAP_USER].Length) {
2934 _mesa_error(ctx, GL_INVALID_VALUE,
2935 "%s(offset %ld + length %ld > mapped length %ld)", func,
2936 (long) offset, (long) length,
2937 (long) bufObj->Mappings[MAP_USER].Length);
2938 return;
2939 }
2940
2941 assert(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_WRITE_BIT);
2942
2943 if (ctx->Driver.FlushMappedBufferRange)
2944 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
2945 MAP_USER);
2946 }
2947
2948 void GLAPIENTRY
2949 _mesa_FlushMappedBufferRange_no_error(GLenum target, GLintptr offset,
2950 GLsizeiptr length)
2951 {
2952 GET_CURRENT_CONTEXT(ctx);
2953 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
2954 struct gl_buffer_object *bufObj = *bufObjPtr;
2955
2956 if (ctx->Driver.FlushMappedBufferRange)
2957 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
2958 MAP_USER);
2959 }
2960
2961 void GLAPIENTRY
2962 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset,
2963 GLsizeiptr length)
2964 {
2965 GET_CURRENT_CONTEXT(ctx);
2966 struct gl_buffer_object *bufObj;
2967
2968 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
2969 GL_INVALID_OPERATION);
2970 if (!bufObj)
2971 return;
2972
2973 flush_mapped_buffer_range(ctx, bufObj, offset, length,
2974 "glFlushMappedBufferRange");
2975 }
2976
2977 void GLAPIENTRY
2978 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer, GLintptr offset,
2979 GLsizeiptr length)
2980 {
2981 GET_CURRENT_CONTEXT(ctx);
2982 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2983
2984 if (ctx->Driver.FlushMappedBufferRange)
2985 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
2986 MAP_USER);
2987 }
2988
2989 void GLAPIENTRY
2990 _mesa_FlushMappedNamedBufferRange(GLuint buffer, GLintptr offset,
2991 GLsizeiptr length)
2992 {
2993 GET_CURRENT_CONTEXT(ctx);
2994 struct gl_buffer_object *bufObj;
2995
2996 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2997 "glFlushMappedNamedBufferRange");
2998 if (!bufObj)
2999 return;
3000
3001 flush_mapped_buffer_range(ctx, bufObj, offset, length,
3002 "glFlushMappedNamedBufferRange");
3003 }
3004
3005
3006 /**
3007 * Binds a buffer object to a uniform buffer binding point.
3008 *
3009 * The caller is responsible for flushing vertices and updating
3010 * NewDriverState.
3011 */
3012 static void
3013 set_ubo_binding(struct gl_context *ctx,
3014 struct gl_uniform_buffer_binding *binding,
3015 struct gl_buffer_object *bufObj,
3016 GLintptr offset,
3017 GLsizeiptr size,
3018 GLboolean autoSize)
3019 {
3020 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
3021
3022 binding->Offset = offset;
3023 binding->Size = size;
3024 binding->AutomaticSize = autoSize;
3025
3026 /* If this is a real buffer object, mark it has having been used
3027 * at some point as a UBO.
3028 */
3029 if (size >= 0)
3030 bufObj->UsageHistory |= USAGE_UNIFORM_BUFFER;
3031 }
3032
3033 /**
3034 * Binds a buffer object to a shader storage buffer binding point.
3035 *
3036 * The caller is responsible for flushing vertices and updating
3037 * NewDriverState.
3038 */
3039 static void
3040 set_ssbo_binding(struct gl_context *ctx,
3041 struct gl_shader_storage_buffer_binding *binding,
3042 struct gl_buffer_object *bufObj,
3043 GLintptr offset,
3044 GLsizeiptr size,
3045 GLboolean autoSize)
3046 {
3047 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
3048
3049 binding->Offset = offset;
3050 binding->Size = size;
3051 binding->AutomaticSize = autoSize;
3052
3053 /* If this is a real buffer object, mark it has having been used
3054 * at some point as a SSBO.
3055 */
3056 if (size >= 0)
3057 bufObj->UsageHistory |= USAGE_SHADER_STORAGE_BUFFER;
3058 }
3059
3060 /**
3061 * Binds a buffer object to a uniform buffer binding point.
3062 *
3063 * Unlike set_ubo_binding(), this function also flushes vertices
3064 * and updates NewDriverState. It also checks if the binding
3065 * has actually changed before updating it.
3066 */
3067 static void
3068 bind_uniform_buffer(struct gl_context *ctx,
3069 GLuint index,
3070 struct gl_buffer_object *bufObj,
3071 GLintptr offset,
3072 GLsizeiptr size,
3073 GLboolean autoSize)
3074 {
3075 struct gl_uniform_buffer_binding *binding =
3076 &ctx->UniformBufferBindings[index];
3077
3078 if (binding->BufferObject == bufObj &&
3079 binding->Offset == offset &&
3080 binding->Size == size &&
3081 binding->AutomaticSize == autoSize) {
3082 return;
3083 }
3084
3085 FLUSH_VERTICES(ctx, 0);
3086 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3087
3088 set_ubo_binding(ctx, binding, bufObj, offset, size, autoSize);
3089 }
3090
3091 /**
3092 * Binds a buffer object to a shader storage buffer binding point.
3093 *
3094 * Unlike set_ssbo_binding(), this function also flushes vertices
3095 * and updates NewDriverState. It also checks if the binding
3096 * has actually changed before updating it.
3097 */
3098 static void
3099 bind_shader_storage_buffer(struct gl_context *ctx,
3100 GLuint index,
3101 struct gl_buffer_object *bufObj,
3102 GLintptr offset,
3103 GLsizeiptr size,
3104 GLboolean autoSize)
3105 {
3106 struct gl_shader_storage_buffer_binding *binding =
3107 &ctx->ShaderStorageBufferBindings[index];
3108
3109 if (binding->BufferObject == bufObj &&
3110 binding->Offset == offset &&
3111 binding->Size == size &&
3112 binding->AutomaticSize == autoSize) {
3113 return;
3114 }
3115
3116 FLUSH_VERTICES(ctx, 0);
3117 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
3118
3119 set_ssbo_binding(ctx, binding, bufObj, offset, size, autoSize);
3120 }
3121
3122 static void
3123 bind_buffer_range_uniform_buffer(struct gl_context *ctx, GLuint index,
3124 struct gl_buffer_object *bufObj,
3125 GLintptr offset, GLsizeiptr size)
3126 {
3127 if (bufObj == ctx->Shared->NullBufferObj) {
3128 offset = -1;
3129 size = -1;
3130 }
3131
3132 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
3133 bind_uniform_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
3134 }
3135
3136 /**
3137 * Bind a region of a buffer object to a uniform block binding point.
3138 * \param index the uniform buffer binding point index
3139 * \param bufObj the buffer object
3140 * \param offset offset to the start of buffer object region
3141 * \param size size of the buffer object region
3142 */
3143 static void
3144 bind_buffer_range_uniform_buffer_err(struct gl_context *ctx, GLuint index,
3145 struct gl_buffer_object *bufObj,
3146 GLintptr offset, GLsizeiptr size)
3147 {
3148 if (index >= ctx->Const.MaxUniformBufferBindings) {
3149 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
3150 return;
3151 }
3152
3153 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3154 _mesa_error(ctx, GL_INVALID_VALUE,
3155 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
3156 ctx->Const.UniformBufferOffsetAlignment);
3157 return;
3158 }
3159
3160 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
3161 }
3162
3163 static void
3164 bind_buffer_range_shader_storage_buffer(struct gl_context *ctx,
3165 GLuint index,
3166 struct gl_buffer_object *bufObj,
3167 GLintptr offset,
3168 GLsizeiptr size)
3169 {
3170 if (bufObj == ctx->Shared->NullBufferObj) {
3171 offset = -1;
3172 size = -1;
3173 }
3174
3175 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
3176 bind_shader_storage_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
3177 }
3178
3179 /**
3180 * Bind a region of a buffer object to a shader storage block binding point.
3181 * \param index the shader storage buffer binding point index
3182 * \param bufObj the buffer object
3183 * \param offset offset to the start of buffer object region
3184 * \param size size of the buffer object region
3185 */
3186 static void
3187 bind_buffer_range_shader_storage_buffer_err(struct gl_context *ctx,
3188 GLuint index,
3189 struct gl_buffer_object *bufObj,
3190 GLintptr offset, GLsizeiptr size)
3191 {
3192 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
3193 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
3194 return;
3195 }
3196
3197 if (offset & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
3198 _mesa_error(ctx, GL_INVALID_VALUE,
3199 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
3200 ctx->Const.ShaderStorageBufferOffsetAlignment);
3201 return;
3202 }
3203
3204 bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset, size);
3205 }
3206
3207 /**
3208 * Bind a buffer object to a uniform block binding point.
3209 * As above, but offset = 0.
3210 */
3211 static void
3212 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
3213 GLuint index,
3214 struct gl_buffer_object *bufObj)
3215 {
3216 if (index >= ctx->Const.MaxUniformBufferBindings) {
3217 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
3218 return;
3219 }
3220
3221 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
3222
3223 if (bufObj == ctx->Shared->NullBufferObj)
3224 bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
3225 else
3226 bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
3227 }
3228
3229 /**
3230 * Bind a buffer object to a shader storage block binding point.
3231 * As above, but offset = 0.
3232 */
3233 static void
3234 bind_buffer_base_shader_storage_buffer(struct gl_context *ctx,
3235 GLuint index,
3236 struct gl_buffer_object *bufObj)
3237 {
3238 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
3239 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
3240 return;
3241 }
3242
3243 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
3244
3245 if (bufObj == ctx->Shared->NullBufferObj)
3246 bind_shader_storage_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
3247 else
3248 bind_shader_storage_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
3249 }
3250
3251 /**
3252 * Binds a buffer object to an atomic buffer binding point.
3253 *
3254 * The caller is responsible for validating the offset,
3255 * flushing the vertices and updating NewDriverState.
3256 */
3257 static void
3258 set_atomic_buffer_binding(struct gl_context *ctx,
3259 struct gl_atomic_buffer_binding *binding,
3260 struct gl_buffer_object *bufObj,
3261 GLintptr offset,
3262 GLsizeiptr size)
3263 {
3264 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
3265
3266 if (bufObj == ctx->Shared->NullBufferObj) {
3267 binding->Offset = 0;
3268 binding->Size = 0;
3269 } else {
3270 binding->Offset = offset;
3271 binding->Size = size;
3272 bufObj->UsageHistory |= USAGE_ATOMIC_COUNTER_BUFFER;
3273 }
3274 }
3275
3276 static void
3277 bind_atomic_buffer(struct gl_context *ctx, unsigned index,
3278 struct gl_buffer_object *bufObj, GLintptr offset,
3279 GLsizeiptr size)
3280 {
3281 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
3282
3283 struct gl_atomic_buffer_binding *binding =
3284 &ctx->AtomicBufferBindings[index];
3285 if (binding->BufferObject == bufObj &&
3286 binding->Offset == offset &&
3287 binding->Size == size) {
3288 return;
3289 }
3290
3291 FLUSH_VERTICES(ctx, 0);
3292 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3293
3294 set_atomic_buffer_binding(ctx, binding, bufObj, offset, size);
3295 }
3296
3297 /**
3298 * Binds a buffer object to an atomic buffer binding point.
3299 *
3300 * Unlike set_atomic_buffer_binding(), this function also validates the
3301 * index and offset, flushes vertices, and updates NewDriverState.
3302 * It also checks if the binding has actually changing before
3303 * updating it.
3304 */
3305 static void
3306 bind_atomic_buffer_err(struct gl_context *ctx, unsigned index,
3307 struct gl_buffer_object *bufObj, GLintptr offset,
3308 GLsizeiptr size, const char *name)
3309 {
3310 if (index >= ctx->Const.MaxAtomicBufferBindings) {
3311 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
3312 return;
3313 }
3314
3315 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
3316 _mesa_error(ctx, GL_INVALID_VALUE,
3317 "%s(offset misaligned %d/%d)", name, (int) offset,
3318 ATOMIC_COUNTER_SIZE);
3319 return;
3320 }
3321
3322 bind_atomic_buffer(ctx, index, bufObj, offset, size);
3323 }
3324
3325 static inline bool
3326 bind_buffers_check_offset_and_size(struct gl_context *ctx,
3327 GLuint index,
3328 const GLintptr *offsets,
3329 const GLsizeiptr *sizes)
3330 {
3331 if (offsets[index] < 0) {
3332 /* The ARB_multi_bind spec says:
3333 *
3334 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3335 * value in <offsets> is less than zero (per binding)."
3336 */
3337 _mesa_error(ctx, GL_INVALID_VALUE,
3338 "glBindBuffersRange(offsets[%u]=%" PRId64 " < 0)",
3339 index, (int64_t) offsets[index]);
3340 return false;
3341 }
3342
3343 if (sizes[index] <= 0) {
3344 /* The ARB_multi_bind spec says:
3345 *
3346 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3347 * value in <sizes> is less than or equal to zero (per binding)."
3348 */
3349 _mesa_error(ctx, GL_INVALID_VALUE,
3350 "glBindBuffersRange(sizes[%u]=%" PRId64 " <= 0)",
3351 index, (int64_t) sizes[index]);
3352 return false;
3353 }
3354
3355 return true;
3356 }
3357
3358 static bool
3359 error_check_bind_uniform_buffers(struct gl_context *ctx,
3360 GLuint first, GLsizei count,
3361 const char *caller)
3362 {
3363 if (!ctx->Extensions.ARB_uniform_buffer_object) {
3364 _mesa_error(ctx, GL_INVALID_ENUM,
3365 "%s(target=GL_UNIFORM_BUFFER)", caller);
3366 return false;
3367 }
3368
3369 /* The ARB_multi_bind_spec says:
3370 *
3371 * "An INVALID_OPERATION error is generated if <first> + <count> is
3372 * greater than the number of target-specific indexed binding points,
3373 * as described in section 6.7.1."
3374 */
3375 if (first + count > ctx->Const.MaxUniformBufferBindings) {
3376 _mesa_error(ctx, GL_INVALID_OPERATION,
3377 "%s(first=%u + count=%d > the value of "
3378 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3379 caller, first, count,
3380 ctx->Const.MaxUniformBufferBindings);
3381 return false;
3382 }
3383
3384 return true;
3385 }
3386
3387 static bool
3388 error_check_bind_shader_storage_buffers(struct gl_context *ctx,
3389 GLuint first, GLsizei count,
3390 const char *caller)
3391 {
3392 if (!ctx->Extensions.ARB_shader_storage_buffer_object) {
3393 _mesa_error(ctx, GL_INVALID_ENUM,
3394 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller);
3395 return false;
3396 }
3397
3398 /* The ARB_multi_bind_spec says:
3399 *
3400 * "An INVALID_OPERATION error is generated if <first> + <count> is
3401 * greater than the number of target-specific indexed binding points,
3402 * as described in section 6.7.1."
3403 */
3404 if (first + count > ctx->Const.MaxShaderStorageBufferBindings) {
3405 _mesa_error(ctx, GL_INVALID_OPERATION,
3406 "%s(first=%u + count=%d > the value of "
3407 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3408 caller, first, count,
3409 ctx->Const.MaxShaderStorageBufferBindings);
3410 return false;
3411 }
3412
3413 return true;
3414 }
3415
3416 /**
3417 * Unbind all uniform buffers in the range
3418 * <first> through <first>+<count>-1
3419 */
3420 static void
3421 unbind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3422 {
3423 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
3424
3425 for (int i = 0; i < count; i++)
3426 set_ubo_binding(ctx, &ctx->UniformBufferBindings[first + i],
3427 bufObj, -1, -1, GL_TRUE);
3428 }
3429
3430 /**
3431 * Unbind all shader storage buffers in the range
3432 * <first> through <first>+<count>-1
3433 */
3434 static void
3435 unbind_shader_storage_buffers(struct gl_context *ctx, GLuint first,
3436 GLsizei count)
3437 {
3438 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
3439
3440 for (int i = 0; i < count; i++)
3441 set_ssbo_binding(ctx, &ctx->ShaderStorageBufferBindings[first + i],
3442 bufObj, -1, -1, GL_TRUE);
3443 }
3444
3445 static void
3446 bind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count,
3447 const GLuint *buffers,
3448 bool range,
3449 const GLintptr *offsets, const GLsizeiptr *sizes,
3450 const char *caller)
3451 {
3452 if (!error_check_bind_uniform_buffers(ctx, first, count, caller))
3453 return;
3454
3455 /* Assume that at least one binding will be changed */
3456 FLUSH_VERTICES(ctx, 0);
3457 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3458
3459 if (!buffers) {
3460 /* The ARB_multi_bind spec says:
3461 *
3462 * "If <buffers> is NULL, all bindings from <first> through
3463 * <first>+<count>-1 are reset to their unbound (zero) state.
3464 * In this case, the offsets and sizes associated with the
3465 * binding points are set to default values, ignoring
3466 * <offsets> and <sizes>."
3467 */
3468 unbind_uniform_buffers(ctx, first, count);
3469 return;
3470 }
3471
3472 /* Note that the error semantics for multi-bind commands differ from
3473 * those of other GL commands.
3474 *
3475 * The Issues section in the ARB_multi_bind spec says:
3476 *
3477 * "(11) Typically, OpenGL specifies that if an error is generated by a
3478 * command, that command has no effect. This is somewhat
3479 * unfortunate for multi-bind commands, because it would require a
3480 * first pass to scan the entire list of bound objects for errors
3481 * and then a second pass to actually perform the bindings.
3482 * Should we have different error semantics?
3483 *
3484 * RESOLVED: Yes. In this specification, when the parameters for
3485 * one of the <count> binding points are invalid, that binding point
3486 * is not updated and an error will be generated. However, other
3487 * binding points in the same command will be updated if their
3488 * parameters are valid and no other error occurs."
3489 */
3490
3491 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
3492
3493 for (int i = 0; i < count; i++) {
3494 struct gl_uniform_buffer_binding *binding =
3495 &ctx->UniformBufferBindings[first + i];
3496 struct gl_buffer_object *bufObj;
3497 GLintptr offset = 0;
3498 GLsizeiptr size = 0;
3499
3500 if (range) {
3501 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3502 continue;
3503
3504 /* The ARB_multi_bind spec says:
3505 *
3506 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3507 * pair of values in <offsets> and <sizes> does not respectively
3508 * satisfy the constraints described for those parameters for the
3509 * specified target, as described in section 6.7.1 (per binding)."
3510 *
3511 * Section 6.7.1 refers to table 6.5, which says:
3512 *
3513 * "┌───────────────────────────────────────────────────────────────┐
3514 * │ Uniform buffer array bindings (see sec. 7.6) │
3515 * ├─────────────────────┬─────────────────────────────────────────┤
3516 * │ ... │ ... │
3517 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3518 * │ │ OFFSET_ALIGNMENT │
3519 * │ ... │ ... │
3520 * │ size restriction │ none │
3521 * └─────────────────────┴─────────────────────────────────────────┘"
3522 */
3523 if (offsets[i] & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3524 _mesa_error(ctx, GL_INVALID_VALUE,
3525 "glBindBuffersRange(offsets[%u]=%" PRId64
3526 " is misaligned; it must be a multiple of the value of "
3527 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3528 "target=GL_UNIFORM_BUFFER)",
3529 i, (int64_t) offsets[i],
3530 ctx->Const.UniformBufferOffsetAlignment);
3531 continue;
3532 }
3533
3534 offset = offsets[i];
3535 size = sizes[i];
3536 }
3537
3538 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3539 bufObj = binding->BufferObject;
3540 else
3541 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller);
3542
3543 if (bufObj) {
3544 if (bufObj == ctx->Shared->NullBufferObj)
3545 set_ubo_binding(ctx, binding, bufObj, -1, -1, !range);
3546 else
3547 set_ubo_binding(ctx, binding, bufObj, offset, size, !range);
3548 }
3549 }
3550
3551 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
3552 }
3553
3554 static void
3555 bind_shader_storage_buffers(struct gl_context *ctx, GLuint first,
3556 GLsizei count, const GLuint *buffers,
3557 bool range,
3558 const GLintptr *offsets,
3559 const GLsizeiptr *sizes,
3560 const char *caller)
3561 {
3562 if (!error_check_bind_shader_storage_buffers(ctx, first, count, caller))
3563 return;
3564
3565 /* Assume that at least one binding will be changed */
3566 FLUSH_VERTICES(ctx, 0);
3567 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
3568
3569 if (!buffers) {
3570 /* The ARB_multi_bind spec says:
3571 *
3572 * "If <buffers> is NULL, all bindings from <first> through
3573 * <first>+<count>-1 are reset to their unbound (zero) state.
3574 * In this case, the offsets and sizes associated with the
3575 * binding points are set to default values, ignoring
3576 * <offsets> and <sizes>."
3577 */
3578 unbind_shader_storage_buffers(ctx, first, count);
3579 return;
3580 }
3581
3582 /* Note that the error semantics for multi-bind commands differ from
3583 * those of other GL commands.
3584 *
3585 * The Issues section in the ARB_multi_bind spec says:
3586 *
3587 * "(11) Typically, OpenGL specifies that if an error is generated by a
3588 * command, that command has no effect. This is somewhat
3589 * unfortunate for multi-bind commands, because it would require a
3590 * first pass to scan the entire list of bound objects for errors
3591 * and then a second pass to actually perform the bindings.
3592 * Should we have different error semantics?
3593 *
3594 * RESOLVED: Yes. In this specification, when the parameters for
3595 * one of the <count> binding points are invalid, that binding point
3596 * is not updated and an error will be generated. However, other
3597 * binding points in the same command will be updated if their
3598 * parameters are valid and no other error occurs."
3599 */
3600
3601 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
3602
3603 for (int i = 0; i < count; i++) {
3604 struct gl_shader_storage_buffer_binding *binding =
3605 &ctx->ShaderStorageBufferBindings[first + i];
3606 struct gl_buffer_object *bufObj;
3607 GLintptr offset = 0;
3608 GLsizeiptr size = 0;
3609
3610 if (range) {
3611 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3612 continue;
3613
3614 /* The ARB_multi_bind spec says:
3615 *
3616 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3617 * pair of values in <offsets> and <sizes> does not respectively
3618 * satisfy the constraints described for those parameters for the
3619 * specified target, as described in section 6.7.1 (per binding)."
3620 *
3621 * Section 6.7.1 refers to table 6.5, which says:
3622 *
3623 * "┌───────────────────────────────────────────────────────────────┐
3624 * │ Shader storage buffer array bindings (see sec. 7.8) │
3625 * ├─────────────────────┬─────────────────────────────────────────┤
3626 * │ ... │ ... │
3627 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3628 * │ │ BUFFER_OFFSET_ALIGNMENT │
3629 * │ ... │ ... │
3630 * │ size restriction │ none │
3631 * └─────────────────────┴─────────────────────────────────────────┘"
3632 */
3633 if (offsets[i] & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
3634 _mesa_error(ctx, GL_INVALID_VALUE,
3635 "glBindBuffersRange(offsets[%u]=%" PRId64
3636 " is misaligned; it must be a multiple of the value of "
3637 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3638 "target=GL_SHADER_STORAGE_BUFFER)",
3639 i, (int64_t) offsets[i],
3640 ctx->Const.ShaderStorageBufferOffsetAlignment);
3641 continue;
3642 }
3643
3644 offset = offsets[i];
3645 size = sizes[i];
3646 }
3647
3648 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3649 bufObj = binding->BufferObject;
3650 else
3651 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller);
3652
3653 if (bufObj) {
3654 if (bufObj == ctx->Shared->NullBufferObj)
3655 set_ssbo_binding(ctx, binding, bufObj, -1, -1, !range);
3656 else
3657 set_ssbo_binding(ctx, binding, bufObj, offset, size, !range);
3658 }
3659 }
3660
3661 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
3662 }
3663
3664 static bool
3665 error_check_bind_xfb_buffers(struct gl_context *ctx,
3666 struct gl_transform_feedback_object *tfObj,
3667 GLuint first, GLsizei count, const char *caller)
3668 {
3669 if (!ctx->Extensions.EXT_transform_feedback) {
3670 _mesa_error(ctx, GL_INVALID_ENUM,
3671 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller);
3672 return false;
3673 }
3674
3675 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3676 *
3677 * "An INVALID_OPERATION error is generated :
3678 *
3679 * ...
3680 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3681 * FEEDBACK_BUFFER and transform feedback is currently active."
3682 *
3683 * We assume that this is also meant to apply to BindBuffersRange
3684 * and BindBuffersBase.
3685 */
3686 if (tfObj->Active) {
3687 _mesa_error(ctx, GL_INVALID_OPERATION,
3688 "%s(Changing transform feedback buffers while "
3689 "transform feedback is active)", caller);
3690 return false;
3691 }
3692
3693 /* The ARB_multi_bind_spec says:
3694 *
3695 * "An INVALID_OPERATION error is generated if <first> + <count> is
3696 * greater than the number of target-specific indexed binding points,
3697 * as described in section 6.7.1."
3698 */
3699 if (first + count > ctx->Const.MaxTransformFeedbackBuffers) {
3700 _mesa_error(ctx, GL_INVALID_OPERATION,
3701 "%s(first=%u + count=%d > the value of "
3702 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3703 caller, first, count,
3704 ctx->Const.MaxTransformFeedbackBuffers);
3705 return false;
3706 }
3707
3708 return true;
3709 }
3710
3711 /**
3712 * Unbind all transform feedback buffers in the range
3713 * <first> through <first>+<count>-1
3714 */
3715 static void
3716 unbind_xfb_buffers(struct gl_context *ctx,
3717 struct gl_transform_feedback_object *tfObj,
3718 GLuint first, GLsizei count)
3719 {
3720 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3721
3722 for (int i = 0; i < count; i++)
3723 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
3724 bufObj, 0, 0);
3725 }
3726
3727 static void
3728 bind_xfb_buffers(struct gl_context *ctx,
3729 GLuint first, GLsizei count,
3730 const GLuint *buffers,
3731 bool range,
3732 const GLintptr *offsets,
3733 const GLsizeiptr *sizes,
3734 const char *caller)
3735 {
3736 struct gl_transform_feedback_object *tfObj =
3737 ctx->TransformFeedback.CurrentObject;
3738
3739 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count, caller))
3740 return;
3741
3742 /* Assume that at least one binding will be changed */
3743 FLUSH_VERTICES(ctx, 0);
3744 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
3745
3746 if (!buffers) {
3747 /* The ARB_multi_bind spec says:
3748 *
3749 * "If <buffers> is NULL, all bindings from <first> through
3750 * <first>+<count>-1 are reset to their unbound (zero) state.
3751 * In this case, the offsets and sizes associated with the
3752 * binding points are set to default values, ignoring
3753 * <offsets> and <sizes>."
3754 */
3755 unbind_xfb_buffers(ctx, tfObj, first, count);
3756 return;
3757 }
3758
3759 /* Note that the error semantics for multi-bind commands differ from
3760 * those of other GL commands.
3761 *
3762 * The Issues section in the ARB_multi_bind spec says:
3763 *
3764 * "(11) Typically, OpenGL specifies that if an error is generated by a
3765 * command, that command has no effect. This is somewhat
3766 * unfortunate for multi-bind commands, because it would require a
3767 * first pass to scan the entire list of bound objects for errors
3768 * and then a second pass to actually perform the bindings.
3769 * Should we have different error semantics?
3770 *
3771 * RESOLVED: Yes. In this specification, when the parameters for
3772 * one of the <count> binding points are invalid, that binding point
3773 * is not updated and an error will be generated. However, other
3774 * binding points in the same command will be updated if their
3775 * parameters are valid and no other error occurs."
3776 */
3777
3778 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
3779
3780 for (int i = 0; i < count; i++) {
3781 const GLuint index = first + i;
3782 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[index];
3783 struct gl_buffer_object *bufObj;
3784 GLintptr offset = 0;
3785 GLsizeiptr size = 0;
3786
3787 if (range) {
3788 offset = offsets[i];
3789 size = sizes[i];
3790
3791 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3792 continue;
3793
3794 /* The ARB_multi_bind spec says:
3795 *
3796 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3797 * pair of values in <offsets> and <sizes> does not respectively
3798 * satisfy the constraints described for those parameters for the
3799 * specified target, as described in section 6.7.1 (per binding)."
3800 *
3801 * Section 6.7.1 refers to table 6.5, which says:
3802 *
3803 * "┌───────────────────────────────────────────────────────────────┐
3804 * │ Transform feedback array bindings (see sec. 13.2.2) │
3805 * ├───────────────────────┬───────────────────────────────────────┤
3806 * │ ... │ ... │
3807 * │ offset restriction │ multiple of 4 │
3808 * │ ... │ ... │
3809 * │ size restriction │ multiple of 4 │
3810 * └───────────────────────┴───────────────────────────────────────┘"
3811 */
3812 if (offsets[i] & 0x3) {
3813 _mesa_error(ctx, GL_INVALID_VALUE,
3814 "glBindBuffersRange(offsets[%u]=%" PRId64
3815 " is misaligned; it must be a multiple of 4 when "
3816 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3817 i, (int64_t) offsets[i]);
3818 continue;
3819 }
3820
3821 if (sizes[i] & 0x3) {
3822 _mesa_error(ctx, GL_INVALID_VALUE,
3823 "glBindBuffersRange(sizes[%u]=%" PRId64
3824 " is misaligned; it must be a multiple of 4 when "
3825 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3826 i, (int64_t) sizes[i]);
3827 continue;
3828 }
3829
3830 offset = offsets[i];
3831 size = sizes[i];
3832 }
3833
3834 if (boundBufObj && boundBufObj->Name == buffers[i])
3835 bufObj = boundBufObj;
3836 else
3837 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller);
3838
3839 if (bufObj)
3840 _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj,
3841 offset, size);
3842 }
3843
3844 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
3845 }
3846
3847 static bool
3848 error_check_bind_atomic_buffers(struct gl_context *ctx,
3849 GLuint first, GLsizei count,
3850 const char *caller)
3851 {
3852 if (!ctx->Extensions.ARB_shader_atomic_counters) {
3853 _mesa_error(ctx, GL_INVALID_ENUM,
3854 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller);
3855 return false;
3856 }
3857
3858 /* The ARB_multi_bind_spec says:
3859 *
3860 * "An INVALID_OPERATION error is generated if <first> + <count> is
3861 * greater than the number of target-specific indexed binding points,
3862 * as described in section 6.7.1."
3863 */
3864 if (first + count > ctx->Const.MaxAtomicBufferBindings) {
3865 _mesa_error(ctx, GL_INVALID_OPERATION,
3866 "%s(first=%u + count=%d > the value of "
3867 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3868 caller, first, count, ctx->Const.MaxAtomicBufferBindings);
3869 return false;
3870 }
3871
3872 return true;
3873 }
3874
3875 /**
3876 * Unbind all atomic counter buffers in the range
3877 * <first> through <first>+<count>-1
3878 */
3879 static void
3880 unbind_atomic_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3881 {
3882 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3883
3884 for (int i = 0; i < count; i++)
3885 set_atomic_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i],
3886 bufObj, -1, -1);
3887 }
3888
3889 static void
3890 bind_atomic_buffers(struct gl_context *ctx,
3891 GLuint first,
3892 GLsizei count,
3893 const GLuint *buffers,
3894 bool range,
3895 const GLintptr *offsets,
3896 const GLsizeiptr *sizes,
3897 const char *caller)
3898 {
3899 if (!error_check_bind_atomic_buffers(ctx, first, count, caller))
3900 return;
3901
3902 /* Assume that at least one binding will be changed */
3903 FLUSH_VERTICES(ctx, 0);
3904 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3905
3906 if (!buffers) {
3907 /* The ARB_multi_bind spec says:
3908 *
3909 * "If <buffers> is NULL, all bindings from <first> through
3910 * <first>+<count>-1 are reset to their unbound (zero) state.
3911 * In this case, the offsets and sizes associated with the
3912 * binding points are set to default values, ignoring
3913 * <offsets> and <sizes>."
3914 */
3915 unbind_atomic_buffers(ctx, first, count);
3916 return;
3917 }
3918
3919 /* Note that the error semantics for multi-bind commands differ from
3920 * those of other GL commands.
3921 *
3922 * The Issues section in the ARB_multi_bind spec says:
3923 *
3924 * "(11) Typically, OpenGL specifies that if an error is generated by a
3925 * command, that command has no effect. This is somewhat
3926 * unfortunate for multi-bind commands, because it would require a
3927 * first pass to scan the entire list of bound objects for errors
3928 * and then a second pass to actually perform the bindings.
3929 * Should we have different error semantics?
3930 *
3931 * RESOLVED: Yes. In this specification, when the parameters for
3932 * one of the <count> binding points are invalid, that binding point
3933 * is not updated and an error will be generated. However, other
3934 * binding points in the same command will be updated if their
3935 * parameters are valid and no other error occurs."
3936 */
3937
3938 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
3939
3940 for (int i = 0; i < count; i++) {
3941 struct gl_atomic_buffer_binding *binding =
3942 &ctx->AtomicBufferBindings[first + i];
3943 struct gl_buffer_object *bufObj;
3944 GLintptr offset = 0;
3945 GLsizeiptr size = 0;
3946
3947 if (range) {
3948 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3949 continue;
3950
3951 /* The ARB_multi_bind spec says:
3952 *
3953 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3954 * pair of values in <offsets> and <sizes> does not respectively
3955 * satisfy the constraints described for those parameters for the
3956 * specified target, as described in section 6.7.1 (per binding)."
3957 *
3958 * Section 6.7.1 refers to table 6.5, which says:
3959 *
3960 * "┌───────────────────────────────────────────────────────────────┐
3961 * │ Atomic counter array bindings (see sec. 7.7.2) │
3962 * ├───────────────────────┬───────────────────────────────────────┤
3963 * │ ... │ ... │
3964 * │ offset restriction │ multiple of 4 │
3965 * │ ... │ ... │
3966 * │ size restriction │ none │
3967 * └───────────────────────┴───────────────────────────────────────┘"
3968 */
3969 if (offsets[i] & (ATOMIC_COUNTER_SIZE - 1)) {
3970 _mesa_error(ctx, GL_INVALID_VALUE,
3971 "glBindBuffersRange(offsets[%u]=%" PRId64
3972 " is misaligned; it must be a multiple of %d when "
3973 "target=GL_ATOMIC_COUNTER_BUFFER)",
3974 i, (int64_t) offsets[i], ATOMIC_COUNTER_SIZE);
3975 continue;
3976 }
3977
3978 offset = offsets[i];
3979 size = sizes[i];
3980 }
3981
3982 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3983 bufObj = binding->BufferObject;
3984 else
3985 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller);
3986
3987 if (bufObj)
3988 set_atomic_buffer_binding(ctx, binding, bufObj, offset, size);
3989 }
3990
3991 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
3992 }
3993
3994 static ALWAYS_INLINE void
3995 bind_buffer_range(GLenum target, GLuint index, GLuint buffer, GLintptr offset,
3996 GLsizeiptr size, bool no_error)
3997 {
3998 GET_CURRENT_CONTEXT(ctx);
3999 struct gl_buffer_object *bufObj;
4000
4001 if (MESA_VERBOSE & VERBOSE_API) {
4002 _mesa_debug(ctx, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4003 _mesa_enum_to_string(target), index, buffer,
4004 (unsigned long) offset, (unsigned long) size);
4005 }
4006
4007 if (buffer == 0) {
4008 bufObj = ctx->Shared->NullBufferObj;
4009 } else {
4010 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4011 }
4012 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
4013 &bufObj, "glBindBufferRange"))
4014 return;
4015
4016 if (no_error) {
4017 switch (target) {
4018 case GL_TRANSFORM_FEEDBACK_BUFFER:
4019 _mesa_bind_buffer_range_xfb(ctx, ctx->TransformFeedback.CurrentObject,
4020 index, bufObj, offset, size);
4021 return;
4022 case GL_UNIFORM_BUFFER:
4023 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
4024 return;
4025 case GL_SHADER_STORAGE_BUFFER:
4026 bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset,
4027 size);
4028 return;
4029 case GL_ATOMIC_COUNTER_BUFFER:
4030 bind_atomic_buffer(ctx, index, bufObj, offset, size);
4031 return;
4032 default:
4033 unreachable("invalid BindBufferRange target with KHR_no_error");
4034 }
4035 } else {
4036 if (!bufObj) {
4037 _mesa_error(ctx, GL_INVALID_OPERATION,
4038 "glBindBufferRange(invalid buffer=%u)", buffer);
4039 return;
4040 }
4041
4042 if (buffer != 0) {
4043 if (size <= 0) {
4044 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
4045 (int) size);
4046 return;
4047 }
4048 }
4049
4050 switch (target) {
4051 case GL_TRANSFORM_FEEDBACK_BUFFER:
4052 if (!_mesa_validate_buffer_range_xfb(ctx,
4053 ctx->TransformFeedback.CurrentObject,
4054 index, bufObj, offset, size,
4055 false))
4056 return;
4057
4058 _mesa_bind_buffer_range_xfb(ctx, ctx->TransformFeedback.CurrentObject,
4059 index, bufObj, offset, size);
4060 return;
4061 case GL_UNIFORM_BUFFER:
4062 bind_buffer_range_uniform_buffer_err(ctx, index, bufObj, offset,
4063 size);
4064 return;
4065 case GL_SHADER_STORAGE_BUFFER:
4066 bind_buffer_range_shader_storage_buffer_err(ctx, index, bufObj,
4067 offset, size);
4068 return;
4069 case GL_ATOMIC_COUNTER_BUFFER:
4070 bind_atomic_buffer_err(ctx, index, bufObj, offset, size,
4071 "glBindBufferRange");
4072 return;
4073 default:
4074 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
4075 return;
4076 }
4077 }
4078 }
4079
4080 void GLAPIENTRY
4081 _mesa_BindBufferRange_no_error(GLenum target, GLuint index, GLuint buffer,
4082 GLintptr offset, GLsizeiptr size)
4083 {
4084 bind_buffer_range(target, index, buffer, offset, size, true);
4085 }
4086
4087 void GLAPIENTRY
4088 _mesa_BindBufferRange(GLenum target, GLuint index,
4089 GLuint buffer, GLintptr offset, GLsizeiptr size)
4090 {
4091 bind_buffer_range(target, index, buffer, offset, size, false);
4092 }
4093
4094 void GLAPIENTRY
4095 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
4096 {
4097 GET_CURRENT_CONTEXT(ctx);
4098 struct gl_buffer_object *bufObj;
4099
4100 if (MESA_VERBOSE & VERBOSE_API) {
4101 _mesa_debug(ctx, "glBindBufferBase(%s, %u, %u)\n",
4102 _mesa_enum_to_string(target), index, buffer);
4103 }
4104
4105 if (buffer == 0) {
4106 bufObj = ctx->Shared->NullBufferObj;
4107 } else {
4108 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4109 }
4110 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
4111 &bufObj, "glBindBufferBase"))
4112 return;
4113
4114 if (!bufObj) {
4115 _mesa_error(ctx, GL_INVALID_OPERATION,
4116 "glBindBufferBase(invalid buffer=%u)", buffer);
4117 return;
4118 }
4119
4120 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4121 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4122 *
4123 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4124 * zero and size equal to the size of buffer."
4125 *
4126 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4127 *
4128 * "If the parameter (starting offset or size) was not specified when the
4129 * buffer object was bound, zero is returned."
4130 *
4131 * What happens if the size of the buffer changes? Does the size of the
4132 * buffer at the moment glBindBufferBase was called still play a role, like
4133 * the first quote would imply, or is the size meaningless in the
4134 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4135 * core spec page 45 says:
4136 *
4137 * "It is equivalent to calling BindBufferRange with offset zero, while
4138 * size is determined by the size of the bound buffer at the time the
4139 * binding is used."
4140 *
4141 * My interpretation is that the GL 4.1 spec was a clarification of the
4142 * behavior, not a change. In particular, this choice will only make
4143 * rendering work in cases where it would have had undefined results.
4144 */
4145
4146 switch (target) {
4147 case GL_TRANSFORM_FEEDBACK_BUFFER:
4148 _mesa_bind_buffer_base_transform_feedback(ctx,
4149 ctx->TransformFeedback.CurrentObject,
4150 index, bufObj, false);
4151 return;
4152 case GL_UNIFORM_BUFFER:
4153 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
4154 return;
4155 case GL_SHADER_STORAGE_BUFFER:
4156 bind_buffer_base_shader_storage_buffer(ctx, index, bufObj);
4157 return;
4158 case GL_ATOMIC_COUNTER_BUFFER:
4159 bind_atomic_buffer_err(ctx, index, bufObj, 0, 0,
4160 "glBindBufferBase");
4161 return;
4162 default:
4163 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
4164 return;
4165 }
4166 }
4167
4168 void GLAPIENTRY
4169 _mesa_BindBuffersRange(GLenum target, GLuint first, GLsizei count,
4170 const GLuint *buffers,
4171 const GLintptr *offsets, const GLsizeiptr *sizes)
4172 {
4173 GET_CURRENT_CONTEXT(ctx);
4174
4175 if (MESA_VERBOSE & VERBOSE_API) {
4176 _mesa_debug(ctx, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4177 _mesa_enum_to_string(target), first, count,
4178 buffers, offsets, sizes);
4179 }
4180
4181 switch (target) {
4182 case GL_TRANSFORM_FEEDBACK_BUFFER:
4183 bind_xfb_buffers(ctx, first, count, buffers, true, offsets, sizes,
4184 "glBindBuffersRange");
4185 return;
4186 case GL_UNIFORM_BUFFER:
4187 bind_uniform_buffers(ctx, first, count, buffers, true, offsets, sizes,
4188 "glBindBuffersRange");
4189 return;
4190 case GL_SHADER_STORAGE_BUFFER:
4191 bind_shader_storage_buffers(ctx, first, count, buffers, true, offsets, sizes,
4192 "glBindBuffersRange");
4193 return;
4194 case GL_ATOMIC_COUNTER_BUFFER:
4195 bind_atomic_buffers(ctx, first, count, buffers, true, offsets, sizes,
4196 "glBindBuffersRange");
4197 return;
4198 default:
4199 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersRange(target=%s)",
4200 _mesa_enum_to_string(target));
4201 break;
4202 }
4203 }
4204
4205 void GLAPIENTRY
4206 _mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count,
4207 const GLuint *buffers)
4208 {
4209 GET_CURRENT_CONTEXT(ctx);
4210
4211 if (MESA_VERBOSE & VERBOSE_API) {
4212 _mesa_debug(ctx, "glBindBuffersBase(%s, %u, %d, %p)\n",
4213 _mesa_enum_to_string(target), first, count, buffers);
4214 }
4215
4216 switch (target) {
4217 case GL_TRANSFORM_FEEDBACK_BUFFER:
4218 bind_xfb_buffers(ctx, first, count, buffers, false, NULL, NULL,
4219 "glBindBuffersBase");
4220 return;
4221 case GL_UNIFORM_BUFFER:
4222 bind_uniform_buffers(ctx, first, count, buffers, false, NULL, NULL,
4223 "glBindBuffersBase");
4224 return;
4225 case GL_SHADER_STORAGE_BUFFER:
4226 bind_shader_storage_buffers(ctx, first, count, buffers, false, NULL, NULL,
4227 "glBindBuffersBase");
4228 return;
4229 case GL_ATOMIC_COUNTER_BUFFER:
4230 bind_atomic_buffers(ctx, first, count, buffers, false, NULL, NULL,
4231 "glBindBuffersBase");
4232 return;
4233 default:
4234 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersBase(target=%s)",
4235 _mesa_enum_to_string(target));
4236 break;
4237 }
4238 }
4239
4240 static ALWAYS_INLINE void
4241 invalidate_buffer_subdata(struct gl_context *ctx,
4242 struct gl_buffer_object *bufObj, GLintptr offset,
4243 GLsizeiptr length)
4244 {
4245 if (ctx->Driver.InvalidateBufferSubData)
4246 ctx->Driver.InvalidateBufferSubData(ctx, bufObj, offset, length);
4247 }
4248
4249 void GLAPIENTRY
4250 _mesa_InvalidateBufferSubData_no_error(GLuint buffer, GLintptr offset,
4251 GLsizeiptr length)
4252 {
4253 GET_CURRENT_CONTEXT(ctx);
4254
4255 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4256 invalidate_buffer_subdata(ctx, bufObj, offset, length);
4257 }
4258
4259 void GLAPIENTRY
4260 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
4261 GLsizeiptr length)
4262 {
4263 GET_CURRENT_CONTEXT(ctx);
4264 struct gl_buffer_object *bufObj;
4265 const GLintptr end = offset + length;
4266
4267 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4268 * Profile) spec says:
4269 *
4270 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4271 * name of an existing buffer object."
4272 */
4273 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4274 if (!bufObj || bufObj == &DummyBufferObject) {
4275 _mesa_error(ctx, GL_INVALID_VALUE,
4276 "glInvalidateBufferSubData(name = %u) invalid object",
4277 buffer);
4278 return;
4279 }
4280
4281 /* The GL_ARB_invalidate_subdata spec says:
4282 *
4283 * "An INVALID_VALUE error is generated if <offset> or <length> is
4284 * negative, or if <offset> + <length> is greater than the value of
4285 * BUFFER_SIZE."
4286 */
4287 if (offset < 0 || length < 0 || end > bufObj->Size) {
4288 _mesa_error(ctx, GL_INVALID_VALUE,
4289 "glInvalidateBufferSubData(invalid offset or length)");
4290 return;
4291 }
4292
4293 /* The OpenGL 4.4 (Core Profile) spec says:
4294 *
4295 * "An INVALID_OPERATION error is generated if buffer is currently
4296 * mapped by MapBuffer or if the invalidate range intersects the range
4297 * currently mapped by MapBufferRange, unless it was mapped
4298 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4299 */
4300 if (!(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) &&
4301 bufferobj_range_mapped(bufObj, offset, length)) {
4302 _mesa_error(ctx, GL_INVALID_OPERATION,
4303 "glInvalidateBufferSubData(intersection with mapped "
4304 "range)");
4305 return;
4306 }
4307
4308 invalidate_buffer_subdata(ctx, bufObj, offset, length);
4309 }
4310
4311 void GLAPIENTRY
4312 _mesa_InvalidateBufferData(GLuint buffer)
4313 {
4314 GET_CURRENT_CONTEXT(ctx);
4315 struct gl_buffer_object *bufObj;
4316
4317 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4318 * Profile) spec says:
4319 *
4320 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4321 * name of an existing buffer object."
4322 */
4323 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4324 if (!bufObj || bufObj == &DummyBufferObject) {
4325 _mesa_error(ctx, GL_INVALID_VALUE,
4326 "glInvalidateBufferData(name = %u) invalid object",
4327 buffer);
4328 return;
4329 }
4330
4331 /* The OpenGL 4.4 (Core Profile) spec says:
4332 *
4333 * "An INVALID_OPERATION error is generated if buffer is currently
4334 * mapped by MapBuffer or if the invalidate range intersects the range
4335 * currently mapped by MapBufferRange, unless it was mapped
4336 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4337 */
4338 if (_mesa_check_disallowed_mapping(bufObj)) {
4339 _mesa_error(ctx, GL_INVALID_OPERATION,
4340 "glInvalidateBufferData(intersection with mapped "
4341 "range)");
4342 return;
4343 }
4344
4345 invalidate_buffer_subdata(ctx, bufObj, 0, bufObj->Size);
4346 }
4347
4348 static void
4349 buffer_page_commitment(struct gl_context *ctx,
4350 struct gl_buffer_object *bufferObj,
4351 GLintptr offset, GLsizeiptr size,
4352 GLboolean commit, const char *func)
4353 {
4354 if (!(bufferObj->StorageFlags & GL_SPARSE_STORAGE_BIT_ARB)) {
4355 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(not a sparse buffer object)",
4356 func);
4357 return;
4358 }
4359
4360 if (size < 0 || size > bufferObj->Size ||
4361 offset < 0 || offset > bufferObj->Size - size) {
4362 _mesa_error(ctx, GL_INVALID_VALUE, "%s(out of bounds)",
4363 func);
4364 return;
4365 }
4366
4367 /* The GL_ARB_sparse_buffer extension specification says:
4368 *
4369 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4370 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4371 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4372 * not extend to the end of the buffer's data store."
4373 */
4374 if (offset % ctx->Const.SparseBufferPageSize != 0) {
4375 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset not aligned to page size)",
4376 func);
4377 return;
4378 }
4379
4380 if (size % ctx->Const.SparseBufferPageSize != 0 &&
4381 offset + size != bufferObj->Size) {
4382 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size not aligned to page size)",
4383 func);
4384 return;
4385 }
4386
4387 ctx->Driver.BufferPageCommitment(ctx, bufferObj, offset, size, commit);
4388 }
4389
4390 void GLAPIENTRY
4391 _mesa_BufferPageCommitmentARB(GLenum target, GLintptr offset, GLsizeiptr size,
4392 GLboolean commit)
4393 {
4394 GET_CURRENT_CONTEXT(ctx);
4395 struct gl_buffer_object *bufferObj;
4396
4397 bufferObj = get_buffer(ctx, "glBufferPageCommitmentARB", target,
4398 GL_INVALID_ENUM);
4399 if (!bufferObj)
4400 return;
4401
4402 buffer_page_commitment(ctx, bufferObj, offset, size, commit,
4403 "glBufferPageCommitmentARB");
4404 }
4405
4406 void GLAPIENTRY
4407 _mesa_NamedBufferPageCommitmentARB(GLuint buffer, GLintptr offset,
4408 GLsizeiptr size, GLboolean commit)
4409 {
4410 GET_CURRENT_CONTEXT(ctx);
4411 struct gl_buffer_object *bufferObj;
4412
4413 bufferObj = _mesa_lookup_bufferobj(ctx, buffer);
4414 if (!bufferObj || bufferObj == &DummyBufferObject) {
4415 /* Note: the extension spec is not clear about the excpected error value. */
4416 _mesa_error(ctx, GL_INVALID_VALUE,
4417 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4418 buffer);
4419 return;
4420 }
4421
4422 buffer_page_commitment(ctx, bufferObj, offset, size, commit,
4423 "glNamedBufferPageCommitmentARB");
4424 }