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