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