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