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