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