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