mesa: Fix warning with MESA_VERBOSE=api for BindBufferRange
[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
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 }
2844
2845 /**
2846 * Bind a region of a buffer object to a shader storage block binding point.
2847 * \param index the shader storage buffer binding point index
2848 * \param bufObj the buffer object
2849 * \param offset offset to the start of buffer object region
2850 * \param size size of the buffer object region
2851 */
2852 static void
2853 bind_buffer_range_shader_storage_buffer(struct gl_context *ctx,
2854 GLuint index,
2855 struct gl_buffer_object *bufObj,
2856 GLintptr offset,
2857 GLsizeiptr size)
2858 {
2859 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
2860 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
2861 return;
2862 }
2863
2864 if (offset & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
2865 _mesa_error(ctx, GL_INVALID_VALUE,
2866 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
2867 ctx->Const.ShaderStorageBufferOffsetAlignment);
2868 return;
2869 }
2870
2871 if (bufObj == ctx->Shared->NullBufferObj) {
2872 offset = -1;
2873 size = -1;
2874 }
2875
2876 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
2877 bind_shader_storage_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
2878 }
2879
2880 /**
2881 * Bind a buffer object to a uniform block binding point.
2882 * As above, but offset = 0.
2883 */
2884 static void
2885 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
2886 GLuint index,
2887 struct gl_buffer_object *bufObj)
2888 {
2889 if (index >= ctx->Const.MaxUniformBufferBindings) {
2890 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2891 return;
2892 }
2893
2894 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2895
2896 if (bufObj == ctx->Shared->NullBufferObj)
2897 bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
2898 else
2899 bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
2900 }
2901
2902 /**
2903 * Bind a buffer object to a shader storage block binding point.
2904 * As above, but offset = 0.
2905 */
2906 static void
2907 bind_buffer_base_shader_storage_buffer(struct gl_context *ctx,
2908 GLuint index,
2909 struct gl_buffer_object *bufObj)
2910 {
2911 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
2912 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2913 return;
2914 }
2915
2916 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
2917
2918 if (bufObj == ctx->Shared->NullBufferObj)
2919 bind_shader_storage_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
2920 else
2921 bind_shader_storage_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
2922 }
2923
2924 /**
2925 * Binds a buffer object to an atomic buffer binding point.
2926 *
2927 * The caller is responsible for validating the offset,
2928 * flushing the vertices and updating NewDriverState.
2929 */
2930 static void
2931 set_atomic_buffer_binding(struct gl_context *ctx,
2932 struct gl_atomic_buffer_binding *binding,
2933 struct gl_buffer_object *bufObj,
2934 GLintptr offset,
2935 GLsizeiptr size)
2936 {
2937 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2938
2939 if (bufObj == ctx->Shared->NullBufferObj) {
2940 binding->Offset = -1;
2941 binding->Size = -1;
2942 } else {
2943 binding->Offset = offset;
2944 binding->Size = size;
2945 bufObj->UsageHistory |= USAGE_ATOMIC_COUNTER_BUFFER;
2946 }
2947 }
2948
2949 /**
2950 * Binds a buffer object to an atomic buffer binding point.
2951 *
2952 * Unlike set_atomic_buffer_binding(), this function also validates the
2953 * index and offset, flushes vertices, and updates NewDriverState.
2954 * It also checks if the binding has actually changing before
2955 * updating it.
2956 */
2957 static void
2958 bind_atomic_buffer(struct gl_context *ctx,
2959 unsigned index,
2960 struct gl_buffer_object *bufObj,
2961 GLintptr offset,
2962 GLsizeiptr size,
2963 const char *name)
2964 {
2965 struct gl_atomic_buffer_binding *binding;
2966
2967 if (index >= ctx->Const.MaxAtomicBufferBindings) {
2968 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
2969 return;
2970 }
2971
2972 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
2973 _mesa_error(ctx, GL_INVALID_VALUE,
2974 "%s(offset misaligned %d/%d)", name, (int) offset,
2975 ATOMIC_COUNTER_SIZE);
2976 return;
2977 }
2978
2979 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
2980
2981 binding = &ctx->AtomicBufferBindings[index];
2982 if (binding->BufferObject == bufObj &&
2983 binding->Offset == offset &&
2984 binding->Size == size) {
2985 return;
2986 }
2987
2988 FLUSH_VERTICES(ctx, 0);
2989 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
2990
2991 set_atomic_buffer_binding(ctx, binding, bufObj, offset, size);
2992 }
2993
2994 static inline bool
2995 bind_buffers_check_offset_and_size(struct gl_context *ctx,
2996 GLuint index,
2997 const GLintptr *offsets,
2998 const GLsizeiptr *sizes)
2999 {
3000 if (offsets[index] < 0) {
3001 /* The ARB_multi_bind spec says:
3002 *
3003 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3004 * value in <offsets> is less than zero (per binding)."
3005 */
3006 _mesa_error(ctx, GL_INVALID_VALUE,
3007 "glBindBuffersRange(offsets[%u]=%" PRId64 " < 0)",
3008 index, (int64_t) offsets[index]);
3009 return false;
3010 }
3011
3012 if (sizes[index] <= 0) {
3013 /* The ARB_multi_bind spec says:
3014 *
3015 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3016 * value in <sizes> is less than or equal to zero (per binding)."
3017 */
3018 _mesa_error(ctx, GL_INVALID_VALUE,
3019 "glBindBuffersRange(sizes[%u]=%" PRId64 " <= 0)",
3020 index, (int64_t) sizes[index]);
3021 return false;
3022 }
3023
3024 return true;
3025 }
3026
3027 static bool
3028 error_check_bind_uniform_buffers(struct gl_context *ctx,
3029 GLuint first, GLsizei count,
3030 const char *caller)
3031 {
3032 if (!ctx->Extensions.ARB_uniform_buffer_object) {
3033 _mesa_error(ctx, GL_INVALID_ENUM,
3034 "%s(target=GL_UNIFORM_BUFFER)", caller);
3035 return false;
3036 }
3037
3038 /* The ARB_multi_bind_spec says:
3039 *
3040 * "An INVALID_OPERATION error is generated if <first> + <count> is
3041 * greater than the number of target-specific indexed binding points,
3042 * as described in section 6.7.1."
3043 */
3044 if (first + count > ctx->Const.MaxUniformBufferBindings) {
3045 _mesa_error(ctx, GL_INVALID_OPERATION,
3046 "%s(first=%u + count=%d > the value of "
3047 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3048 caller, first, count,
3049 ctx->Const.MaxUniformBufferBindings);
3050 return false;
3051 }
3052
3053 return true;
3054 }
3055
3056 static bool
3057 error_check_bind_shader_storage_buffers(struct gl_context *ctx,
3058 GLuint first, GLsizei count,
3059 const char *caller)
3060 {
3061 if (!ctx->Extensions.ARB_shader_storage_buffer_object) {
3062 _mesa_error(ctx, GL_INVALID_ENUM,
3063 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller);
3064 return false;
3065 }
3066
3067 /* The ARB_multi_bind_spec says:
3068 *
3069 * "An INVALID_OPERATION error is generated if <first> + <count> is
3070 * greater than the number of target-specific indexed binding points,
3071 * as described in section 6.7.1."
3072 */
3073 if (first + count > ctx->Const.MaxShaderStorageBufferBindings) {
3074 _mesa_error(ctx, GL_INVALID_OPERATION,
3075 "%s(first=%u + count=%d > the value of "
3076 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3077 caller, first, count,
3078 ctx->Const.MaxShaderStorageBufferBindings);
3079 return false;
3080 }
3081
3082 return true;
3083 }
3084
3085 /**
3086 * Unbind all uniform buffers in the range
3087 * <first> through <first>+<count>-1
3088 */
3089 static void
3090 unbind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3091 {
3092 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
3093 GLint i;
3094
3095 for (i = 0; i < count; i++)
3096 set_ubo_binding(ctx, &ctx->UniformBufferBindings[first + i],
3097 bufObj, -1, -1, GL_TRUE);
3098 }
3099
3100 /**
3101 * Unbind all shader storage buffers in the range
3102 * <first> through <first>+<count>-1
3103 */
3104 static void
3105 unbind_shader_storage_buffers(struct gl_context *ctx, GLuint first,
3106 GLsizei count)
3107 {
3108 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
3109 GLint i;
3110
3111 for (i = 0; i < count; i++)
3112 set_ssbo_binding(ctx, &ctx->ShaderStorageBufferBindings[first + i],
3113 bufObj, -1, -1, GL_TRUE);
3114 }
3115
3116 static void
3117 bind_uniform_buffers_base(struct gl_context *ctx, GLuint first, GLsizei count,
3118 const GLuint *buffers)
3119 {
3120 GLint i;
3121
3122 if (!error_check_bind_uniform_buffers(ctx, first, count, "glBindBuffersBase"))
3123 return;
3124
3125 /* Assume that at least one binding will be changed */
3126 FLUSH_VERTICES(ctx, 0);
3127 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3128
3129 if (!buffers) {
3130 /* The ARB_multi_bind spec says:
3131 *
3132 * "If <buffers> is NULL, all bindings from <first> through
3133 * <first>+<count>-1 are reset to their unbound (zero) state."
3134 */
3135 unbind_uniform_buffers(ctx, first, count);
3136 return;
3137 }
3138
3139 /* Note that the error semantics for multi-bind commands differ from
3140 * those of other GL commands.
3141 *
3142 * The Issues section in the ARB_multi_bind spec says:
3143 *
3144 * "(11) Typically, OpenGL specifies that if an error is generated by a
3145 * command, that command has no effect. This is somewhat
3146 * unfortunate for multi-bind commands, because it would require a
3147 * first pass to scan the entire list of bound objects for errors
3148 * and then a second pass to actually perform the bindings.
3149 * Should we have different error semantics?
3150 *
3151 * RESOLVED: Yes. In this specification, when the parameters for
3152 * one of the <count> binding points are invalid, that binding point
3153 * is not updated and an error will be generated. However, other
3154 * binding points in the same command will be updated if their
3155 * parameters are valid and no other error occurs."
3156 */
3157
3158 _mesa_begin_bufferobj_lookups(ctx);
3159
3160 for (i = 0; i < count; i++) {
3161 struct gl_uniform_buffer_binding *binding =
3162 &ctx->UniformBufferBindings[first + i];
3163 struct gl_buffer_object *bufObj;
3164
3165 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3166 bufObj = binding->BufferObject;
3167 else
3168 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3169 "glBindBuffersBase");
3170
3171 if (bufObj) {
3172 if (bufObj == ctx->Shared->NullBufferObj)
3173 set_ubo_binding(ctx, binding, bufObj, -1, -1, GL_TRUE);
3174 else
3175 set_ubo_binding(ctx, binding, bufObj, 0, 0, GL_TRUE);
3176 }
3177 }
3178
3179 _mesa_end_bufferobj_lookups(ctx);
3180 }
3181
3182 static void
3183 bind_shader_storage_buffers_base(struct gl_context *ctx, GLuint first,
3184 GLsizei count, const GLuint *buffers)
3185 {
3186 GLint i;
3187
3188 if (!error_check_bind_shader_storage_buffers(ctx, first, count,
3189 "glBindBuffersBase"))
3190 return;
3191
3192 /* Assume that at least one binding will be changed */
3193 FLUSH_VERTICES(ctx, 0);
3194 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
3195
3196 if (!buffers) {
3197 /* The ARB_multi_bind spec says:
3198 *
3199 * "If <buffers> is NULL, all bindings from <first> through
3200 * <first>+<count>-1 are reset to their unbound (zero) state."
3201 */
3202 unbind_shader_storage_buffers(ctx, first, count);
3203 return;
3204 }
3205
3206 /* Note that the error semantics for multi-bind commands differ from
3207 * those of other GL commands.
3208 *
3209 * The Issues section in the ARB_multi_bind spec says:
3210 *
3211 * "(11) Typically, OpenGL specifies that if an error is generated by a
3212 * command, that command has no effect. This is somewhat
3213 * unfortunate for multi-bind commands, because it would require a
3214 * first pass to scan the entire list of bound objects for errors
3215 * and then a second pass to actually perform the bindings.
3216 * Should we have different error semantics?
3217 *
3218 * RESOLVED: Yes. In this specification, when the parameters for
3219 * one of the <count> binding points are invalid, that binding point
3220 * is not updated and an error will be generated. However, other
3221 * binding points in the same command will be updated if their
3222 * parameters are valid and no other error occurs."
3223 */
3224
3225 _mesa_begin_bufferobj_lookups(ctx);
3226
3227 for (i = 0; i < count; i++) {
3228 struct gl_shader_storage_buffer_binding *binding =
3229 &ctx->ShaderStorageBufferBindings[first + i];
3230 struct gl_buffer_object *bufObj;
3231
3232 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3233 bufObj = binding->BufferObject;
3234 else
3235 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3236 "glBindBuffersBase");
3237
3238 if (bufObj) {
3239 if (bufObj == ctx->Shared->NullBufferObj)
3240 set_ssbo_binding(ctx, binding, bufObj, -1, -1, GL_TRUE);
3241 else
3242 set_ssbo_binding(ctx, binding, bufObj, 0, 0, GL_TRUE);
3243 }
3244 }
3245
3246 _mesa_end_bufferobj_lookups(ctx);
3247 }
3248
3249 static void
3250 bind_uniform_buffers_range(struct gl_context *ctx, GLuint first, GLsizei count,
3251 const GLuint *buffers,
3252 const GLintptr *offsets, const GLsizeiptr *sizes)
3253 {
3254 GLint i;
3255
3256 if (!error_check_bind_uniform_buffers(ctx, first, count,
3257 "glBindBuffersRange"))
3258 return;
3259
3260 /* Assume that at least one binding will be changed */
3261 FLUSH_VERTICES(ctx, 0);
3262 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3263
3264 if (!buffers) {
3265 /* The ARB_multi_bind spec says:
3266 *
3267 * "If <buffers> is NULL, all bindings from <first> through
3268 * <first>+<count>-1 are reset to their unbound (zero) state.
3269 * In this case, the offsets and sizes associated with the
3270 * binding points are set to default values, ignoring
3271 * <offsets> and <sizes>."
3272 */
3273 unbind_uniform_buffers(ctx, first, count);
3274 return;
3275 }
3276
3277 /* Note that the error semantics for multi-bind commands differ from
3278 * those of other GL commands.
3279 *
3280 * The Issues section in the ARB_multi_bind spec says:
3281 *
3282 * "(11) Typically, OpenGL specifies that if an error is generated by a
3283 * command, that command has no effect. This is somewhat
3284 * unfortunate for multi-bind commands, because it would require a
3285 * first pass to scan the entire list of bound objects for errors
3286 * and then a second pass to actually perform the bindings.
3287 * Should we have different error semantics?
3288 *
3289 * RESOLVED: Yes. In this specification, when the parameters for
3290 * one of the <count> binding points are invalid, that binding point
3291 * is not updated and an error will be generated. However, other
3292 * binding points in the same command will be updated if their
3293 * parameters are valid and no other error occurs."
3294 */
3295
3296 _mesa_begin_bufferobj_lookups(ctx);
3297
3298 for (i = 0; i < count; i++) {
3299 struct gl_uniform_buffer_binding *binding =
3300 &ctx->UniformBufferBindings[first + i];
3301 struct gl_buffer_object *bufObj;
3302
3303 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3304 continue;
3305
3306 /* The ARB_multi_bind spec says:
3307 *
3308 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3309 * pair of values in <offsets> and <sizes> does not respectively
3310 * satisfy the constraints described for those parameters for the
3311 * specified target, as described in section 6.7.1 (per binding)."
3312 *
3313 * Section 6.7.1 refers to table 6.5, which says:
3314 *
3315 * "┌───────────────────────────────────────────────────────────────┐
3316 * │ Uniform buffer array bindings (see sec. 7.6) │
3317 * ├─────────────────────┬─────────────────────────────────────────┤
3318 * │ ... │ ... │
3319 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3320 * │ │ OFFSET_ALIGNMENT │
3321 * │ ... │ ... │
3322 * │ size restriction │ none │
3323 * └─────────────────────┴─────────────────────────────────────────┘"
3324 */
3325 if (offsets[i] & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3326 _mesa_error(ctx, GL_INVALID_VALUE,
3327 "glBindBuffersRange(offsets[%u]=%" PRId64
3328 " is misaligned; it must be a multiple of the value of "
3329 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3330 "target=GL_UNIFORM_BUFFER)",
3331 i, (int64_t) offsets[i],
3332 ctx->Const.UniformBufferOffsetAlignment);
3333 continue;
3334 }
3335
3336 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3337 bufObj = binding->BufferObject;
3338 else
3339 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3340 "glBindBuffersRange");
3341
3342 if (bufObj) {
3343 if (bufObj == ctx->Shared->NullBufferObj)
3344 set_ubo_binding(ctx, binding, bufObj, -1, -1, GL_FALSE);
3345 else
3346 set_ubo_binding(ctx, binding, bufObj,
3347 offsets[i], sizes[i], GL_FALSE);
3348 }
3349 }
3350
3351 _mesa_end_bufferobj_lookups(ctx);
3352 }
3353
3354 static void
3355 bind_shader_storage_buffers_range(struct gl_context *ctx, GLuint first,
3356 GLsizei count, const GLuint *buffers,
3357 const GLintptr *offsets,
3358 const GLsizeiptr *sizes)
3359 {
3360 GLint i;
3361
3362 if (!error_check_bind_shader_storage_buffers(ctx, first, count,
3363 "glBindBuffersRange"))
3364 return;
3365
3366 /* Assume that at least one binding will be changed */
3367 FLUSH_VERTICES(ctx, 0);
3368 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
3369
3370 if (!buffers) {
3371 /* The ARB_multi_bind spec says:
3372 *
3373 * "If <buffers> is NULL, all bindings from <first> through
3374 * <first>+<count>-1 are reset to their unbound (zero) state.
3375 * In this case, the offsets and sizes associated with the
3376 * binding points are set to default values, ignoring
3377 * <offsets> and <sizes>."
3378 */
3379 unbind_shader_storage_buffers(ctx, first, count);
3380 return;
3381 }
3382
3383 /* Note that the error semantics for multi-bind commands differ from
3384 * those of other GL commands.
3385 *
3386 * The Issues section in the ARB_multi_bind spec says:
3387 *
3388 * "(11) Typically, OpenGL specifies that if an error is generated by a
3389 * command, that command has no effect. This is somewhat
3390 * unfortunate for multi-bind commands, because it would require a
3391 * first pass to scan the entire list of bound objects for errors
3392 * and then a second pass to actually perform the bindings.
3393 * Should we have different error semantics?
3394 *
3395 * RESOLVED: Yes. In this specification, when the parameters for
3396 * one of the <count> binding points are invalid, that binding point
3397 * is not updated and an error will be generated. However, other
3398 * binding points in the same command will be updated if their
3399 * parameters are valid and no other error occurs."
3400 */
3401
3402 _mesa_begin_bufferobj_lookups(ctx);
3403
3404 for (i = 0; i < count; i++) {
3405 struct gl_shader_storage_buffer_binding *binding =
3406 &ctx->ShaderStorageBufferBindings[first + i];
3407 struct gl_buffer_object *bufObj;
3408
3409 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3410 continue;
3411
3412 /* The ARB_multi_bind spec says:
3413 *
3414 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3415 * pair of values in <offsets> and <sizes> does not respectively
3416 * satisfy the constraints described for those parameters for the
3417 * specified target, as described in section 6.7.1 (per binding)."
3418 *
3419 * Section 6.7.1 refers to table 6.5, which says:
3420 *
3421 * "┌───────────────────────────────────────────────────────────────┐
3422 * │ Shader storage buffer array bindings (see sec. 7.8) │
3423 * ├─────────────────────┬─────────────────────────────────────────┤
3424 * │ ... │ ... │
3425 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3426 * │ │ BUFFER_OFFSET_ALIGNMENT │
3427 * │ ... │ ... │
3428 * │ size restriction │ none │
3429 * └─────────────────────┴─────────────────────────────────────────┘"
3430 */
3431 if (offsets[i] & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
3432 _mesa_error(ctx, GL_INVALID_VALUE,
3433 "glBindBuffersRange(offsets[%u]=%" PRId64
3434 " is misaligned; it must be a multiple of the value of "
3435 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3436 "target=GL_SHADER_STORAGE_BUFFER)",
3437 i, (int64_t) offsets[i],
3438 ctx->Const.ShaderStorageBufferOffsetAlignment);
3439 continue;
3440 }
3441
3442 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3443 bufObj = binding->BufferObject;
3444 else
3445 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3446 "glBindBuffersRange");
3447
3448 if (bufObj) {
3449 if (bufObj == ctx->Shared->NullBufferObj)
3450 set_ssbo_binding(ctx, binding, bufObj, -1, -1, GL_FALSE);
3451 else
3452 set_ssbo_binding(ctx, binding, bufObj,
3453 offsets[i], sizes[i], GL_FALSE);
3454 }
3455 }
3456
3457 _mesa_end_bufferobj_lookups(ctx);
3458 }
3459
3460 static bool
3461 error_check_bind_xfb_buffers(struct gl_context *ctx,
3462 struct gl_transform_feedback_object *tfObj,
3463 GLuint first, GLsizei count, const char *caller)
3464 {
3465 if (!ctx->Extensions.EXT_transform_feedback) {
3466 _mesa_error(ctx, GL_INVALID_ENUM,
3467 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller);
3468 return false;
3469 }
3470
3471 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3472 *
3473 * "An INVALID_OPERATION error is generated :
3474 *
3475 * ...
3476 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3477 * FEEDBACK_BUFFER and transform feedback is currently active."
3478 *
3479 * We assume that this is also meant to apply to BindBuffersRange
3480 * and BindBuffersBase.
3481 */
3482 if (tfObj->Active) {
3483 _mesa_error(ctx, GL_INVALID_OPERATION,
3484 "%s(Changing transform feedback buffers while "
3485 "transform feedback is active)", caller);
3486 return false;
3487 }
3488
3489 /* The ARB_multi_bind_spec says:
3490 *
3491 * "An INVALID_OPERATION error is generated if <first> + <count> is
3492 * greater than the number of target-specific indexed binding points,
3493 * as described in section 6.7.1."
3494 */
3495 if (first + count > ctx->Const.MaxTransformFeedbackBuffers) {
3496 _mesa_error(ctx, GL_INVALID_OPERATION,
3497 "%s(first=%u + count=%d > the value of "
3498 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3499 caller, first, count,
3500 ctx->Const.MaxTransformFeedbackBuffers);
3501 return false;
3502 }
3503
3504 return true;
3505 }
3506
3507 /**
3508 * Unbind all transform feedback buffers in the range
3509 * <first> through <first>+<count>-1
3510 */
3511 static void
3512 unbind_xfb_buffers(struct gl_context *ctx,
3513 struct gl_transform_feedback_object *tfObj,
3514 GLuint first, GLsizei count)
3515 {
3516 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3517 GLint i;
3518
3519 for (i = 0; i < count; i++)
3520 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
3521 bufObj, 0, 0);
3522 }
3523
3524 static void
3525 bind_xfb_buffers_base(struct gl_context *ctx,
3526 GLuint first, GLsizei count,
3527 const GLuint *buffers)
3528 {
3529 struct gl_transform_feedback_object *tfObj =
3530 ctx->TransformFeedback.CurrentObject;
3531 GLint i;
3532
3533 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count,
3534 "glBindBuffersBase"))
3535 return;
3536
3537 /* Assume that at least one binding will be changed */
3538 FLUSH_VERTICES(ctx, 0);
3539 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
3540
3541 if (!buffers) {
3542 /* The ARB_multi_bind spec says:
3543 *
3544 * "If <buffers> is NULL, all bindings from <first> through
3545 * <first>+<count>-1 are reset to their unbound (zero) state."
3546 */
3547 unbind_xfb_buffers(ctx, tfObj, first, count);
3548 return;
3549 }
3550
3551 /* Note that the error semantics for multi-bind commands differ from
3552 * those of other GL commands.
3553 *
3554 * The Issues section in the ARB_multi_bind spec says:
3555 *
3556 * "(11) Typically, OpenGL specifies that if an error is generated by a
3557 * command, that command has no effect. This is somewhat
3558 * unfortunate for multi-bind commands, because it would require a
3559 * first pass to scan the entire list of bound objects for errors
3560 * and then a second pass to actually perform the bindings.
3561 * Should we have different error semantics?
3562 *
3563 * RESOLVED: Yes. In this specification, when the parameters for
3564 * one of the <count> binding points are invalid, that binding point
3565 * is not updated and an error will be generated. However, other
3566 * binding points in the same command will be updated if their
3567 * parameters are valid and no other error occurs."
3568 */
3569
3570 _mesa_begin_bufferobj_lookups(ctx);
3571
3572 for (i = 0; i < count; i++) {
3573 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[first + i];
3574 struct gl_buffer_object *bufObj;
3575
3576 if (boundBufObj && boundBufObj->Name == buffers[i])
3577 bufObj = boundBufObj;
3578 else
3579 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3580 "glBindBuffersBase");
3581
3582 if (bufObj)
3583 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
3584 bufObj, 0, 0);
3585 }
3586
3587 _mesa_end_bufferobj_lookups(ctx);
3588 }
3589
3590 static void
3591 bind_xfb_buffers_range(struct gl_context *ctx,
3592 GLuint first, GLsizei count,
3593 const GLuint *buffers,
3594 const GLintptr *offsets,
3595 const GLsizeiptr *sizes)
3596 {
3597 struct gl_transform_feedback_object *tfObj =
3598 ctx->TransformFeedback.CurrentObject;
3599 GLint i;
3600
3601 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count,
3602 "glBindBuffersRange"))
3603 return;
3604
3605 /* Assume that at least one binding will be changed */
3606 FLUSH_VERTICES(ctx, 0);
3607 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
3608
3609 if (!buffers) {
3610 /* The ARB_multi_bind spec says:
3611 *
3612 * "If <buffers> is NULL, all bindings from <first> through
3613 * <first>+<count>-1 are reset to their unbound (zero) state.
3614 * In this case, the offsets and sizes associated with the
3615 * binding points are set to default values, ignoring
3616 * <offsets> and <sizes>."
3617 */
3618 unbind_xfb_buffers(ctx, tfObj, first, count);
3619 return;
3620 }
3621
3622 /* Note that the error semantics for multi-bind commands differ from
3623 * those of other GL commands.
3624 *
3625 * The Issues section in the ARB_multi_bind spec says:
3626 *
3627 * "(11) Typically, OpenGL specifies that if an error is generated by a
3628 * command, that command has no effect. This is somewhat
3629 * unfortunate for multi-bind commands, because it would require a
3630 * first pass to scan the entire list of bound objects for errors
3631 * and then a second pass to actually perform the bindings.
3632 * Should we have different error semantics?
3633 *
3634 * RESOLVED: Yes. In this specification, when the parameters for
3635 * one of the <count> binding points are invalid, that binding point
3636 * is not updated and an error will be generated. However, other
3637 * binding points in the same command will be updated if their
3638 * parameters are valid and no other error occurs."
3639 */
3640
3641 _mesa_begin_bufferobj_lookups(ctx);
3642
3643 for (i = 0; i < count; i++) {
3644 const GLuint index = first + i;
3645 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[index];
3646 struct gl_buffer_object *bufObj;
3647
3648 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3649 continue;
3650
3651 /* The ARB_multi_bind spec says:
3652 *
3653 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3654 * pair of values in <offsets> and <sizes> does not respectively
3655 * satisfy the constraints described for those parameters for the
3656 * specified target, as described in section 6.7.1 (per binding)."
3657 *
3658 * Section 6.7.1 refers to table 6.5, which says:
3659 *
3660 * "┌───────────────────────────────────────────────────────────────┐
3661 * │ Transform feedback array bindings (see sec. 13.2.2) │
3662 * ├───────────────────────┬───────────────────────────────────────┤
3663 * │ ... │ ... │
3664 * │ offset restriction │ multiple of 4 │
3665 * │ ... │ ... │
3666 * │ size restriction │ multiple of 4 │
3667 * └───────────────────────┴───────────────────────────────────────┘"
3668 */
3669 if (offsets[i] & 0x3) {
3670 _mesa_error(ctx, GL_INVALID_VALUE,
3671 "glBindBuffersRange(offsets[%u]=%" PRId64
3672 " is misaligned; it must be a multiple of 4 when "
3673 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3674 i, (int64_t) offsets[i]);
3675 continue;
3676 }
3677
3678 if (sizes[i] & 0x3) {
3679 _mesa_error(ctx, GL_INVALID_VALUE,
3680 "glBindBuffersRange(sizes[%u]=%" PRId64
3681 " is misaligned; it must be a multiple of 4 when "
3682 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3683 i, (int64_t) sizes[i]);
3684 continue;
3685 }
3686
3687 if (boundBufObj && boundBufObj->Name == buffers[i])
3688 bufObj = boundBufObj;
3689 else
3690 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3691 "glBindBuffersRange");
3692
3693 if (bufObj)
3694 _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj,
3695 offsets[i], sizes[i]);
3696 }
3697
3698 _mesa_end_bufferobj_lookups(ctx);
3699 }
3700
3701 static bool
3702 error_check_bind_atomic_buffers(struct gl_context *ctx,
3703 GLuint first, GLsizei count,
3704 const char *caller)
3705 {
3706 if (!ctx->Extensions.ARB_shader_atomic_counters) {
3707 _mesa_error(ctx, GL_INVALID_ENUM,
3708 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller);
3709 return false;
3710 }
3711
3712 /* The ARB_multi_bind_spec says:
3713 *
3714 * "An INVALID_OPERATION error is generated if <first> + <count> is
3715 * greater than the number of target-specific indexed binding points,
3716 * as described in section 6.7.1."
3717 */
3718 if (first + count > ctx->Const.MaxAtomicBufferBindings) {
3719 _mesa_error(ctx, GL_INVALID_OPERATION,
3720 "%s(first=%u + count=%d > the value of "
3721 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3722 caller, first, count, ctx->Const.MaxAtomicBufferBindings);
3723 return false;
3724 }
3725
3726 return true;
3727 }
3728
3729 /**
3730 * Unbind all atomic counter buffers in the range
3731 * <first> through <first>+<count>-1
3732 */
3733 static void
3734 unbind_atomic_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3735 {
3736 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3737 GLint i;
3738
3739 for (i = 0; i < count; i++)
3740 set_atomic_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i],
3741 bufObj, -1, -1);
3742 }
3743
3744 static void
3745 bind_atomic_buffers_base(struct gl_context *ctx,
3746 GLuint first,
3747 GLsizei count,
3748 const GLuint *buffers)
3749 {
3750 GLint i;
3751
3752 if (!error_check_bind_atomic_buffers(ctx, first, count,
3753 "glBindBuffersBase"))
3754 return;
3755
3756 /* Assume that at least one binding will be changed */
3757 FLUSH_VERTICES(ctx, 0);
3758 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3759
3760 if (!buffers) {
3761 /* The ARB_multi_bind spec says:
3762 *
3763 * "If <buffers> is NULL, all bindings from <first> through
3764 * <first>+<count>-1 are reset to their unbound (zero) state."
3765 */
3766 unbind_atomic_buffers(ctx, first, count);
3767 return;
3768 }
3769
3770 /* Note that the error semantics for multi-bind commands differ from
3771 * those of other GL commands.
3772 *
3773 * The Issues section in the ARB_multi_bind spec says:
3774 *
3775 * "(11) Typically, OpenGL specifies that if an error is generated by a
3776 * command, that command has no effect. This is somewhat
3777 * unfortunate for multi-bind commands, because it would require a
3778 * first pass to scan the entire list of bound objects for errors
3779 * and then a second pass to actually perform the bindings.
3780 * Should we have different error semantics?
3781 *
3782 * RESOLVED: Yes. In this specification, when the parameters for
3783 * one of the <count> binding points are invalid, that binding point
3784 * is not updated and an error will be generated. However, other
3785 * binding points in the same command will be updated if their
3786 * parameters are valid and no other error occurs."
3787 */
3788
3789 _mesa_begin_bufferobj_lookups(ctx);
3790
3791 for (i = 0; i < count; i++) {
3792 struct gl_atomic_buffer_binding *binding =
3793 &ctx->AtomicBufferBindings[first + i];
3794 struct gl_buffer_object *bufObj;
3795
3796 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3797 bufObj = binding->BufferObject;
3798 else
3799 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3800 "glBindBuffersBase");
3801
3802 if (bufObj)
3803 set_atomic_buffer_binding(ctx, binding, bufObj, 0, 0);
3804 }
3805
3806 _mesa_end_bufferobj_lookups(ctx);
3807 }
3808
3809 static void
3810 bind_atomic_buffers_range(struct gl_context *ctx,
3811 GLuint first,
3812 GLsizei count,
3813 const GLuint *buffers,
3814 const GLintptr *offsets,
3815 const GLsizeiptr *sizes)
3816 {
3817 GLint i;
3818
3819 if (!error_check_bind_atomic_buffers(ctx, first, count,
3820 "glBindBuffersRange"))
3821 return;
3822
3823 /* Assume that at least one binding will be changed */
3824 FLUSH_VERTICES(ctx, 0);
3825 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3826
3827 if (!buffers) {
3828 /* The ARB_multi_bind spec says:
3829 *
3830 * "If <buffers> is NULL, all bindings from <first> through
3831 * <first>+<count>-1 are reset to their unbound (zero) state.
3832 * In this case, the offsets and sizes associated with the
3833 * binding points are set to default values, ignoring
3834 * <offsets> and <sizes>."
3835 */
3836 unbind_atomic_buffers(ctx, first, count);
3837 return;
3838 }
3839
3840 /* Note that the error semantics for multi-bind commands differ from
3841 * those of other GL commands.
3842 *
3843 * The Issues section in the ARB_multi_bind spec says:
3844 *
3845 * "(11) Typically, OpenGL specifies that if an error is generated by a
3846 * command, that command has no effect. This is somewhat
3847 * unfortunate for multi-bind commands, because it would require a
3848 * first pass to scan the entire list of bound objects for errors
3849 * and then a second pass to actually perform the bindings.
3850 * Should we have different error semantics?
3851 *
3852 * RESOLVED: Yes. In this specification, when the parameters for
3853 * one of the <count> binding points are invalid, that binding point
3854 * is not updated and an error will be generated. However, other
3855 * binding points in the same command will be updated if their
3856 * parameters are valid and no other error occurs."
3857 */
3858
3859 _mesa_begin_bufferobj_lookups(ctx);
3860
3861 for (i = 0; i < count; i++) {
3862 struct gl_atomic_buffer_binding *binding =
3863 &ctx->AtomicBufferBindings[first + i];
3864 struct gl_buffer_object *bufObj;
3865
3866 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3867 continue;
3868
3869 /* The ARB_multi_bind spec says:
3870 *
3871 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3872 * pair of values in <offsets> and <sizes> does not respectively
3873 * satisfy the constraints described for those parameters for the
3874 * specified target, as described in section 6.7.1 (per binding)."
3875 *
3876 * Section 6.7.1 refers to table 6.5, which says:
3877 *
3878 * "┌───────────────────────────────────────────────────────────────┐
3879 * │ Atomic counter array bindings (see sec. 7.7.2) │
3880 * ├───────────────────────┬───────────────────────────────────────┤
3881 * │ ... │ ... │
3882 * │ offset restriction │ multiple of 4 │
3883 * │ ... │ ... │
3884 * │ size restriction │ none │
3885 * └───────────────────────┴───────────────────────────────────────┘"
3886 */
3887 if (offsets[i] & (ATOMIC_COUNTER_SIZE - 1)) {
3888 _mesa_error(ctx, GL_INVALID_VALUE,
3889 "glBindBuffersRange(offsets[%u]=%" PRId64
3890 " is misaligned; it must be a multiple of %d when "
3891 "target=GL_ATOMIC_COUNTER_BUFFER)",
3892 i, (int64_t) offsets[i], ATOMIC_COUNTER_SIZE);
3893 continue;
3894 }
3895
3896 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3897 bufObj = binding->BufferObject;
3898 else
3899 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3900 "glBindBuffersRange");
3901
3902 if (bufObj)
3903 set_atomic_buffer_binding(ctx, binding, bufObj, offsets[i], sizes[i]);
3904 }
3905
3906 _mesa_end_bufferobj_lookups(ctx);
3907 }
3908
3909 void GLAPIENTRY
3910 _mesa_BindBufferRange(GLenum target, GLuint index,
3911 GLuint buffer, GLintptr offset, GLsizeiptr size)
3912 {
3913 GET_CURRENT_CONTEXT(ctx);
3914 struct gl_buffer_object *bufObj;
3915
3916 if (MESA_VERBOSE & VERBOSE_API) {
3917 _mesa_debug(ctx, "glBindBufferRange(%s, %u, %u, %ld, %ld)\n",
3918 _mesa_enum_to_string(target), index, buffer, offset, size);
3919 }
3920
3921 if (buffer == 0) {
3922 bufObj = ctx->Shared->NullBufferObj;
3923 } else {
3924 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3925 }
3926 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
3927 &bufObj, "glBindBufferRange"))
3928 return;
3929
3930 if (!bufObj) {
3931 _mesa_error(ctx, GL_INVALID_OPERATION,
3932 "glBindBufferRange(invalid buffer=%u)", buffer);
3933 return;
3934 }
3935
3936 if (buffer != 0) {
3937 if (size <= 0) {
3938 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
3939 (int) size);
3940 return;
3941 }
3942 }
3943
3944 switch (target) {
3945 case GL_TRANSFORM_FEEDBACK_BUFFER:
3946 _mesa_bind_buffer_range_transform_feedback(ctx,
3947 ctx->TransformFeedback.CurrentObject,
3948 index, bufObj, offset, size,
3949 false);
3950 return;
3951 case GL_UNIFORM_BUFFER:
3952 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
3953 return;
3954 case GL_SHADER_STORAGE_BUFFER:
3955 bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset, size);
3956 return;
3957 case GL_ATOMIC_COUNTER_BUFFER:
3958 bind_atomic_buffer(ctx, index, bufObj, offset, size,
3959 "glBindBufferRange");
3960 return;
3961 default:
3962 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
3963 return;
3964 }
3965 }
3966
3967 void GLAPIENTRY
3968 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
3969 {
3970 GET_CURRENT_CONTEXT(ctx);
3971 struct gl_buffer_object *bufObj;
3972
3973 if (MESA_VERBOSE & VERBOSE_API) {
3974 _mesa_debug(ctx, "glBindBufferBase(%s, %u, %u)\n",
3975 _mesa_enum_to_string(target), index, buffer);
3976 }
3977
3978 if (buffer == 0) {
3979 bufObj = ctx->Shared->NullBufferObj;
3980 } else {
3981 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3982 }
3983 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
3984 &bufObj, "glBindBufferBase"))
3985 return;
3986
3987 if (!bufObj) {
3988 _mesa_error(ctx, GL_INVALID_OPERATION,
3989 "glBindBufferBase(invalid buffer=%u)", buffer);
3990 return;
3991 }
3992
3993 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3994 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3995 *
3996 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3997 * zero and size equal to the size of buffer."
3998 *
3999 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4000 *
4001 * "If the parameter (starting offset or size) was not specified when the
4002 * buffer object was bound, zero is returned."
4003 *
4004 * What happens if the size of the buffer changes? Does the size of the
4005 * buffer at the moment glBindBufferBase was called still play a role, like
4006 * the first quote would imply, or is the size meaningless in the
4007 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4008 * core spec page 45 says:
4009 *
4010 * "It is equivalent to calling BindBufferRange with offset zero, while
4011 * size is determined by the size of the bound buffer at the time the
4012 * binding is used."
4013 *
4014 * My interpretation is that the GL 4.1 spec was a clarification of the
4015 * behavior, not a change. In particular, this choice will only make
4016 * rendering work in cases where it would have had undefined results.
4017 */
4018
4019 switch (target) {
4020 case GL_TRANSFORM_FEEDBACK_BUFFER:
4021 _mesa_bind_buffer_base_transform_feedback(ctx,
4022 ctx->TransformFeedback.CurrentObject,
4023 index, bufObj, false);
4024 return;
4025 case GL_UNIFORM_BUFFER:
4026 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
4027 return;
4028 case GL_SHADER_STORAGE_BUFFER:
4029 bind_buffer_base_shader_storage_buffer(ctx, index, bufObj);
4030 return;
4031 case GL_ATOMIC_COUNTER_BUFFER:
4032 bind_atomic_buffer(ctx, index, bufObj, 0, 0,
4033 "glBindBufferBase");
4034 return;
4035 default:
4036 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
4037 return;
4038 }
4039 }
4040
4041 void GLAPIENTRY
4042 _mesa_BindBuffersRange(GLenum target, GLuint first, GLsizei count,
4043 const GLuint *buffers,
4044 const GLintptr *offsets, const GLsizeiptr *sizes)
4045 {
4046 GET_CURRENT_CONTEXT(ctx);
4047
4048 if (MESA_VERBOSE & VERBOSE_API) {
4049 _mesa_debug(ctx, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4050 _mesa_enum_to_string(target), first, count,
4051 buffers, offsets, sizes);
4052 }
4053
4054 switch (target) {
4055 case GL_TRANSFORM_FEEDBACK_BUFFER:
4056 bind_xfb_buffers_range(ctx, first, count, buffers, offsets, sizes);
4057 return;
4058 case GL_UNIFORM_BUFFER:
4059 bind_uniform_buffers_range(ctx, first, count, buffers, offsets, sizes);
4060 return;
4061 case GL_SHADER_STORAGE_BUFFER:
4062 bind_shader_storage_buffers_range(ctx, first, count, buffers, offsets,
4063 sizes);
4064 return;
4065 case GL_ATOMIC_COUNTER_BUFFER:
4066 bind_atomic_buffers_range(ctx, first, count, buffers,
4067 offsets, sizes);
4068 return;
4069 default:
4070 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersRange(target=%s)",
4071 _mesa_enum_to_string(target));
4072 break;
4073 }
4074 }
4075
4076 void GLAPIENTRY
4077 _mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count,
4078 const GLuint *buffers)
4079 {
4080 GET_CURRENT_CONTEXT(ctx);
4081
4082 if (MESA_VERBOSE & VERBOSE_API) {
4083 _mesa_debug(ctx, "glBindBuffersBase(%s, %u, %d, %p)\n",
4084 _mesa_enum_to_string(target), first, count, buffers);
4085 }
4086
4087 switch (target) {
4088 case GL_TRANSFORM_FEEDBACK_BUFFER:
4089 bind_xfb_buffers_base(ctx, first, count, buffers);
4090 return;
4091 case GL_UNIFORM_BUFFER:
4092 bind_uniform_buffers_base(ctx, first, count, buffers);
4093 return;
4094 case GL_SHADER_STORAGE_BUFFER:
4095 bind_shader_storage_buffers_base(ctx, first, count, buffers);
4096 return;
4097 case GL_ATOMIC_COUNTER_BUFFER:
4098 bind_atomic_buffers_base(ctx, first, count, buffers);
4099 return;
4100 default:
4101 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersBase(target=%s)",
4102 _mesa_enum_to_string(target));
4103 break;
4104 }
4105 }
4106
4107 void GLAPIENTRY
4108 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
4109 GLsizeiptr length)
4110 {
4111 GET_CURRENT_CONTEXT(ctx);
4112 struct gl_buffer_object *bufObj;
4113 const GLintptr end = offset + length;
4114
4115 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4116 if (!bufObj) {
4117 _mesa_error(ctx, GL_INVALID_VALUE,
4118 "glInvalidateBufferSubData(name = 0x%x) invalid object",
4119 buffer);
4120 return;
4121 }
4122
4123 /* The GL_ARB_invalidate_subdata spec says:
4124 *
4125 * "An INVALID_VALUE error is generated if <offset> or <length> is
4126 * negative, or if <offset> + <length> is greater than the value of
4127 * BUFFER_SIZE."
4128 */
4129 if (end < 0 || end > bufObj->Size) {
4130 _mesa_error(ctx, GL_INVALID_VALUE,
4131 "glInvalidateBufferSubData(invalid offset or length)");
4132 return;
4133 }
4134
4135 /* The OpenGL 4.4 (Core Profile) spec says:
4136 *
4137 * "An INVALID_OPERATION error is generated if buffer is currently
4138 * mapped by MapBuffer or if the invalidate range intersects the range
4139 * currently mapped by MapBufferRange, unless it was mapped
4140 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4141 */
4142 if (!(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) &&
4143 bufferobj_range_mapped(bufObj, offset, length)) {
4144 _mesa_error(ctx, GL_INVALID_OPERATION,
4145 "glInvalidateBufferSubData(intersection with mapped "
4146 "range)");
4147 return;
4148 }
4149
4150 /* We don't actually do anything for this yet. Just return after
4151 * validating the parameters and generating the required errors.
4152 */
4153 return;
4154 }
4155
4156 void GLAPIENTRY
4157 _mesa_InvalidateBufferData(GLuint buffer)
4158 {
4159 GET_CURRENT_CONTEXT(ctx);
4160 struct gl_buffer_object *bufObj;
4161
4162 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4163 if (!bufObj) {
4164 _mesa_error(ctx, GL_INVALID_VALUE,
4165 "glInvalidateBufferData(name = 0x%x) invalid object",
4166 buffer);
4167 return;
4168 }
4169
4170 /* The OpenGL 4.4 (Core Profile) spec says:
4171 *
4172 * "An INVALID_OPERATION error is generated if buffer is currently
4173 * mapped by MapBuffer or if the invalidate range intersects the range
4174 * currently mapped by MapBufferRange, unless it was mapped
4175 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4176 */
4177 if (_mesa_check_disallowed_mapping(bufObj)) {
4178 _mesa_error(ctx, GL_INVALID_OPERATION,
4179 "glInvalidateBufferData(intersection with mapped "
4180 "range)");
4181 return;
4182 }
4183
4184 /* We don't actually do anything for this yet. Just return after
4185 * validating the parameters and generating the required errors.
4186 */
4187 return;
4188 }