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