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