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