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