mesa: align atomic buffer handling code with ubo/ssbo (v1.1)
[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 "externalobjects.h"
43 #include "mtypes.h"
44 #include "teximage.h"
45 #include "glformats.h"
46 #include "texstore.h"
47 #include "transformfeedback.h"
48 #include "varray.h"
49
50
51 /* Debug flags */
52 /*#define VBO_DEBUG*/
53 /*#define BOUNDS_CHECK*/
54
55
56 /**
57 * We count the number of buffer modification calls to check for
58 * inefficient buffer use. This is the number of such calls before we
59 * issue a warning.
60 */
61 #define BUFFER_WARNING_CALL_COUNT 4
62
63
64 /**
65 * Helper to warn of possible performance issues, such as frequently
66 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
67 * below.
68 */
69 static void
70 buffer_usage_warning(struct gl_context *ctx, GLuint *id, const char *fmt, ...)
71 {
72 va_list args;
73
74 va_start(args, fmt);
75 _mesa_gl_vdebug(ctx, id,
76 MESA_DEBUG_SOURCE_API,
77 MESA_DEBUG_TYPE_PERFORMANCE,
78 MESA_DEBUG_SEVERITY_MEDIUM,
79 fmt, args);
80 va_end(args);
81 }
82
83 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
84 do { \
85 static GLuint id = 0; \
86 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
87 } while (0)
88
89
90 /**
91 * Used as a placeholder for buffer objects between glGenBuffers() and
92 * glBindBuffer() so that glIsBuffer() can work correctly.
93 */
94 static struct gl_buffer_object DummyBufferObject;
95
96
97 /**
98 * Return pointer to address of a buffer object target.
99 * \param ctx the GL context
100 * \param target the buffer object target to be retrieved.
101 * \return pointer to pointer to the buffer object bound to \c target in the
102 * specified context or \c NULL if \c target is invalid.
103 */
104 static inline struct gl_buffer_object **
105 get_buffer_target(struct gl_context *ctx, GLenum target)
106 {
107 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
108 */
109 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx)
110 && target != GL_ARRAY_BUFFER && target != GL_ELEMENT_ARRAY_BUFFER)
111 return NULL;
112
113 switch (target) {
114 case GL_ARRAY_BUFFER_ARB:
115 return &ctx->Array.ArrayBufferObj;
116 case GL_ELEMENT_ARRAY_BUFFER_ARB:
117 return &ctx->Array.VAO->IndexBufferObj;
118 case GL_PIXEL_PACK_BUFFER_EXT:
119 return &ctx->Pack.BufferObj;
120 case GL_PIXEL_UNPACK_BUFFER_EXT:
121 return &ctx->Unpack.BufferObj;
122 case GL_COPY_READ_BUFFER:
123 return &ctx->CopyReadBuffer;
124 case GL_COPY_WRITE_BUFFER:
125 return &ctx->CopyWriteBuffer;
126 case GL_QUERY_BUFFER:
127 if (_mesa_has_ARB_query_buffer_object(ctx))
128 return &ctx->QueryBuffer;
129 break;
130 case GL_DRAW_INDIRECT_BUFFER:
131 if ((ctx->API == API_OPENGL_CORE &&
132 ctx->Extensions.ARB_draw_indirect) ||
133 _mesa_is_gles31(ctx)) {
134 return &ctx->DrawIndirectBuffer;
135 }
136 break;
137 case GL_PARAMETER_BUFFER_ARB:
138 if (_mesa_has_ARB_indirect_parameters(ctx)) {
139 return &ctx->ParameterBuffer;
140 }
141 break;
142 case GL_DISPATCH_INDIRECT_BUFFER:
143 if (_mesa_has_compute_shaders(ctx)) {
144 return &ctx->DispatchIndirectBuffer;
145 }
146 break;
147 case GL_TRANSFORM_FEEDBACK_BUFFER:
148 if (ctx->Extensions.EXT_transform_feedback) {
149 return &ctx->TransformFeedback.CurrentBuffer;
150 }
151 break;
152 case GL_TEXTURE_BUFFER:
153 if (_mesa_has_ARB_texture_buffer_object(ctx) ||
154 _mesa_has_OES_texture_buffer(ctx)) {
155 return &ctx->Texture.BufferObject;
156 }
157 break;
158 case GL_UNIFORM_BUFFER:
159 if (ctx->Extensions.ARB_uniform_buffer_object) {
160 return &ctx->UniformBuffer;
161 }
162 break;
163 case GL_SHADER_STORAGE_BUFFER:
164 if (ctx->Extensions.ARB_shader_storage_buffer_object) {
165 return &ctx->ShaderStorageBuffer;
166 }
167 break;
168 case GL_ATOMIC_COUNTER_BUFFER:
169 if (ctx->Extensions.ARB_shader_atomic_counters) {
170 return &ctx->AtomicBuffer;
171 }
172 break;
173 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD:
174 if (ctx->Extensions.AMD_pinned_memory) {
175 return &ctx->ExternalVirtualMemoryBuffer;
176 }
177 break;
178 default:
179 return NULL;
180 }
181 return NULL;
182 }
183
184
185 /**
186 * Get the buffer object bound to the specified target in a GL context.
187 * \param ctx the GL context
188 * \param target the buffer object target to be retrieved.
189 * \param error the GL error to record if target is illegal.
190 * \return pointer to the buffer object bound to \c target in the
191 * specified context or \c NULL if \c target is invalid.
192 */
193 static inline struct gl_buffer_object *
194 get_buffer(struct gl_context *ctx, const char *func, GLenum target,
195 GLenum error)
196 {
197 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
198
199 if (!bufObj) {
200 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
201 return NULL;
202 }
203
204 if (!_mesa_is_bufferobj(*bufObj)) {
205 _mesa_error(ctx, error, "%s(no buffer bound)", func);
206 return NULL;
207 }
208
209 return *bufObj;
210 }
211
212
213 /**
214 * Convert a GLbitfield describing the mapped buffer access flags
215 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
216 */
217 static GLenum
218 simplified_access_mode(struct gl_context *ctx, GLbitfield access)
219 {
220 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
221 if ((access & rwFlags) == rwFlags)
222 return GL_READ_WRITE;
223 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
224 return GL_READ_ONLY;
225 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
226 return GL_WRITE_ONLY;
227
228 /* Otherwise, AccessFlags is zero (the default state).
229 *
230 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
231 *
232 * Name Type Initial Value Legal Values
233 * ... ... ... ...
234 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
235 * READ_WRITE
236 *
237 * However, table 6.8 in the GL_OES_mapbuffer extension says:
238 *
239 * Get Value Type Get Command Value Description
240 * --------- ---- ----------- ----- -----------
241 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
242 *
243 * The difference is because GL_OES_mapbuffer only supports mapping buffers
244 * write-only.
245 */
246 assert(access == 0);
247
248 return _mesa_is_gles(ctx) ? GL_WRITE_ONLY : GL_READ_WRITE;
249 }
250
251
252 /**
253 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
254 * given range.
255 * The regions do not overlap if and only if the end of the given
256 * region is before the mapped region or the start of the given region
257 * is after the mapped region.
258 *
259 * \param obj Buffer object target on which to operate.
260 * \param offset Offset of the first byte of the subdata range.
261 * \param size Size, in bytes, of the subdata range.
262 * \return true if ranges overlap, false otherwise
263 *
264 */
265 static bool
266 bufferobj_range_mapped(const struct gl_buffer_object *obj,
267 GLintptr offset, GLsizeiptr size)
268 {
269 if (_mesa_bufferobj_mapped(obj, MAP_USER)) {
270 const GLintptr end = offset + size;
271 const GLintptr mapEnd = obj->Mappings[MAP_USER].Offset +
272 obj->Mappings[MAP_USER].Length;
273
274 if (!(end <= obj->Mappings[MAP_USER].Offset || offset >= mapEnd)) {
275 return true;
276 }
277 }
278 return false;
279 }
280
281
282 /**
283 * Tests the subdata range parameters and sets the GL error code for
284 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
285 * \c glClearBufferSubData.
286 *
287 * \param ctx GL context.
288 * \param bufObj The buffer object.
289 * \param offset Offset of the first byte of the subdata range.
290 * \param size Size, in bytes, of the subdata range.
291 * \param mappedRange If true, checks if an overlapping range is mapped.
292 * If false, checks if buffer is mapped.
293 * \param caller Name of calling function for recording errors.
294 * \return false if error, true otherwise
295 *
296 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
297 */
298 static bool
299 buffer_object_subdata_range_good(struct gl_context *ctx,
300 const struct gl_buffer_object *bufObj,
301 GLintptr offset, GLsizeiptr size,
302 bool mappedRange, const char *caller)
303 {
304 if (size < 0) {
305 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
306 return false;
307 }
308
309 if (offset < 0) {
310 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
311 return false;
312 }
313
314 if (offset + size > bufObj->Size) {
315 _mesa_error(ctx, GL_INVALID_VALUE,
316 "%s(offset %lu + size %lu > buffer size %lu)", caller,
317 (unsigned long) offset,
318 (unsigned long) size,
319 (unsigned long) bufObj->Size);
320 return false;
321 }
322
323 if (bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT)
324 return true;
325
326 if (mappedRange) {
327 if (bufferobj_range_mapped(bufObj, offset, size)) {
328 _mesa_error(ctx, GL_INVALID_OPERATION,
329 "%s(range is mapped without persistent bit)",
330 caller);
331 return false;
332 }
333 }
334 else {
335 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
336 _mesa_error(ctx, GL_INVALID_OPERATION,
337 "%s(buffer is mapped without persistent bit)",
338 caller);
339 return false;
340 }
341 }
342
343 return true;
344 }
345
346
347 /**
348 * Test the format and type parameters and set the GL error code for
349 * \c glClearBufferData and \c glClearBufferSubData.
350 *
351 * \param ctx GL context.
352 * \param internalformat Format to which the data is to be converted.
353 * \param format Format of the supplied data.
354 * \param type Type of the supplied data.
355 * \param caller Name of calling function for recording errors.
356 * \return If internalformat, format and type are legal the mesa_format
357 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
358 *
359 * \sa glClearBufferData and glClearBufferSubData
360 */
361 static mesa_format
362 validate_clear_buffer_format(struct gl_context *ctx,
363 GLenum internalformat,
364 GLenum format, GLenum type,
365 const char *caller)
366 {
367 mesa_format mesaFormat;
368 GLenum errorFormatType;
369
370 mesaFormat = _mesa_validate_texbuffer_format(ctx, internalformat);
371 if (mesaFormat == MESA_FORMAT_NONE) {
372 _mesa_error(ctx, GL_INVALID_ENUM,
373 "%s(invalid internalformat)", caller);
374 return MESA_FORMAT_NONE;
375 }
376
377 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
378 * EXT_texture_integer there is no conversion between integer and
379 * non-integer formats
380 */
381 if (_mesa_is_enum_format_signed_int(format) !=
382 _mesa_is_format_integer_color(mesaFormat)) {
383 _mesa_error(ctx, GL_INVALID_OPERATION,
384 "%s(integer vs non-integer)", caller);
385 return MESA_FORMAT_NONE;
386 }
387
388 if (!_mesa_is_color_format(format)) {
389 _mesa_error(ctx, GL_INVALID_ENUM,
390 "%s(format is not a color format)", caller);
391 return MESA_FORMAT_NONE;
392 }
393
394 errorFormatType = _mesa_error_check_format_and_type(ctx, format, type);
395 if (errorFormatType != GL_NO_ERROR) {
396 _mesa_error(ctx, GL_INVALID_ENUM,
397 "%s(invalid format or type)", caller);
398 return MESA_FORMAT_NONE;
399 }
400
401 return mesaFormat;
402 }
403
404
405 /**
406 * Convert user-specified clear value to the specified internal format.
407 *
408 * \param ctx GL context.
409 * \param internalformat Format to which the data is converted.
410 * \param clearValue Points to the converted clear value.
411 * \param format Format of the supplied data.
412 * \param type Type of the supplied data.
413 * \param data Data which is to be converted to internalformat.
414 * \param caller Name of calling function for recording errors.
415 * \return true if data could be converted, false otherwise.
416 *
417 * \sa glClearBufferData, glClearBufferSubData
418 */
419 static bool
420 convert_clear_buffer_data(struct gl_context *ctx,
421 mesa_format internalformat,
422 GLubyte *clearValue, GLenum format, GLenum type,
423 const GLvoid *data, const char *caller)
424 {
425 GLenum internalformatBase = _mesa_get_format_base_format(internalformat);
426
427 if (_mesa_texstore(ctx, 1, internalformatBase, internalformat,
428 0, &clearValue, 1, 1, 1,
429 format, type, data, &ctx->Unpack)) {
430 return true;
431 }
432 else {
433 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
434 return false;
435 }
436 }
437
438
439 /**
440 * Allocate and initialize a new buffer object.
441 *
442 * Default callback for the \c dd_function_table::NewBufferObject() hook.
443 */
444 static struct gl_buffer_object *
445 _mesa_new_buffer_object(struct gl_context *ctx, GLuint name)
446 {
447 struct gl_buffer_object *obj = MALLOC_STRUCT(gl_buffer_object);
448 if (!obj)
449 return NULL;
450
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 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 buf = ctx->Driver.NewBufferObject(ctx, buffer);
993 if (!buf) {
994 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
995 return false;
996 }
997 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf);
998 *buf_handle = buf;
999 }
1000
1001 return true;
1002 }
1003
1004 /**
1005 * Bind the specified target to buffer for the specified context.
1006 * Called by glBindBuffer() and other functions.
1007 */
1008 static void
1009 bind_buffer_object(struct gl_context *ctx,
1010 struct gl_buffer_object **bindTarget, GLuint buffer)
1011 {
1012 struct gl_buffer_object *oldBufObj;
1013 struct gl_buffer_object *newBufObj = NULL;
1014
1015 assert(bindTarget);
1016
1017 /* Get pointer to old buffer object (to be unbound) */
1018 oldBufObj = *bindTarget;
1019 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
1020 return; /* rebinding the same buffer object- no change */
1021
1022 /*
1023 * Get pointer to new buffer object (newBufObj)
1024 */
1025 if (buffer == 0) {
1026 /* The spec says there's not a buffer object named 0, but we use
1027 * one internally because it simplifies things.
1028 */
1029 newBufObj = ctx->Shared->NullBufferObj;
1030 }
1031 else {
1032 /* non-default buffer object */
1033 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
1034 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
1035 &newBufObj, "glBindBuffer"))
1036 return;
1037 }
1038
1039 /* record usage history */
1040 if (bindTarget == &ctx->Pack.BufferObj) {
1041 newBufObj->UsageHistory |= USAGE_PIXEL_PACK_BUFFER;
1042 }
1043
1044 /* bind new buffer */
1045 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
1046 }
1047
1048
1049 /**
1050 * Update the default buffer objects in the given context to reference those
1051 * specified in the shared state and release those referencing the old
1052 * shared state.
1053 */
1054 void
1055 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
1056 {
1057 /* Bind the NullBufferObj to remove references to those
1058 * in the shared context hash table.
1059 */
1060 bind_buffer_object(ctx, &ctx->Array.ArrayBufferObj, 0);
1061 bind_buffer_object(ctx, &ctx->Array.VAO->IndexBufferObj, 0);
1062 bind_buffer_object(ctx, &ctx->Pack.BufferObj, 0);
1063 bind_buffer_object(ctx, &ctx->Unpack.BufferObj, 0);
1064 }
1065
1066
1067
1068 /**
1069 * Return the gl_buffer_object for the given ID.
1070 * Always return NULL for ID 0.
1071 */
1072 struct gl_buffer_object *
1073 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
1074 {
1075 if (buffer == 0)
1076 return NULL;
1077 else
1078 return (struct gl_buffer_object *)
1079 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
1080 }
1081
1082
1083 struct gl_buffer_object *
1084 _mesa_lookup_bufferobj_locked(struct gl_context *ctx, GLuint buffer)
1085 {
1086 if (buffer == 0)
1087 return NULL;
1088 else
1089 return (struct gl_buffer_object *)
1090 _mesa_HashLookupLocked(ctx->Shared->BufferObjects, buffer);
1091 }
1092
1093 /**
1094 * A convenience function for direct state access functions that throws
1095 * GL_INVALID_OPERATION if buffer is not the name of an existing
1096 * buffer object.
1097 */
1098 struct gl_buffer_object *
1099 _mesa_lookup_bufferobj_err(struct gl_context *ctx, GLuint buffer,
1100 const char *caller)
1101 {
1102 struct gl_buffer_object *bufObj;
1103
1104 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
1105 if (!bufObj || bufObj == &DummyBufferObject) {
1106 _mesa_error(ctx, GL_INVALID_OPERATION,
1107 "%s(non-existent buffer object %u)", caller, buffer);
1108 return NULL;
1109 }
1110
1111 return bufObj;
1112 }
1113
1114
1115 /**
1116 * Look up a buffer object for a multi-bind function.
1117 *
1118 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1119 * of generating an error if the buffer ID is not zero or the name
1120 * of an existing buffer object.
1121 *
1122 * If the buffer ID refers to an existing buffer object, a pointer
1123 * to the buffer object is returned. If the ID is zero, a pointer
1124 * to the shared NullBufferObj is returned. If the ID is not zero
1125 * and does not refer to a valid buffer object, this function
1126 * returns NULL.
1127 *
1128 * This function assumes that the caller has already locked the
1129 * hash table mutex by calling
1130 * _mesa_HashLockMutex(ctx->Shared->BufferObjects).
1131 */
1132 struct gl_buffer_object *
1133 _mesa_multi_bind_lookup_bufferobj(struct gl_context *ctx,
1134 const GLuint *buffers,
1135 GLuint index, const char *caller)
1136 {
1137 struct gl_buffer_object *bufObj;
1138
1139 if (buffers[index] != 0) {
1140 bufObj = _mesa_lookup_bufferobj_locked(ctx, buffers[index]);
1141
1142 /* The multi-bind functions don't create the buffer objects
1143 when they don't exist. */
1144 if (bufObj == &DummyBufferObject)
1145 bufObj = NULL;
1146 } else
1147 bufObj = ctx->Shared->NullBufferObj;
1148
1149 if (!bufObj) {
1150 /* The ARB_multi_bind spec says:
1151 *
1152 * "An INVALID_OPERATION error is generated if any value
1153 * in <buffers> is not zero or the name of an existing
1154 * buffer object (per binding)."
1155 */
1156 _mesa_error(ctx, GL_INVALID_OPERATION,
1157 "%s(buffers[%u]=%u is not zero or the name "
1158 "of an existing buffer object)",
1159 caller, index, buffers[index]);
1160 }
1161
1162 return bufObj;
1163 }
1164
1165
1166 /**
1167 * If *ptr points to obj, set ptr = the Null/default buffer object.
1168 * This is a helper for buffer object deletion.
1169 * The GL spec says that deleting a buffer object causes it to get
1170 * unbound from all arrays in the current context.
1171 */
1172 static void
1173 unbind(struct gl_context *ctx,
1174 struct gl_vertex_array_object *vao, unsigned index,
1175 struct gl_buffer_object *obj)
1176 {
1177 if (vao->BufferBinding[index].BufferObj == obj) {
1178 _mesa_bind_vertex_buffer(ctx, vao, index, ctx->Shared->NullBufferObj,
1179 vao->BufferBinding[index].Offset,
1180 vao->BufferBinding[index].Stride);
1181 }
1182 }
1183
1184
1185 /**
1186 * Plug default/fallback buffer object functions into the device
1187 * driver hooks.
1188 */
1189 void
1190 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
1191 {
1192 /* GL_ARB_vertex/pixel_buffer_object */
1193 driver->NewBufferObject = _mesa_new_buffer_object;
1194 driver->DeleteBuffer = _mesa_delete_buffer_object;
1195 driver->BufferData = buffer_data_fallback;
1196 driver->BufferSubData = buffer_sub_data_fallback;
1197 driver->GetBufferSubData = buffer_get_subdata;
1198 driver->UnmapBuffer = unmap_buffer_fallback;
1199
1200 /* GL_ARB_clear_buffer_object */
1201 driver->ClearBufferSubData = _mesa_ClearBufferSubData_sw;
1202
1203 /* GL_ARB_map_buffer_range */
1204 driver->MapBufferRange = map_buffer_range_fallback;
1205 driver->FlushMappedBufferRange = flush_mapped_buffer_range_fallback;
1206
1207 /* GL_ARB_copy_buffer */
1208 driver->CopyBufferSubData = copy_buffer_sub_data_fallback;
1209 }
1210
1211
1212 void
1213 _mesa_buffer_unmap_all_mappings(struct gl_context *ctx,
1214 struct gl_buffer_object *bufObj)
1215 {
1216 for (int i = 0; i < MAP_COUNT; i++) {
1217 if (_mesa_bufferobj_mapped(bufObj, i)) {
1218 ctx->Driver.UnmapBuffer(ctx, bufObj, i);
1219 assert(bufObj->Mappings[i].Pointer == NULL);
1220 bufObj->Mappings[i].AccessFlags = 0;
1221 }
1222 }
1223 }
1224
1225
1226 /**********************************************************************/
1227 /* API Functions */
1228 /**********************************************************************/
1229
1230 void GLAPIENTRY
1231 _mesa_BindBuffer_no_error(GLenum target, GLuint buffer)
1232 {
1233 GET_CURRENT_CONTEXT(ctx);
1234
1235 struct gl_buffer_object **bindTarget = get_buffer_target(ctx, target);
1236 bind_buffer_object(ctx, bindTarget, buffer);
1237 }
1238
1239
1240 void GLAPIENTRY
1241 _mesa_BindBuffer(GLenum target, GLuint buffer)
1242 {
1243 GET_CURRENT_CONTEXT(ctx);
1244
1245 if (MESA_VERBOSE & VERBOSE_API) {
1246 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
1247 _mesa_enum_to_string(target), buffer);
1248 }
1249
1250 struct gl_buffer_object **bindTarget = get_buffer_target(ctx, target);
1251 if (!bindTarget) {
1252 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target %s)",
1253 _mesa_enum_to_string(target));
1254 return;
1255 }
1256
1257 bind_buffer_object(ctx, bindTarget, buffer);
1258 }
1259
1260 /**
1261 * Binds a buffer object to an atomic buffer binding point.
1262 *
1263 * The caller is responsible for validating the offset,
1264 * flushing the vertices and updating NewDriverState.
1265 */
1266 static void
1267 set_atomic_buffer_binding(struct gl_context *ctx,
1268 struct gl_atomic_buffer_binding *binding,
1269 struct gl_buffer_object *bufObj,
1270 GLintptr offset,
1271 GLsizeiptr size,
1272 bool autoSize)
1273 {
1274 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
1275
1276 binding->Offset = offset;
1277 binding->Size = size;
1278 binding->AutomaticSize = autoSize;
1279
1280 /* If this is a real buffer object, mark it has having been used
1281 * at some point as an atomic counter buffer.
1282 */
1283 if (size >= 0)
1284 bufObj->UsageHistory |= USAGE_ATOMIC_COUNTER_BUFFER;
1285 }
1286
1287 /**
1288 * Binds a buffer object to a uniform buffer binding point.
1289 *
1290 * The caller is responsible for flushing vertices and updating
1291 * NewDriverState.
1292 */
1293 static void
1294 set_ubo_binding(struct gl_context *ctx,
1295 struct gl_uniform_buffer_binding *binding,
1296 struct gl_buffer_object *bufObj,
1297 GLintptr offset,
1298 GLsizeiptr size,
1299 GLboolean autoSize)
1300 {
1301 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
1302
1303 binding->Offset = offset;
1304 binding->Size = size;
1305 binding->AutomaticSize = autoSize;
1306
1307 /* If this is a real buffer object, mark it has having been used
1308 * at some point as a UBO.
1309 */
1310 if (size >= 0)
1311 bufObj->UsageHistory |= USAGE_UNIFORM_BUFFER;
1312 }
1313
1314 /**
1315 * Binds a buffer object to a shader storage buffer binding point.
1316 *
1317 * The caller is responsible for flushing vertices and updating
1318 * NewDriverState.
1319 */
1320 static void
1321 set_ssbo_binding(struct gl_context *ctx,
1322 struct gl_shader_storage_buffer_binding *binding,
1323 struct gl_buffer_object *bufObj,
1324 GLintptr offset,
1325 GLsizeiptr size,
1326 GLboolean autoSize)
1327 {
1328 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
1329
1330 binding->Offset = offset;
1331 binding->Size = size;
1332 binding->AutomaticSize = autoSize;
1333
1334 /* If this is a real buffer object, mark it has having been used
1335 * at some point as a SSBO.
1336 */
1337 if (size >= 0)
1338 bufObj->UsageHistory |= USAGE_SHADER_STORAGE_BUFFER;
1339 }
1340
1341 /**
1342 * Binds a buffer object to a uniform buffer binding point.
1343 *
1344 * Unlike set_ubo_binding(), this function also flushes vertices
1345 * and updates NewDriverState. It also checks if the binding
1346 * has actually changed before updating it.
1347 */
1348 static void
1349 bind_uniform_buffer(struct gl_context *ctx,
1350 GLuint index,
1351 struct gl_buffer_object *bufObj,
1352 GLintptr offset,
1353 GLsizeiptr size,
1354 GLboolean autoSize)
1355 {
1356 struct gl_uniform_buffer_binding *binding =
1357 &ctx->UniformBufferBindings[index];
1358
1359 if (binding->BufferObject == bufObj &&
1360 binding->Offset == offset &&
1361 binding->Size == size &&
1362 binding->AutomaticSize == autoSize) {
1363 return;
1364 }
1365
1366 FLUSH_VERTICES(ctx, 0);
1367 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
1368
1369 set_ubo_binding(ctx, binding, bufObj, offset, size, autoSize);
1370 }
1371
1372 /**
1373 * Binds a buffer object to a shader storage buffer binding point.
1374 *
1375 * Unlike set_ssbo_binding(), this function also flushes vertices
1376 * and updates NewDriverState. It also checks if the binding
1377 * has actually changed before updating it.
1378 */
1379 static void
1380 bind_shader_storage_buffer(struct gl_context *ctx,
1381 GLuint index,
1382 struct gl_buffer_object *bufObj,
1383 GLintptr offset,
1384 GLsizeiptr size,
1385 GLboolean autoSize)
1386 {
1387 struct gl_shader_storage_buffer_binding *binding =
1388 &ctx->ShaderStorageBufferBindings[index];
1389
1390 if (binding->BufferObject == bufObj &&
1391 binding->Offset == offset &&
1392 binding->Size == size &&
1393 binding->AutomaticSize == autoSize) {
1394 return;
1395 }
1396
1397 FLUSH_VERTICES(ctx, 0);
1398 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
1399
1400 set_ssbo_binding(ctx, binding, bufObj, offset, size, autoSize);
1401 }
1402
1403 /**
1404 * Binds a buffer object to an atomic buffer binding point.
1405 *
1406 * Unlike set_atomic_binding(), this function also flushes vertices
1407 * and updates NewDriverState. It also checks if the binding
1408 * has actually changed before updating it.
1409 */
1410 static void
1411 bind_atomic_buffer(struct gl_context *ctx, unsigned index,
1412 struct gl_buffer_object *bufObj, GLintptr offset,
1413 GLsizeiptr size, GLboolean autoSize)
1414 {
1415 struct gl_atomic_buffer_binding *binding =
1416 &ctx->AtomicBufferBindings[index];
1417
1418 if (binding->BufferObject == bufObj &&
1419 binding->Offset == offset &&
1420 binding->Size == size &&
1421 binding->AutomaticSize == autoSize) {
1422 return;
1423 }
1424
1425 FLUSH_VERTICES(ctx, 0);
1426 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
1427
1428 set_atomic_buffer_binding(ctx, binding, bufObj, offset, size, autoSize);
1429 }
1430
1431 /**
1432 * Bind a buffer object to a uniform block binding point.
1433 * As above, but offset = 0.
1434 */
1435 static void
1436 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
1437 GLuint index,
1438 struct gl_buffer_object *bufObj)
1439 {
1440 if (index >= ctx->Const.MaxUniformBufferBindings) {
1441 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
1442 return;
1443 }
1444
1445 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
1446
1447 if (bufObj == ctx->Shared->NullBufferObj)
1448 bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
1449 else
1450 bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
1451 }
1452
1453 /**
1454 * Bind a buffer object to a shader storage block binding point.
1455 * As above, but offset = 0.
1456 */
1457 static void
1458 bind_buffer_base_shader_storage_buffer(struct gl_context *ctx,
1459 GLuint index,
1460 struct gl_buffer_object *bufObj)
1461 {
1462 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
1463 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
1464 return;
1465 }
1466
1467 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
1468
1469 if (bufObj == ctx->Shared->NullBufferObj)
1470 bind_shader_storage_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
1471 else
1472 bind_shader_storage_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
1473 }
1474
1475 /**
1476 * Bind a buffer object to a shader storage block binding point.
1477 * As above, but offset = 0.
1478 */
1479 static void
1480 bind_buffer_base_atomic_buffer(struct gl_context *ctx,
1481 GLuint index,
1482 struct gl_buffer_object *bufObj)
1483 {
1484 if (index >= ctx->Const.MaxAtomicBufferBindings) {
1485 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
1486 return;
1487 }
1488
1489 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
1490
1491 if (bufObj == ctx->Shared->NullBufferObj)
1492 bind_atomic_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
1493 else
1494 bind_atomic_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
1495 }
1496
1497 /**
1498 * Delete a set of buffer objects.
1499 *
1500 * \param n Number of buffer objects to delete.
1501 * \param ids Array of \c n buffer object IDs.
1502 */
1503 static void
1504 delete_buffers(struct gl_context *ctx, GLsizei n, const GLuint *ids)
1505 {
1506 FLUSH_VERTICES(ctx, 0);
1507
1508 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
1509
1510 for (GLsizei i = 0; i < n; i++) {
1511 struct gl_buffer_object *bufObj =
1512 _mesa_lookup_bufferobj_locked(ctx, ids[i]);
1513 if (bufObj) {
1514 struct gl_vertex_array_object *vao = ctx->Array.VAO;
1515 GLuint j;
1516
1517 assert(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
1518
1519 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1520
1521 /* unbind any vertex pointers bound to this buffer */
1522 for (j = 0; j < ARRAY_SIZE(vao->BufferBinding); j++) {
1523 unbind(ctx, vao, j, bufObj);
1524 }
1525
1526 if (ctx->Array.ArrayBufferObj == bufObj) {
1527 bind_buffer_object(ctx, &ctx->Array.ArrayBufferObj, 0);
1528 }
1529 if (vao->IndexBufferObj == bufObj) {
1530 bind_buffer_object(ctx, &vao->IndexBufferObj, 0);
1531 }
1532
1533 /* unbind ARB_draw_indirect binding point */
1534 if (ctx->DrawIndirectBuffer == bufObj) {
1535 bind_buffer_object(ctx, &ctx->DrawIndirectBuffer, 0);
1536 }
1537
1538 /* unbind ARB_indirect_parameters binding point */
1539 if (ctx->ParameterBuffer == bufObj) {
1540 bind_buffer_object(ctx, &ctx->ParameterBuffer, 0);
1541 }
1542
1543 /* unbind ARB_compute_shader binding point */
1544 if (ctx->DispatchIndirectBuffer == bufObj) {
1545 bind_buffer_object(ctx, &ctx->DispatchIndirectBuffer, 0);
1546 }
1547
1548 /* unbind ARB_copy_buffer binding points */
1549 if (ctx->CopyReadBuffer == bufObj) {
1550 bind_buffer_object(ctx, &ctx->CopyReadBuffer, 0);
1551 }
1552 if (ctx->CopyWriteBuffer == bufObj) {
1553 bind_buffer_object(ctx, &ctx->CopyWriteBuffer, 0);
1554 }
1555
1556 /* unbind transform feedback binding points */
1557 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
1558 bind_buffer_object(ctx, &ctx->TransformFeedback.CurrentBuffer, 0);
1559 }
1560 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
1561 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
1562 _mesa_bind_buffer_base_transform_feedback(ctx,
1563 ctx->TransformFeedback.CurrentObject,
1564 j, ctx->Shared->NullBufferObj,
1565 false);
1566 }
1567 }
1568
1569 /* unbind UBO binding points */
1570 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
1571 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
1572 bind_buffer_base_uniform_buffer(ctx, j,
1573 ctx->Shared->NullBufferObj);
1574 }
1575 }
1576
1577 if (ctx->UniformBuffer == bufObj) {
1578 bind_buffer_object(ctx, &ctx->UniformBuffer, 0);
1579 }
1580
1581 /* unbind SSBO binding points */
1582 for (j = 0; j < ctx->Const.MaxShaderStorageBufferBindings; j++) {
1583 if (ctx->ShaderStorageBufferBindings[j].BufferObject == bufObj) {
1584 bind_buffer_base_shader_storage_buffer(ctx, j,
1585 ctx->Shared->NullBufferObj);
1586 }
1587 }
1588
1589 if (ctx->ShaderStorageBuffer == bufObj) {
1590 bind_buffer_object(ctx, &ctx->ShaderStorageBuffer, 0);
1591 }
1592
1593 /* unbind Atomci Buffer binding points */
1594 for (j = 0; j < ctx->Const.MaxAtomicBufferBindings; j++) {
1595 if (ctx->AtomicBufferBindings[j].BufferObject == bufObj) {
1596 bind_buffer_base_atomic_buffer(ctx, j,
1597 ctx->Shared->NullBufferObj);
1598 }
1599 }
1600
1601 if (ctx->AtomicBuffer == bufObj) {
1602 bind_buffer_object(ctx, &ctx->AtomicBuffer, 0);
1603 }
1604
1605 /* unbind any pixel pack/unpack pointers bound to this buffer */
1606 if (ctx->Pack.BufferObj == bufObj) {
1607 bind_buffer_object(ctx, &ctx->Pack.BufferObj, 0);
1608 }
1609 if (ctx->Unpack.BufferObj == bufObj) {
1610 bind_buffer_object(ctx, &ctx->Unpack.BufferObj, 0);
1611 }
1612
1613 if (ctx->Texture.BufferObject == bufObj) {
1614 bind_buffer_object(ctx, &ctx->Texture.BufferObject, 0);
1615 }
1616
1617 if (ctx->ExternalVirtualMemoryBuffer == bufObj) {
1618 bind_buffer_object(ctx, &ctx->ExternalVirtualMemoryBuffer, 0);
1619 }
1620
1621 /* unbind query buffer binding point */
1622 if (ctx->QueryBuffer == bufObj) {
1623 bind_buffer_object(ctx, &ctx->QueryBuffer, 0);
1624 }
1625
1626 /* The ID is immediately freed for re-use */
1627 _mesa_HashRemoveLocked(ctx->Shared->BufferObjects, ids[i]);
1628 /* Make sure we do not run into the classic ABA problem on bind.
1629 * We don't want to allow re-binding a buffer object that's been
1630 * "deleted" by glDeleteBuffers().
1631 *
1632 * The explicit rebinding to the default object in the current context
1633 * prevents the above in the current context, but another context
1634 * sharing the same objects might suffer from this problem.
1635 * The alternative would be to do the hash lookup in any case on bind
1636 * which would introduce more runtime overhead than this.
1637 */
1638 bufObj->DeletePending = GL_TRUE;
1639 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
1640 }
1641 }
1642
1643 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
1644 }
1645
1646
1647 void GLAPIENTRY
1648 _mesa_DeleteBuffers_no_error(GLsizei n, const GLuint *ids)
1649 {
1650 GET_CURRENT_CONTEXT(ctx);
1651 delete_buffers(ctx, n, ids);
1652 }
1653
1654
1655 void GLAPIENTRY
1656 _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
1657 {
1658 GET_CURRENT_CONTEXT(ctx);
1659
1660 if (n < 0) {
1661 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
1662 return;
1663 }
1664
1665 delete_buffers(ctx, n, ids);
1666 }
1667
1668
1669 /**
1670 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1671 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1672 * driver internals.
1673 */
1674 static void
1675 create_buffers(struct gl_context *ctx, GLsizei n, GLuint *buffers, bool dsa)
1676 {
1677 GLuint first;
1678 struct gl_buffer_object *buf;
1679
1680 if (!buffers)
1681 return;
1682
1683 /*
1684 * This must be atomic (generation and allocation of buffer object IDs)
1685 */
1686 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
1687
1688 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
1689
1690 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1691 * DummyBufferObject. Otherwise, create a new buffer object and insert
1692 * it.
1693 */
1694 for (int i = 0; i < n; i++) {
1695 buffers[i] = first + i;
1696 if (dsa) {
1697 assert(ctx->Driver.NewBufferObject);
1698 buf = ctx->Driver.NewBufferObject(ctx, buffers[i]);
1699 if (!buf) {
1700 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCreateBuffers");
1701 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
1702 return;
1703 }
1704 }
1705 else
1706 buf = &DummyBufferObject;
1707
1708 _mesa_HashInsertLocked(ctx->Shared->BufferObjects, buffers[i], buf);
1709 }
1710
1711 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
1712 }
1713
1714
1715 static void
1716 create_buffers_err(struct gl_context *ctx, GLsizei n, GLuint *buffers, bool dsa)
1717 {
1718 const char *func = dsa ? "glCreateBuffers" : "glGenBuffers";
1719
1720 if (MESA_VERBOSE & VERBOSE_API)
1721 _mesa_debug(ctx, "%s(%d)\n", func, n);
1722
1723 if (n < 0) {
1724 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n %d < 0)", func, n);
1725 return;
1726 }
1727
1728 create_buffers(ctx, n, buffers, dsa);
1729 }
1730
1731 /**
1732 * Generate a set of unique buffer object IDs and store them in \c buffers.
1733 *
1734 * \param n Number of IDs to generate.
1735 * \param buffers Array of \c n locations to store the IDs.
1736 */
1737 void GLAPIENTRY
1738 _mesa_GenBuffers_no_error(GLsizei n, GLuint *buffers)
1739 {
1740 GET_CURRENT_CONTEXT(ctx);
1741 create_buffers(ctx, n, buffers, false);
1742 }
1743
1744
1745 void GLAPIENTRY
1746 _mesa_GenBuffers(GLsizei n, GLuint *buffers)
1747 {
1748 GET_CURRENT_CONTEXT(ctx);
1749 create_buffers_err(ctx, n, buffers, false);
1750 }
1751
1752 /**
1753 * Create a set of buffer objects and store their unique IDs in \c buffers.
1754 *
1755 * \param n Number of IDs to generate.
1756 * \param buffers Array of \c n locations to store the IDs.
1757 */
1758 void GLAPIENTRY
1759 _mesa_CreateBuffers_no_error(GLsizei n, GLuint *buffers)
1760 {
1761 GET_CURRENT_CONTEXT(ctx);
1762 create_buffers(ctx, n, buffers, true);
1763 }
1764
1765
1766 void GLAPIENTRY
1767 _mesa_CreateBuffers(GLsizei n, GLuint *buffers)
1768 {
1769 GET_CURRENT_CONTEXT(ctx);
1770 create_buffers_err(ctx, n, buffers, true);
1771 }
1772
1773
1774 /**
1775 * Determine if ID is the name of a buffer object.
1776 *
1777 * \param id ID of the potential buffer object.
1778 * \return \c GL_TRUE if \c id is the name of a buffer object,
1779 * \c GL_FALSE otherwise.
1780 */
1781 GLboolean GLAPIENTRY
1782 _mesa_IsBuffer(GLuint id)
1783 {
1784 struct gl_buffer_object *bufObj;
1785 GET_CURRENT_CONTEXT(ctx);
1786 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1787
1788 bufObj = _mesa_lookup_bufferobj(ctx, id);
1789
1790 return bufObj && bufObj != &DummyBufferObject;
1791 }
1792
1793
1794 static bool
1795 validate_buffer_storage(struct gl_context *ctx,
1796 struct gl_buffer_object *bufObj, GLsizeiptr size,
1797 GLbitfield flags, const char *func)
1798 {
1799 if (size <= 0) {
1800 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size <= 0)", func);
1801 return false;
1802 }
1803
1804 GLbitfield valid_flags = GL_MAP_READ_BIT |
1805 GL_MAP_WRITE_BIT |
1806 GL_MAP_PERSISTENT_BIT |
1807 GL_MAP_COHERENT_BIT |
1808 GL_DYNAMIC_STORAGE_BIT |
1809 GL_CLIENT_STORAGE_BIT;
1810
1811 if (ctx->Extensions.ARB_sparse_buffer)
1812 valid_flags |= GL_SPARSE_STORAGE_BIT_ARB;
1813
1814 if (flags & ~valid_flags) {
1815 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid flag bits set)", func);
1816 return false;
1817 }
1818
1819 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1820 *
1821 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1822 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1823 * MAP_READ_BIT or MAP_WRITE_BIT."
1824 */
1825 if (flags & GL_SPARSE_STORAGE_BIT_ARB &&
1826 flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) {
1827 _mesa_error(ctx, GL_INVALID_VALUE, "%s(SPARSE_STORAGE and READ/WRITE)", func);
1828 return false;
1829 }
1830
1831 if (flags & GL_MAP_PERSISTENT_BIT &&
1832 !(flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))) {
1833 _mesa_error(ctx, GL_INVALID_VALUE,
1834 "%s(PERSISTENT and flags!=READ/WRITE)", func);
1835 return false;
1836 }
1837
1838 if (flags & GL_MAP_COHERENT_BIT && !(flags & GL_MAP_PERSISTENT_BIT)) {
1839 _mesa_error(ctx, GL_INVALID_VALUE,
1840 "%s(COHERENT and flags!=PERSISTENT)", func);
1841 return false;
1842 }
1843
1844 if (bufObj->Immutable || bufObj->HandleAllocated) {
1845 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
1846 return false;
1847 }
1848
1849 return true;
1850 }
1851
1852
1853 static void
1854 buffer_storage(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1855 struct gl_memory_object *memObj, GLenum target,
1856 GLsizeiptr size, const GLvoid *data, GLbitfield flags,
1857 GLuint64 offset, const char *func)
1858 {
1859 GLboolean res;
1860
1861 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1862 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1863
1864 FLUSH_VERTICES(ctx, 0);
1865
1866 bufObj->Written = GL_TRUE;
1867 bufObj->Immutable = GL_TRUE;
1868 bufObj->MinMaxCacheDirty = true;
1869
1870 if (memObj) {
1871 assert(ctx->Driver.BufferDataMem);
1872 res = ctx->Driver.BufferDataMem(ctx, target, size, memObj, offset,
1873 GL_DYNAMIC_DRAW, bufObj);
1874 }
1875 else {
1876 assert(ctx->Driver.BufferData);
1877 res = ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
1878 flags, bufObj);
1879 }
1880
1881 if (!res) {
1882 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
1883 /* Even though the interaction between AMD_pinned_memory and
1884 * glBufferStorage is not described in the spec, Graham Sellers
1885 * said that it should behave the same as glBufferData.
1886 */
1887 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1888 }
1889 else {
1890 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1891 }
1892 }
1893 }
1894
1895
1896 static ALWAYS_INLINE void
1897 inlined_buffer_storage(GLenum target, GLuint buffer, GLsizeiptr size,
1898 const GLvoid *data, GLbitfield flags,
1899 GLuint memory, GLuint64 offset,
1900 bool dsa, bool mem, bool no_error, const char *func)
1901 {
1902 GET_CURRENT_CONTEXT(ctx);
1903 struct gl_buffer_object *bufObj;
1904 struct gl_memory_object *memObj = NULL;
1905
1906 if (mem) {
1907 if (!no_error) {
1908 if (!ctx->Extensions.EXT_memory_object) {
1909 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
1910 return;
1911 }
1912
1913 /* From the EXT_external_objects spec:
1914 *
1915 * "An INVALID_VALUE error is generated by BufferStorageMemEXT and
1916 * NamedBufferStorageMemEXT if <memory> is 0, or ..."
1917 */
1918 if (memory == 0) {
1919 _mesa_error(ctx, GL_INVALID_VALUE, "%s(memory == 0)", func);
1920 }
1921 }
1922
1923 memObj = _mesa_lookup_memory_object(ctx, memory);
1924 if (!memObj)
1925 return;
1926
1927 /* From the EXT_external_objects spec:
1928 *
1929 * "An INVALID_OPERATION error is generated if <memory> names a
1930 * valid memory object which has no associated memory."
1931 */
1932 if (!no_error && !memObj->Immutable) {
1933 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no associated memory)",
1934 func);
1935 return;
1936 }
1937 }
1938
1939 if (dsa) {
1940 if (no_error) {
1941 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
1942 } else {
1943 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, func);
1944 if (!bufObj)
1945 return;
1946 }
1947 } else {
1948 if (no_error) {
1949 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
1950 bufObj = *bufObjPtr;
1951 } else {
1952 bufObj = get_buffer(ctx, func, target, GL_INVALID_OPERATION);
1953 if (!bufObj)
1954 return;
1955 }
1956 }
1957
1958 if (no_error || validate_buffer_storage(ctx, bufObj, size, flags, func))
1959 buffer_storage(ctx, bufObj, memObj, target, size, data, flags, offset, func);
1960 }
1961
1962
1963 void GLAPIENTRY
1964 _mesa_BufferStorage_no_error(GLenum target, GLsizeiptr size,
1965 const GLvoid *data, GLbitfield flags)
1966 {
1967 inlined_buffer_storage(target, 0, size, data, flags, GL_NONE, 0,
1968 false, false, true, "glBufferStorage");
1969 }
1970
1971
1972 void GLAPIENTRY
1973 _mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data,
1974 GLbitfield flags)
1975 {
1976 inlined_buffer_storage(target, 0, size, data, flags, GL_NONE, 0,
1977 false, false, false, "glBufferStorage");
1978 }
1979
1980
1981 void GLAPIENTRY
1982 _mesa_BufferStorageMemEXT(GLenum target, GLsizeiptr size,
1983 GLuint memory, GLuint64 offset)
1984 {
1985 inlined_buffer_storage(target, 0, size, NULL, 0, memory, offset,
1986 false, true, false, "glBufferStorageMemEXT");
1987 }
1988
1989
1990 void GLAPIENTRY
1991 _mesa_BufferStorageMemEXT_no_error(GLenum target, GLsizeiptr size,
1992 GLuint memory, GLuint64 offset)
1993 {
1994 inlined_buffer_storage(target, 0, size, NULL, 0, memory, offset,
1995 false, true, true, "glBufferStorageMemEXT");
1996 }
1997
1998
1999 void GLAPIENTRY
2000 _mesa_NamedBufferStorage_no_error(GLuint buffer, GLsizeiptr size,
2001 const GLvoid *data, GLbitfield flags)
2002 {
2003 /* In direct state access, buffer objects have an unspecified target
2004 * since they are not required to be bound.
2005 */
2006 inlined_buffer_storage(GL_NONE, buffer, size, data, flags, GL_NONE, 0,
2007 true, false, true, "glNamedBufferStorage");
2008 }
2009
2010
2011 void GLAPIENTRY
2012 _mesa_NamedBufferStorage(GLuint buffer, GLsizeiptr size, const GLvoid *data,
2013 GLbitfield flags)
2014 {
2015 /* In direct state access, buffer objects have an unspecified target
2016 * since they are not required to be bound.
2017 */
2018 inlined_buffer_storage(GL_NONE, buffer, size, data, flags, GL_NONE, 0,
2019 true, false, false, "glNamedBufferStorage");
2020 }
2021
2022 void GLAPIENTRY
2023 _mesa_NamedBufferStorageMemEXT(GLuint buffer, GLsizeiptr size,
2024 GLuint memory, GLuint64 offset)
2025 {
2026 inlined_buffer_storage(GL_NONE, buffer, size, GL_NONE, 0, memory, offset,
2027 true, true, false, "glNamedBufferStorageMemEXT");
2028 }
2029
2030
2031 void GLAPIENTRY
2032 _mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer, GLsizeiptr size,
2033 GLuint memory, GLuint64 offset)
2034 {
2035 inlined_buffer_storage(GL_NONE, buffer, size, GL_NONE, 0, memory, offset,
2036 true, true, true, "glNamedBufferStorageMemEXT");
2037 }
2038
2039
2040 static ALWAYS_INLINE void
2041 buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2042 GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage,
2043 const char *func, bool no_error)
2044 {
2045 bool valid_usage;
2046
2047 if (MESA_VERBOSE & VERBOSE_API) {
2048 _mesa_debug(ctx, "%s(%s, %ld, %p, %s)\n",
2049 func,
2050 _mesa_enum_to_string(target),
2051 (long int) size, data,
2052 _mesa_enum_to_string(usage));
2053 }
2054
2055 if (!no_error) {
2056 if (size < 0) {
2057 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", func);
2058 return;
2059 }
2060
2061 switch (usage) {
2062 case GL_STREAM_DRAW_ARB:
2063 valid_usage = (ctx->API != API_OPENGLES);
2064 break;
2065 case GL_STATIC_DRAW_ARB:
2066 case GL_DYNAMIC_DRAW_ARB:
2067 valid_usage = true;
2068 break;
2069 case GL_STREAM_READ_ARB:
2070 case GL_STREAM_COPY_ARB:
2071 case GL_STATIC_READ_ARB:
2072 case GL_STATIC_COPY_ARB:
2073 case GL_DYNAMIC_READ_ARB:
2074 case GL_DYNAMIC_COPY_ARB:
2075 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
2076 break;
2077 default:
2078 valid_usage = false;
2079 break;
2080 }
2081
2082 if (!valid_usage) {
2083 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid usage: %s)", func,
2084 _mesa_enum_to_string(usage));
2085 return;
2086 }
2087
2088 if (bufObj->Immutable || bufObj->HandleAllocated) {
2089 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
2090 return;
2091 }
2092 }
2093
2094 /* Unmap the existing buffer. We'll replace it now. Not an error. */
2095 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
2096
2097 FLUSH_VERTICES(ctx, 0);
2098
2099 bufObj->Written = GL_TRUE;
2100 bufObj->MinMaxCacheDirty = true;
2101
2102 #ifdef VBO_DEBUG
2103 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
2104 bufObj->Name, size, data, usage);
2105 #endif
2106
2107 #ifdef BOUNDS_CHECK
2108 size += 100;
2109 #endif
2110
2111 assert(ctx->Driver.BufferData);
2112 if (!ctx->Driver.BufferData(ctx, target, size, data, usage,
2113 GL_MAP_READ_BIT |
2114 GL_MAP_WRITE_BIT |
2115 GL_DYNAMIC_STORAGE_BIT,
2116 bufObj)) {
2117 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
2118 if (!no_error) {
2119 /* From GL_AMD_pinned_memory:
2120 *
2121 * INVALID_OPERATION is generated by BufferData if <target> is
2122 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
2123 * mapped to the GPU address space.
2124 */
2125 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
2126 }
2127 } else {
2128 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
2129 }
2130 }
2131 }
2132
2133 static void
2134 buffer_data_error(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2135 GLenum target, GLsizeiptr size, const GLvoid *data,
2136 GLenum usage, const char *func)
2137 {
2138 buffer_data(ctx, bufObj, target, size, data, usage, func, false);
2139 }
2140
2141 static void
2142 buffer_data_no_error(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2143 GLenum target, GLsizeiptr size, const GLvoid *data,
2144 GLenum usage, const char *func)
2145 {
2146 buffer_data(ctx, bufObj, target, size, data, usage, func, true);
2147 }
2148
2149 void
2150 _mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2151 GLenum target, GLsizeiptr size, const GLvoid *data,
2152 GLenum usage, const char *func)
2153 {
2154 buffer_data_error(ctx, bufObj, target, size, data, usage, func);
2155 }
2156
2157 void GLAPIENTRY
2158 _mesa_BufferData_no_error(GLenum target, GLsizeiptr size, const GLvoid *data,
2159 GLenum usage)
2160 {
2161 GET_CURRENT_CONTEXT(ctx);
2162
2163 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
2164 buffer_data_no_error(ctx, *bufObj, target, size, data, usage,
2165 "glBufferData");
2166 }
2167
2168 void GLAPIENTRY
2169 _mesa_BufferData(GLenum target, GLsizeiptr size,
2170 const GLvoid *data, GLenum usage)
2171 {
2172 GET_CURRENT_CONTEXT(ctx);
2173 struct gl_buffer_object *bufObj;
2174
2175 bufObj = get_buffer(ctx, "glBufferData", target, GL_INVALID_OPERATION);
2176 if (!bufObj)
2177 return;
2178
2179 _mesa_buffer_data(ctx, bufObj, target, size, data, usage,
2180 "glBufferData");
2181 }
2182
2183 void GLAPIENTRY
2184 _mesa_NamedBufferData_no_error(GLuint buffer, GLsizeiptr size,
2185 const GLvoid *data, GLenum usage)
2186 {
2187 GET_CURRENT_CONTEXT(ctx);
2188
2189 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2190 buffer_data_no_error(ctx, bufObj, GL_NONE, size, data, usage,
2191 "glNamedBufferData");
2192 }
2193
2194 void GLAPIENTRY
2195 _mesa_NamedBufferData(GLuint buffer, GLsizeiptr size, const GLvoid *data,
2196 GLenum usage)
2197 {
2198 GET_CURRENT_CONTEXT(ctx);
2199 struct gl_buffer_object *bufObj;
2200
2201 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferData");
2202 if (!bufObj)
2203 return;
2204
2205 /* In direct state access, buffer objects have an unspecified target since
2206 * they are not required to be bound.
2207 */
2208 _mesa_buffer_data(ctx, bufObj, GL_NONE, size, data, usage,
2209 "glNamedBufferData");
2210 }
2211
2212
2213 static bool
2214 validate_buffer_sub_data(struct gl_context *ctx,
2215 struct gl_buffer_object *bufObj,
2216 GLintptr offset, GLsizeiptr size,
2217 const char *func)
2218 {
2219 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
2220 true, func)) {
2221 /* error already recorded */
2222 return false;
2223 }
2224
2225 if (bufObj->Immutable &&
2226 !(bufObj->StorageFlags & GL_DYNAMIC_STORAGE_BIT)) {
2227 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
2228 return false;
2229 }
2230
2231 if ((bufObj->Usage == GL_STATIC_DRAW ||
2232 bufObj->Usage == GL_STATIC_COPY) &&
2233 bufObj->NumSubDataCalls >= BUFFER_WARNING_CALL_COUNT - 1) {
2234 /* If the application declared the buffer as static draw/copy or stream
2235 * draw, it should not be frequently modified with glBufferSubData.
2236 */
2237 BUFFER_USAGE_WARNING(ctx,
2238 "using %s(buffer %u, offset %u, size %u) to "
2239 "update a %s buffer",
2240 func, bufObj->Name, offset, size,
2241 _mesa_enum_to_string(bufObj->Usage));
2242 }
2243
2244 return true;
2245 }
2246
2247
2248 /**
2249 * Implementation for glBufferSubData and glNamedBufferSubData.
2250 *
2251 * \param ctx GL context.
2252 * \param bufObj The buffer object.
2253 * \param offset Offset of the first byte of the subdata range.
2254 * \param size Size, in bytes, of the subdata range.
2255 * \param data The data store.
2256 * \param func Name of calling function for recording errors.
2257 *
2258 */
2259 void
2260 _mesa_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2261 GLintptr offset, GLsizeiptr size, const GLvoid *data)
2262 {
2263 if (size == 0)
2264 return;
2265
2266 bufObj->NumSubDataCalls++;
2267 bufObj->Written = GL_TRUE;
2268 bufObj->MinMaxCacheDirty = true;
2269
2270 assert(ctx->Driver.BufferSubData);
2271 ctx->Driver.BufferSubData(ctx, offset, size, data, bufObj);
2272 }
2273
2274
2275 static ALWAYS_INLINE void
2276 buffer_sub_data(GLenum target, GLuint buffer, GLintptr offset,
2277 GLsizeiptr size, const GLvoid *data,
2278 bool dsa, bool no_error, const char *func)
2279 {
2280 GET_CURRENT_CONTEXT(ctx);
2281 struct gl_buffer_object *bufObj;
2282
2283 if (dsa) {
2284 if (no_error) {
2285 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2286 } else {
2287 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, func);
2288 if (!bufObj)
2289 return;
2290 }
2291 } else {
2292 if (no_error) {
2293 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
2294 bufObj = *bufObjPtr;
2295 } else {
2296 bufObj = get_buffer(ctx, func, target, GL_INVALID_OPERATION);
2297 if (!bufObj)
2298 return;
2299 }
2300 }
2301
2302 if (no_error || validate_buffer_sub_data(ctx, bufObj, offset, size, func))
2303 _mesa_buffer_sub_data(ctx, bufObj, offset, size, data);
2304 }
2305
2306
2307 void GLAPIENTRY
2308 _mesa_BufferSubData_no_error(GLenum target, GLintptr offset,
2309 GLsizeiptr size, const GLvoid *data)
2310 {
2311 buffer_sub_data(target, 0, offset, size, data, false, true,
2312 "glBufferSubData");
2313 }
2314
2315
2316 void GLAPIENTRY
2317 _mesa_BufferSubData(GLenum target, GLintptr offset,
2318 GLsizeiptr size, const GLvoid *data)
2319 {
2320 buffer_sub_data(target, 0, offset, size, data, false, false,
2321 "glBufferSubData");
2322 }
2323
2324 void GLAPIENTRY
2325 _mesa_NamedBufferSubData_no_error(GLuint buffer, GLintptr offset,
2326 GLsizeiptr size, const GLvoid *data)
2327 {
2328 buffer_sub_data(0, buffer, offset, size, data, true, true,
2329 "glNamedBufferSubData");
2330 }
2331
2332 void GLAPIENTRY
2333 _mesa_NamedBufferSubData(GLuint buffer, GLintptr offset,
2334 GLsizeiptr size, const GLvoid *data)
2335 {
2336 buffer_sub_data(0, buffer, offset, size, data, true, false,
2337 "glNamedBufferSubData");
2338 }
2339
2340
2341 void GLAPIENTRY
2342 _mesa_GetBufferSubData(GLenum target, GLintptr offset,
2343 GLsizeiptr size, GLvoid *data)
2344 {
2345 GET_CURRENT_CONTEXT(ctx);
2346 struct gl_buffer_object *bufObj;
2347
2348 bufObj = get_buffer(ctx, "glGetBufferSubData", target,
2349 GL_INVALID_OPERATION);
2350 if (!bufObj)
2351 return;
2352
2353 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
2354 "glGetBufferSubData")) {
2355 return;
2356 }
2357
2358 assert(ctx->Driver.GetBufferSubData);
2359 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
2360 }
2361
2362 void GLAPIENTRY
2363 _mesa_GetNamedBufferSubData(GLuint buffer, GLintptr offset,
2364 GLsizeiptr size, GLvoid *data)
2365 {
2366 GET_CURRENT_CONTEXT(ctx);
2367 struct gl_buffer_object *bufObj;
2368
2369 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2370 "glGetNamedBufferSubData");
2371 if (!bufObj)
2372 return;
2373
2374 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
2375 "glGetNamedBufferSubData")) {
2376 return;
2377 }
2378
2379 assert(ctx->Driver.GetBufferSubData);
2380 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
2381 }
2382
2383
2384 /**
2385 * \param subdata true if caller is *SubData, false if *Data
2386 */
2387 static ALWAYS_INLINE void
2388 clear_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2389 GLenum internalformat, GLintptr offset, GLsizeiptr size,
2390 GLenum format, GLenum type, const GLvoid *data,
2391 const char *func, bool subdata, bool no_error)
2392 {
2393 mesa_format mesaFormat;
2394 GLubyte clearValue[MAX_PIXEL_BYTES];
2395 GLsizeiptr clearValueSize;
2396
2397 /* This checks for disallowed mappings. */
2398 if (!no_error && !buffer_object_subdata_range_good(ctx, bufObj, offset, size,
2399 subdata, func)) {
2400 return;
2401 }
2402
2403 if (no_error) {
2404 mesaFormat = _mesa_get_texbuffer_format(ctx, internalformat);
2405 } else {
2406 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
2407 format, type, func);
2408 }
2409
2410 if (mesaFormat == MESA_FORMAT_NONE)
2411 return;
2412
2413 clearValueSize = _mesa_get_format_bytes(mesaFormat);
2414 if (!no_error &&
2415 (offset % clearValueSize != 0 || size % clearValueSize != 0)) {
2416 _mesa_error(ctx, GL_INVALID_VALUE,
2417 "%s(offset or size is not a multiple of "
2418 "internalformat size)", func);
2419 return;
2420 }
2421
2422 /* Bail early. Negative size has already been checked. */
2423 if (size == 0)
2424 return;
2425
2426 bufObj->MinMaxCacheDirty = true;
2427
2428 if (data == NULL) {
2429 /* clear to zeros, per the spec */
2430 ctx->Driver.ClearBufferSubData(ctx, offset, size,
2431 NULL, clearValueSize, bufObj);
2432 return;
2433 }
2434
2435 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
2436 format, type, data, func)) {
2437 return;
2438 }
2439
2440 ctx->Driver.ClearBufferSubData(ctx, offset, size,
2441 clearValue, clearValueSize, bufObj);
2442 }
2443
2444 static void
2445 clear_buffer_sub_data_error(struct gl_context *ctx,
2446 struct gl_buffer_object *bufObj,
2447 GLenum internalformat, GLintptr offset,
2448 GLsizeiptr size, GLenum format, GLenum type,
2449 const GLvoid *data, const char *func, bool subdata)
2450 {
2451 clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size, format,
2452 type, data, func, subdata, false);
2453 }
2454
2455
2456 static void
2457 clear_buffer_sub_data_no_error(struct gl_context *ctx,
2458 struct gl_buffer_object *bufObj,
2459 GLenum internalformat, GLintptr offset,
2460 GLsizeiptr size, GLenum format, GLenum type,
2461 const GLvoid *data, const char *func,
2462 bool subdata)
2463 {
2464 clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size, format,
2465 type, data, func, subdata, true);
2466 }
2467
2468
2469 void GLAPIENTRY
2470 _mesa_ClearBufferData_no_error(GLenum target, GLenum internalformat,
2471 GLenum format, GLenum type, const GLvoid *data)
2472 {
2473 GET_CURRENT_CONTEXT(ctx);
2474
2475 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
2476 clear_buffer_sub_data_no_error(ctx, *bufObj, internalformat, 0,
2477 (*bufObj)->Size, format, type, data,
2478 "glClearBufferData", false);
2479 }
2480
2481
2482 void GLAPIENTRY
2483 _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
2484 GLenum type, const GLvoid *data)
2485 {
2486 GET_CURRENT_CONTEXT(ctx);
2487 struct gl_buffer_object *bufObj;
2488
2489 bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
2490 if (!bufObj)
2491 return;
2492
2493 clear_buffer_sub_data_error(ctx, bufObj, internalformat, 0, bufObj->Size,
2494 format, type, data, "glClearBufferData", false);
2495 }
2496
2497
2498 void GLAPIENTRY
2499 _mesa_ClearNamedBufferData_no_error(GLuint buffer, GLenum internalformat,
2500 GLenum format, GLenum type,
2501 const GLvoid *data)
2502 {
2503 GET_CURRENT_CONTEXT(ctx);
2504
2505 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2506 clear_buffer_sub_data_no_error(ctx, bufObj, internalformat, 0, bufObj->Size,
2507 format, type, data, "glClearNamedBufferData",
2508 false);
2509 }
2510
2511
2512 void GLAPIENTRY
2513 _mesa_ClearNamedBufferData(GLuint buffer, GLenum internalformat,
2514 GLenum format, GLenum type, const GLvoid *data)
2515 {
2516 GET_CURRENT_CONTEXT(ctx);
2517 struct gl_buffer_object *bufObj;
2518
2519 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glClearNamedBufferData");
2520 if (!bufObj)
2521 return;
2522
2523 clear_buffer_sub_data_error(ctx, bufObj, internalformat, 0, bufObj->Size,
2524 format, type, data, "glClearNamedBufferData",
2525 false);
2526 }
2527
2528
2529 void GLAPIENTRY
2530 _mesa_ClearBufferSubData_no_error(GLenum target, GLenum internalformat,
2531 GLintptr offset, GLsizeiptr size,
2532 GLenum format, GLenum type,
2533 const GLvoid *data)
2534 {
2535 GET_CURRENT_CONTEXT(ctx);
2536
2537 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
2538 clear_buffer_sub_data_no_error(ctx, *bufObj, internalformat, offset, size,
2539 format, type, data, "glClearBufferSubData",
2540 true);
2541 }
2542
2543
2544 void GLAPIENTRY
2545 _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
2546 GLintptr offset, GLsizeiptr size,
2547 GLenum format, GLenum type,
2548 const GLvoid *data)
2549 {
2550 GET_CURRENT_CONTEXT(ctx);
2551 struct gl_buffer_object *bufObj;
2552
2553 bufObj = get_buffer(ctx, "glClearBufferSubData", target, GL_INVALID_VALUE);
2554 if (!bufObj)
2555 return;
2556
2557 clear_buffer_sub_data_error(ctx, bufObj, internalformat, offset, size,
2558 format, type, data, "glClearBufferSubData",
2559 true);
2560 }
2561
2562
2563 void GLAPIENTRY
2564 _mesa_ClearNamedBufferSubData_no_error(GLuint buffer, GLenum internalformat,
2565 GLintptr offset, GLsizeiptr size,
2566 GLenum format, GLenum type,
2567 const GLvoid *data)
2568 {
2569 GET_CURRENT_CONTEXT(ctx);
2570
2571 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2572 clear_buffer_sub_data_no_error(ctx, bufObj, internalformat, offset, size,
2573 format, type, data,
2574 "glClearNamedBufferSubData", true);
2575 }
2576
2577
2578 void GLAPIENTRY
2579 _mesa_ClearNamedBufferSubData(GLuint buffer, GLenum internalformat,
2580 GLintptr offset, GLsizeiptr size,
2581 GLenum format, GLenum type,
2582 const GLvoid *data)
2583 {
2584 GET_CURRENT_CONTEXT(ctx);
2585 struct gl_buffer_object *bufObj;
2586
2587 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2588 "glClearNamedBufferSubData");
2589 if (!bufObj)
2590 return;
2591
2592 clear_buffer_sub_data_error(ctx, bufObj, internalformat, offset, size,
2593 format, type, data, "glClearNamedBufferSubData",
2594 true);
2595 }
2596
2597 static GLboolean
2598 unmap_buffer(struct gl_context *ctx, struct gl_buffer_object *bufObj)
2599 {
2600 GLboolean status = ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_USER);
2601 bufObj->Mappings[MAP_USER].AccessFlags = 0;
2602 assert(bufObj->Mappings[MAP_USER].Pointer == NULL);
2603 assert(bufObj->Mappings[MAP_USER].Offset == 0);
2604 assert(bufObj->Mappings[MAP_USER].Length == 0);
2605
2606 return status;
2607 }
2608
2609 static GLboolean
2610 validate_and_unmap_buffer(struct gl_context *ctx,
2611 struct gl_buffer_object *bufObj,
2612 const char *func)
2613 {
2614 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
2615
2616 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2617 _mesa_error(ctx, GL_INVALID_OPERATION,
2618 "%s(buffer is not mapped)", func);
2619 return GL_FALSE;
2620 }
2621
2622 #ifdef BOUNDS_CHECK
2623 if (bufObj->Access != GL_READ_ONLY_ARB) {
2624 GLubyte *buf = (GLubyte *) bufObj->Pointer;
2625 GLuint i;
2626 /* check that last 100 bytes are still = magic value */
2627 for (i = 0; i < 100; i++) {
2628 GLuint pos = bufObj->Size - i - 1;
2629 if (buf[pos] != 123) {
2630 _mesa_warning(ctx, "Out of bounds buffer object write detected"
2631 " at position %d (value = %u)\n",
2632 pos, buf[pos]);
2633 }
2634 }
2635 }
2636 #endif
2637
2638 #ifdef VBO_DEBUG
2639 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
2640 GLuint i, unchanged = 0;
2641 GLubyte *b = (GLubyte *) bufObj->Pointer;
2642 GLint pos = -1;
2643 /* check which bytes changed */
2644 for (i = 0; i < bufObj->Size - 1; i++) {
2645 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
2646 unchanged++;
2647 if (pos == -1)
2648 pos = i;
2649 }
2650 }
2651 if (unchanged) {
2652 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2653 bufObj->Name, unchanged, bufObj->Size, pos);
2654 }
2655 }
2656 #endif
2657
2658 return unmap_buffer(ctx, bufObj);
2659 }
2660
2661 GLboolean GLAPIENTRY
2662 _mesa_UnmapBuffer_no_error(GLenum target)
2663 {
2664 GET_CURRENT_CONTEXT(ctx);
2665 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
2666 struct gl_buffer_object *bufObj = *bufObjPtr;
2667
2668 return unmap_buffer(ctx, bufObj);
2669 }
2670
2671 GLboolean GLAPIENTRY
2672 _mesa_UnmapBuffer(GLenum target)
2673 {
2674 GET_CURRENT_CONTEXT(ctx);
2675 struct gl_buffer_object *bufObj;
2676
2677 bufObj = get_buffer(ctx, "glUnmapBuffer", target, GL_INVALID_OPERATION);
2678 if (!bufObj)
2679 return GL_FALSE;
2680
2681 return validate_and_unmap_buffer(ctx, bufObj, "glUnmapBuffer");
2682 }
2683
2684 GLboolean GLAPIENTRY
2685 _mesa_UnmapNamedBuffer_no_error(GLuint buffer)
2686 {
2687 GET_CURRENT_CONTEXT(ctx);
2688 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2689
2690 return unmap_buffer(ctx, bufObj);
2691 }
2692
2693 GLboolean GLAPIENTRY
2694 _mesa_UnmapNamedBuffer(GLuint buffer)
2695 {
2696 GET_CURRENT_CONTEXT(ctx);
2697 struct gl_buffer_object *bufObj;
2698
2699 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glUnmapNamedBuffer");
2700 if (!bufObj)
2701 return GL_FALSE;
2702
2703 return validate_and_unmap_buffer(ctx, bufObj, "glUnmapNamedBuffer");
2704 }
2705
2706
2707 static bool
2708 get_buffer_parameter(struct gl_context *ctx,
2709 struct gl_buffer_object *bufObj, GLenum pname,
2710 GLint64 *params, const char *func)
2711 {
2712 switch (pname) {
2713 case GL_BUFFER_SIZE_ARB:
2714 *params = bufObj->Size;
2715 break;
2716 case GL_BUFFER_USAGE_ARB:
2717 *params = bufObj->Usage;
2718 break;
2719 case GL_BUFFER_ACCESS_ARB:
2720 *params = simplified_access_mode(ctx,
2721 bufObj->Mappings[MAP_USER].AccessFlags);
2722 break;
2723 case GL_BUFFER_MAPPED_ARB:
2724 *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
2725 break;
2726 case GL_BUFFER_ACCESS_FLAGS:
2727 if (!ctx->Extensions.ARB_map_buffer_range)
2728 goto invalid_pname;
2729 *params = bufObj->Mappings[MAP_USER].AccessFlags;
2730 break;
2731 case GL_BUFFER_MAP_OFFSET:
2732 if (!ctx->Extensions.ARB_map_buffer_range)
2733 goto invalid_pname;
2734 *params = bufObj->Mappings[MAP_USER].Offset;
2735 break;
2736 case GL_BUFFER_MAP_LENGTH:
2737 if (!ctx->Extensions.ARB_map_buffer_range)
2738 goto invalid_pname;
2739 *params = bufObj->Mappings[MAP_USER].Length;
2740 break;
2741 case GL_BUFFER_IMMUTABLE_STORAGE:
2742 if (!ctx->Extensions.ARB_buffer_storage)
2743 goto invalid_pname;
2744 *params = bufObj->Immutable;
2745 break;
2746 case GL_BUFFER_STORAGE_FLAGS:
2747 if (!ctx->Extensions.ARB_buffer_storage)
2748 goto invalid_pname;
2749 *params = bufObj->StorageFlags;
2750 break;
2751 default:
2752 goto invalid_pname;
2753 }
2754
2755 return true;
2756
2757 invalid_pname:
2758 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname: %s)", func,
2759 _mesa_enum_to_string(pname));
2760 return false;
2761 }
2762
2763 void GLAPIENTRY
2764 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
2765 {
2766 GET_CURRENT_CONTEXT(ctx);
2767 struct gl_buffer_object *bufObj;
2768 GLint64 parameter;
2769
2770 bufObj = get_buffer(ctx, "glGetBufferParameteriv", target,
2771 GL_INVALID_OPERATION);
2772 if (!bufObj)
2773 return;
2774
2775 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2776 "glGetBufferParameteriv"))
2777 return; /* Error already recorded. */
2778
2779 *params = (GLint) parameter;
2780 }
2781
2782 void GLAPIENTRY
2783 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
2784 {
2785 GET_CURRENT_CONTEXT(ctx);
2786 struct gl_buffer_object *bufObj;
2787 GLint64 parameter;
2788
2789 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
2790 GL_INVALID_OPERATION);
2791 if (!bufObj)
2792 return;
2793
2794 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2795 "glGetBufferParameteri64v"))
2796 return; /* Error already recorded. */
2797
2798 *params = parameter;
2799 }
2800
2801 void GLAPIENTRY
2802 _mesa_GetNamedBufferParameteriv(GLuint buffer, GLenum pname, GLint *params)
2803 {
2804 GET_CURRENT_CONTEXT(ctx);
2805 struct gl_buffer_object *bufObj;
2806 GLint64 parameter;
2807
2808 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2809 "glGetNamedBufferParameteriv");
2810 if (!bufObj)
2811 return;
2812
2813 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2814 "glGetNamedBufferParameteriv"))
2815 return; /* Error already recorded. */
2816
2817 *params = (GLint) parameter;
2818 }
2819
2820 void GLAPIENTRY
2821 _mesa_GetNamedBufferParameteri64v(GLuint buffer, GLenum pname,
2822 GLint64 *params)
2823 {
2824 GET_CURRENT_CONTEXT(ctx);
2825 struct gl_buffer_object *bufObj;
2826 GLint64 parameter;
2827
2828 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2829 "glGetNamedBufferParameteri64v");
2830 if (!bufObj)
2831 return;
2832
2833 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2834 "glGetNamedBufferParameteri64v"))
2835 return; /* Error already recorded. */
2836
2837 *params = parameter;
2838 }
2839
2840
2841 void GLAPIENTRY
2842 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
2843 {
2844 GET_CURRENT_CONTEXT(ctx);
2845 struct gl_buffer_object *bufObj;
2846
2847 if (pname != GL_BUFFER_MAP_POINTER) {
2848 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointerv(pname != "
2849 "GL_BUFFER_MAP_POINTER)");
2850 return;
2851 }
2852
2853 bufObj = get_buffer(ctx, "glGetBufferPointerv", target,
2854 GL_INVALID_OPERATION);
2855 if (!bufObj)
2856 return;
2857
2858 *params = bufObj->Mappings[MAP_USER].Pointer;
2859 }
2860
2861 void GLAPIENTRY
2862 _mesa_GetNamedBufferPointerv(GLuint buffer, GLenum pname, GLvoid **params)
2863 {
2864 GET_CURRENT_CONTEXT(ctx);
2865 struct gl_buffer_object *bufObj;
2866
2867 if (pname != GL_BUFFER_MAP_POINTER) {
2868 _mesa_error(ctx, GL_INVALID_ENUM, "glGetNamedBufferPointerv(pname != "
2869 "GL_BUFFER_MAP_POINTER)");
2870 return;
2871 }
2872
2873 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2874 "glGetNamedBufferPointerv");
2875 if (!bufObj)
2876 return;
2877
2878 *params = bufObj->Mappings[MAP_USER].Pointer;
2879 }
2880
2881
2882 static void
2883 copy_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *src,
2884 struct gl_buffer_object *dst, GLintptr readOffset,
2885 GLintptr writeOffset, GLsizeiptr size, const char *func)
2886 {
2887 if (_mesa_check_disallowed_mapping(src)) {
2888 _mesa_error(ctx, GL_INVALID_OPERATION,
2889 "%s(readBuffer is mapped)", func);
2890 return;
2891 }
2892
2893 if (_mesa_check_disallowed_mapping(dst)) {
2894 _mesa_error(ctx, GL_INVALID_OPERATION,
2895 "%s(writeBuffer is mapped)", func);
2896 return;
2897 }
2898
2899 if (readOffset < 0) {
2900 _mesa_error(ctx, GL_INVALID_VALUE,
2901 "%s(readOffset %d < 0)", func, (int) readOffset);
2902 return;
2903 }
2904
2905 if (writeOffset < 0) {
2906 _mesa_error(ctx, GL_INVALID_VALUE,
2907 "%s(writeOffset %d < 0)", func, (int) writeOffset);
2908 return;
2909 }
2910
2911 if (size < 0) {
2912 _mesa_error(ctx, GL_INVALID_VALUE,
2913 "%s(size %d < 0)", func, (int) size);
2914 return;
2915 }
2916
2917 if (readOffset + size > src->Size) {
2918 _mesa_error(ctx, GL_INVALID_VALUE,
2919 "%s(readOffset %d + size %d > src_buffer_size %d)", func,
2920 (int) readOffset, (int) size, (int) src->Size);
2921 return;
2922 }
2923
2924 if (writeOffset + size > dst->Size) {
2925 _mesa_error(ctx, GL_INVALID_VALUE,
2926 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func,
2927 (int) writeOffset, (int) size, (int) dst->Size);
2928 return;
2929 }
2930
2931 if (src == dst) {
2932 if (readOffset + size <= writeOffset) {
2933 /* OK */
2934 }
2935 else if (writeOffset + size <= readOffset) {
2936 /* OK */
2937 }
2938 else {
2939 /* overlapping src/dst is illegal */
2940 _mesa_error(ctx, GL_INVALID_VALUE,
2941 "%s(overlapping src/dst)", func);
2942 return;
2943 }
2944 }
2945
2946 dst->MinMaxCacheDirty = true;
2947
2948 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
2949 }
2950
2951 void GLAPIENTRY
2952 _mesa_CopyBufferSubData_no_error(GLenum readTarget, GLenum writeTarget,
2953 GLintptr readOffset, GLintptr writeOffset,
2954 GLsizeiptr size)
2955 {
2956 GET_CURRENT_CONTEXT(ctx);
2957
2958 struct gl_buffer_object **src_ptr = get_buffer_target(ctx, readTarget);
2959 struct gl_buffer_object *src = *src_ptr;
2960
2961 struct gl_buffer_object **dst_ptr = get_buffer_target(ctx, writeTarget);
2962 struct gl_buffer_object *dst = *dst_ptr;
2963
2964 dst->MinMaxCacheDirty = true;
2965 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset,
2966 size);
2967 }
2968
2969 void GLAPIENTRY
2970 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
2971 GLintptr readOffset, GLintptr writeOffset,
2972 GLsizeiptr size)
2973 {
2974 GET_CURRENT_CONTEXT(ctx);
2975 struct gl_buffer_object *src, *dst;
2976
2977 src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
2978 GL_INVALID_OPERATION);
2979 if (!src)
2980 return;
2981
2982 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
2983 GL_INVALID_OPERATION);
2984 if (!dst)
2985 return;
2986
2987 copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
2988 "glCopyBufferSubData");
2989 }
2990
2991 void GLAPIENTRY
2992 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer, GLuint writeBuffer,
2993 GLintptr readOffset,
2994 GLintptr writeOffset, GLsizeiptr size)
2995 {
2996 GET_CURRENT_CONTEXT(ctx);
2997
2998 struct gl_buffer_object *src = _mesa_lookup_bufferobj(ctx, readBuffer);
2999 struct gl_buffer_object *dst = _mesa_lookup_bufferobj(ctx, writeBuffer);
3000
3001 dst->MinMaxCacheDirty = true;
3002 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset,
3003 size);
3004 }
3005
3006 void GLAPIENTRY
3007 _mesa_CopyNamedBufferSubData(GLuint readBuffer, GLuint writeBuffer,
3008 GLintptr readOffset, GLintptr writeOffset,
3009 GLsizeiptr size)
3010 {
3011 GET_CURRENT_CONTEXT(ctx);
3012 struct gl_buffer_object *src, *dst;
3013
3014 src = _mesa_lookup_bufferobj_err(ctx, readBuffer,
3015 "glCopyNamedBufferSubData");
3016 if (!src)
3017 return;
3018
3019 dst = _mesa_lookup_bufferobj_err(ctx, writeBuffer,
3020 "glCopyNamedBufferSubData");
3021 if (!dst)
3022 return;
3023
3024 copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
3025 "glCopyNamedBufferSubData");
3026 }
3027
3028 static bool
3029 validate_map_buffer_range(struct gl_context *ctx,
3030 struct gl_buffer_object *bufObj, GLintptr offset,
3031 GLsizeiptr length, GLbitfield access,
3032 const char *func)
3033 {
3034 GLbitfield allowed_access;
3035
3036 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, false);
3037
3038 if (offset < 0) {
3039 _mesa_error(ctx, GL_INVALID_VALUE,
3040 "%s(offset %ld < 0)", func, (long) offset);
3041 return false;
3042 }
3043
3044 if (length < 0) {
3045 _mesa_error(ctx, GL_INVALID_VALUE,
3046 "%s(length %ld < 0)", func, (long) length);
3047 return false;
3048 }
3049
3050 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
3051 *
3052 * "An INVALID_OPERATION error is generated for any of the following
3053 * conditions:
3054 *
3055 * * <length> is zero."
3056 *
3057 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
3058 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
3059 * either.
3060 */
3061 if (length == 0) {
3062 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(length = 0)", func);
3063 return false;
3064 }
3065
3066 allowed_access = GL_MAP_READ_BIT |
3067 GL_MAP_WRITE_BIT |
3068 GL_MAP_INVALIDATE_RANGE_BIT |
3069 GL_MAP_INVALIDATE_BUFFER_BIT |
3070 GL_MAP_FLUSH_EXPLICIT_BIT |
3071 GL_MAP_UNSYNCHRONIZED_BIT;
3072
3073 if (ctx->Extensions.ARB_buffer_storage) {
3074 allowed_access |= GL_MAP_PERSISTENT_BIT |
3075 GL_MAP_COHERENT_BIT;
3076 }
3077
3078 if (access & ~allowed_access) {
3079 /* generate an error if any bits other than those allowed are set */
3080 _mesa_error(ctx, GL_INVALID_VALUE,
3081 "%s(access has undefined bits set)", func);
3082 return false;
3083 }
3084
3085 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
3086 _mesa_error(ctx, GL_INVALID_OPERATION,
3087 "%s(access indicates neither read or write)", func);
3088 return false;
3089 }
3090
3091 if ((access & GL_MAP_READ_BIT) &&
3092 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
3093 GL_MAP_INVALIDATE_BUFFER_BIT |
3094 GL_MAP_UNSYNCHRONIZED_BIT))) {
3095 _mesa_error(ctx, GL_INVALID_OPERATION,
3096 "%s(read access with disallowed bits)", func);
3097 return false;
3098 }
3099
3100 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
3101 ((access & GL_MAP_WRITE_BIT) == 0)) {
3102 _mesa_error(ctx, GL_INVALID_OPERATION,
3103 "%s(access has flush explicit without write)", func);
3104 return false;
3105 }
3106
3107 if (access & GL_MAP_READ_BIT &&
3108 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
3109 _mesa_error(ctx, GL_INVALID_OPERATION,
3110 "%s(buffer does not allow read access)", func);
3111 return false;
3112 }
3113
3114 if (access & GL_MAP_WRITE_BIT &&
3115 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
3116 _mesa_error(ctx, GL_INVALID_OPERATION,
3117 "%s(buffer does not allow write access)", func);
3118 return false;
3119 }
3120
3121 if (access & GL_MAP_COHERENT_BIT &&
3122 !(bufObj->StorageFlags & GL_MAP_COHERENT_BIT)) {
3123 _mesa_error(ctx, GL_INVALID_OPERATION,
3124 "%s(buffer does not allow coherent access)", func);
3125 return false;
3126 }
3127
3128 if (access & GL_MAP_PERSISTENT_BIT &&
3129 !(bufObj->StorageFlags & GL_MAP_PERSISTENT_BIT)) {
3130 _mesa_error(ctx, GL_INVALID_OPERATION,
3131 "%s(buffer does not allow persistent access)", func);
3132 return false;
3133 }
3134
3135 if (offset + length > bufObj->Size) {
3136 _mesa_error(ctx, GL_INVALID_VALUE,
3137 "%s(offset %lu + length %lu > buffer_size %lu)", func,
3138 (unsigned long) offset, (unsigned long) length,
3139 (unsigned long) bufObj->Size);
3140 return false;
3141 }
3142
3143 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
3144 _mesa_error(ctx, GL_INVALID_OPERATION,
3145 "%s(buffer already mapped)", func);
3146 return false;
3147 }
3148
3149 if (access & GL_MAP_WRITE_BIT) {
3150 bufObj->NumMapBufferWriteCalls++;
3151 if ((bufObj->Usage == GL_STATIC_DRAW ||
3152 bufObj->Usage == GL_STATIC_COPY) &&
3153 bufObj->NumMapBufferWriteCalls >= BUFFER_WARNING_CALL_COUNT) {
3154 BUFFER_USAGE_WARNING(ctx,
3155 "using %s(buffer %u, offset %u, length %u) to "
3156 "update a %s buffer",
3157 func, bufObj->Name, offset, length,
3158 _mesa_enum_to_string(bufObj->Usage));
3159 }
3160 }
3161
3162 return true;
3163 }
3164
3165 static void *
3166 map_buffer_range(struct gl_context *ctx, struct gl_buffer_object *bufObj,
3167 GLintptr offset, GLsizeiptr length, GLbitfield access,
3168 const char *func)
3169 {
3170 if (!bufObj->Size) {
3171 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(buffer size = 0)", func);
3172 return NULL;
3173 }
3174
3175 assert(ctx->Driver.MapBufferRange);
3176 void *map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj,
3177 MAP_USER);
3178 if (!map) {
3179 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(map failed)", func);
3180 }
3181 else {
3182 /* The driver callback should have set all these fields.
3183 * This is important because other modules (like VBO) might call
3184 * the driver function directly.
3185 */
3186 assert(bufObj->Mappings[MAP_USER].Pointer == map);
3187 assert(bufObj->Mappings[MAP_USER].Length == length);
3188 assert(bufObj->Mappings[MAP_USER].Offset == offset);
3189 assert(bufObj->Mappings[MAP_USER].AccessFlags == access);
3190 }
3191
3192 if (access & GL_MAP_WRITE_BIT) {
3193 bufObj->Written = GL_TRUE;
3194 bufObj->MinMaxCacheDirty = true;
3195 }
3196
3197 #ifdef VBO_DEBUG
3198 if (strstr(func, "Range") == NULL) { /* If not MapRange */
3199 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
3200 bufObj->Name, bufObj->Size, access);
3201 /* Access must be write only */
3202 if ((access & GL_MAP_WRITE_BIT) && (!(access & ~GL_MAP_WRITE_BIT))) {
3203 GLuint i;
3204 GLubyte *b = (GLubyte *) bufObj->Pointer;
3205 for (i = 0; i < bufObj->Size; i++)
3206 b[i] = i & 0xff;
3207 }
3208 }
3209 #endif
3210
3211 #ifdef BOUNDS_CHECK
3212 if (strstr(func, "Range") == NULL) { /* If not MapRange */
3213 GLubyte *buf = (GLubyte *) bufObj->Pointer;
3214 GLuint i;
3215 /* buffer is 100 bytes larger than requested, fill with magic value */
3216 for (i = 0; i < 100; i++) {
3217 buf[bufObj->Size - i - 1] = 123;
3218 }
3219 }
3220 #endif
3221
3222 return map;
3223 }
3224
3225 void * GLAPIENTRY
3226 _mesa_MapBufferRange_no_error(GLenum target, GLintptr offset,
3227 GLsizeiptr length, GLbitfield access)
3228 {
3229 GET_CURRENT_CONTEXT(ctx);
3230
3231 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
3232 struct gl_buffer_object *bufObj = *bufObjPtr;
3233
3234 return map_buffer_range(ctx, bufObj, offset, length, access,
3235 "glMapBufferRange");
3236 }
3237
3238 void * GLAPIENTRY
3239 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
3240 GLbitfield access)
3241 {
3242 GET_CURRENT_CONTEXT(ctx);
3243 struct gl_buffer_object *bufObj;
3244
3245 if (!ctx->Extensions.ARB_map_buffer_range) {
3246 _mesa_error(ctx, GL_INVALID_OPERATION,
3247 "glMapBufferRange(ARB_map_buffer_range not supported)");
3248 return NULL;
3249 }
3250
3251 bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
3252 if (!bufObj)
3253 return NULL;
3254
3255 if (!validate_map_buffer_range(ctx, bufObj, offset, length, access,
3256 "glMapBufferRange"))
3257 return NULL;
3258
3259 return map_buffer_range(ctx, bufObj, offset, length, access,
3260 "glMapBufferRange");
3261 }
3262
3263 void * GLAPIENTRY
3264 _mesa_MapNamedBufferRange_no_error(GLuint buffer, GLintptr offset,
3265 GLsizeiptr length, GLbitfield access)
3266 {
3267 GET_CURRENT_CONTEXT(ctx);
3268 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3269
3270 return map_buffer_range(ctx, bufObj, offset, length, access,
3271 "glMapNamedBufferRange");
3272 }
3273
3274 void * GLAPIENTRY
3275 _mesa_MapNamedBufferRange(GLuint buffer, GLintptr offset, GLsizeiptr length,
3276 GLbitfield access)
3277 {
3278 GET_CURRENT_CONTEXT(ctx);
3279 struct gl_buffer_object *bufObj;
3280
3281 if (!ctx->Extensions.ARB_map_buffer_range) {
3282 _mesa_error(ctx, GL_INVALID_OPERATION,
3283 "glMapNamedBufferRange("
3284 "ARB_map_buffer_range not supported)");
3285 return NULL;
3286 }
3287
3288 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBufferRange");
3289 if (!bufObj)
3290 return NULL;
3291
3292 if (!validate_map_buffer_range(ctx, bufObj, offset, length, access,
3293 "glMapNamedBufferRange"))
3294 return NULL;
3295
3296 return map_buffer_range(ctx, bufObj, offset, length, access,
3297 "glMapNamedBufferRange");
3298 }
3299
3300 /**
3301 * Converts GLenum access from MapBuffer and MapNamedBuffer into
3302 * flags for input to map_buffer_range.
3303 *
3304 * \return true if the type of requested access is permissible.
3305 */
3306 static bool
3307 get_map_buffer_access_flags(struct gl_context *ctx, GLenum access,
3308 GLbitfield *flags)
3309 {
3310 switch (access) {
3311 case GL_READ_ONLY_ARB:
3312 *flags = GL_MAP_READ_BIT;
3313 return _mesa_is_desktop_gl(ctx);
3314 case GL_WRITE_ONLY_ARB:
3315 *flags = GL_MAP_WRITE_BIT;
3316 return true;
3317 case GL_READ_WRITE_ARB:
3318 *flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
3319 return _mesa_is_desktop_gl(ctx);
3320 default:
3321 *flags = 0;
3322 return false;
3323 }
3324 }
3325
3326 void * GLAPIENTRY
3327 _mesa_MapBuffer_no_error(GLenum target, GLenum access)
3328 {
3329 GET_CURRENT_CONTEXT(ctx);
3330
3331 GLbitfield accessFlags;
3332 get_map_buffer_access_flags(ctx, access, &accessFlags);
3333
3334 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
3335 struct gl_buffer_object *bufObj = *bufObjPtr;
3336
3337 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3338 "glMapBuffer");
3339 }
3340
3341 void * GLAPIENTRY
3342 _mesa_MapBuffer(GLenum target, GLenum access)
3343 {
3344 GET_CURRENT_CONTEXT(ctx);
3345 struct gl_buffer_object *bufObj;
3346 GLbitfield accessFlags;
3347
3348 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
3349 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBuffer(invalid access)");
3350 return NULL;
3351 }
3352
3353 bufObj = get_buffer(ctx, "glMapBuffer", target, GL_INVALID_OPERATION);
3354 if (!bufObj)
3355 return NULL;
3356
3357 if (!validate_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3358 "glMapBuffer"))
3359 return NULL;
3360
3361 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3362 "glMapBuffer");
3363 }
3364
3365 void * GLAPIENTRY
3366 _mesa_MapNamedBuffer_no_error(GLuint buffer, GLenum access)
3367 {
3368 GET_CURRENT_CONTEXT(ctx);
3369
3370 GLbitfield accessFlags;
3371 get_map_buffer_access_flags(ctx, access, &accessFlags);
3372
3373 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3374
3375 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3376 "glMapNamedBuffer");
3377 }
3378
3379 void * GLAPIENTRY
3380 _mesa_MapNamedBuffer(GLuint buffer, GLenum access)
3381 {
3382 GET_CURRENT_CONTEXT(ctx);
3383 struct gl_buffer_object *bufObj;
3384 GLbitfield accessFlags;
3385
3386 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
3387 _mesa_error(ctx, GL_INVALID_ENUM, "glMapNamedBuffer(invalid access)");
3388 return NULL;
3389 }
3390
3391 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBuffer");
3392 if (!bufObj)
3393 return NULL;
3394
3395 if (!validate_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3396 "glMapNamedBuffer"))
3397 return NULL;
3398
3399 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3400 "glMapNamedBuffer");
3401 }
3402
3403
3404 static void
3405 flush_mapped_buffer_range(struct gl_context *ctx,
3406 struct gl_buffer_object *bufObj,
3407 GLintptr offset, GLsizeiptr length,
3408 const char *func)
3409 {
3410 if (!ctx->Extensions.ARB_map_buffer_range) {
3411 _mesa_error(ctx, GL_INVALID_OPERATION,
3412 "%s(ARB_map_buffer_range not supported)", func);
3413 return;
3414 }
3415
3416 if (offset < 0) {
3417 _mesa_error(ctx, GL_INVALID_VALUE,
3418 "%s(offset %ld < 0)", func, (long) offset);
3419 return;
3420 }
3421
3422 if (length < 0) {
3423 _mesa_error(ctx, GL_INVALID_VALUE,
3424 "%s(length %ld < 0)", func, (long) length);
3425 return;
3426 }
3427
3428 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
3429 /* buffer is not mapped */
3430 _mesa_error(ctx, GL_INVALID_OPERATION,
3431 "%s(buffer is not mapped)", func);
3432 return;
3433 }
3434
3435 if ((bufObj->Mappings[MAP_USER].AccessFlags &
3436 GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
3437 _mesa_error(ctx, GL_INVALID_OPERATION,
3438 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func);
3439 return;
3440 }
3441
3442 if (offset + length > bufObj->Mappings[MAP_USER].Length) {
3443 _mesa_error(ctx, GL_INVALID_VALUE,
3444 "%s(offset %ld + length %ld > mapped length %ld)", func,
3445 (long) offset, (long) length,
3446 (long) bufObj->Mappings[MAP_USER].Length);
3447 return;
3448 }
3449
3450 assert(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_WRITE_BIT);
3451
3452 if (ctx->Driver.FlushMappedBufferRange)
3453 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
3454 MAP_USER);
3455 }
3456
3457 void GLAPIENTRY
3458 _mesa_FlushMappedBufferRange_no_error(GLenum target, GLintptr offset,
3459 GLsizeiptr length)
3460 {
3461 GET_CURRENT_CONTEXT(ctx);
3462 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
3463 struct gl_buffer_object *bufObj = *bufObjPtr;
3464
3465 if (ctx->Driver.FlushMappedBufferRange)
3466 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
3467 MAP_USER);
3468 }
3469
3470 void GLAPIENTRY
3471 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset,
3472 GLsizeiptr length)
3473 {
3474 GET_CURRENT_CONTEXT(ctx);
3475 struct gl_buffer_object *bufObj;
3476
3477 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
3478 GL_INVALID_OPERATION);
3479 if (!bufObj)
3480 return;
3481
3482 flush_mapped_buffer_range(ctx, bufObj, offset, length,
3483 "glFlushMappedBufferRange");
3484 }
3485
3486 void GLAPIENTRY
3487 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer, GLintptr offset,
3488 GLsizeiptr length)
3489 {
3490 GET_CURRENT_CONTEXT(ctx);
3491 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3492
3493 if (ctx->Driver.FlushMappedBufferRange)
3494 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
3495 MAP_USER);
3496 }
3497
3498 void GLAPIENTRY
3499 _mesa_FlushMappedNamedBufferRange(GLuint buffer, GLintptr offset,
3500 GLsizeiptr length)
3501 {
3502 GET_CURRENT_CONTEXT(ctx);
3503 struct gl_buffer_object *bufObj;
3504
3505 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
3506 "glFlushMappedNamedBufferRange");
3507 if (!bufObj)
3508 return;
3509
3510 flush_mapped_buffer_range(ctx, bufObj, offset, length,
3511 "glFlushMappedNamedBufferRange");
3512 }
3513
3514 static void
3515 bind_buffer_range_uniform_buffer(struct gl_context *ctx, GLuint index,
3516 struct gl_buffer_object *bufObj,
3517 GLintptr offset, GLsizeiptr size)
3518 {
3519 if (bufObj == ctx->Shared->NullBufferObj) {
3520 offset = -1;
3521 size = -1;
3522 }
3523
3524 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
3525 bind_uniform_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
3526 }
3527
3528 /**
3529 * Bind a region of a buffer object to a uniform block binding point.
3530 * \param index the uniform buffer binding point index
3531 * \param bufObj the buffer object
3532 * \param offset offset to the start of buffer object region
3533 * \param size size of the buffer object region
3534 */
3535 static void
3536 bind_buffer_range_uniform_buffer_err(struct gl_context *ctx, GLuint index,
3537 struct gl_buffer_object *bufObj,
3538 GLintptr offset, GLsizeiptr size)
3539 {
3540 if (index >= ctx->Const.MaxUniformBufferBindings) {
3541 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
3542 return;
3543 }
3544
3545 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3546 _mesa_error(ctx, GL_INVALID_VALUE,
3547 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
3548 ctx->Const.UniformBufferOffsetAlignment);
3549 return;
3550 }
3551
3552 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
3553 }
3554
3555 static void
3556 bind_buffer_range_shader_storage_buffer(struct gl_context *ctx,
3557 GLuint index,
3558 struct gl_buffer_object *bufObj,
3559 GLintptr offset,
3560 GLsizeiptr size)
3561 {
3562 if (bufObj == ctx->Shared->NullBufferObj) {
3563 offset = -1;
3564 size = -1;
3565 }
3566
3567 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
3568 bind_shader_storage_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
3569 }
3570
3571 /**
3572 * Bind a region of a buffer object to a shader storage block binding point.
3573 * \param index the shader storage buffer binding point index
3574 * \param bufObj the buffer object
3575 * \param offset offset to the start of buffer object region
3576 * \param size size of the buffer object region
3577 */
3578 static void
3579 bind_buffer_range_shader_storage_buffer_err(struct gl_context *ctx,
3580 GLuint index,
3581 struct gl_buffer_object *bufObj,
3582 GLintptr offset, GLsizeiptr size)
3583 {
3584 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
3585 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
3586 return;
3587 }
3588
3589 if (offset & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
3590 _mesa_error(ctx, GL_INVALID_VALUE,
3591 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
3592 ctx->Const.ShaderStorageBufferOffsetAlignment);
3593 return;
3594 }
3595
3596 bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset, size);
3597 }
3598
3599 static void
3600 bind_buffer_range_atomic_buffer(struct gl_context *ctx, GLuint index,
3601 struct gl_buffer_object *bufObj,
3602 GLintptr offset, GLsizeiptr size)
3603 {
3604 if (bufObj == ctx->Shared->NullBufferObj) {
3605 offset = -1;
3606 size = -1;
3607 }
3608
3609 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
3610 bind_atomic_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
3611 }
3612
3613 /**
3614 * Bind a region of a buffer object to an atomic storage block binding point.
3615 * \param index the shader storage buffer binding point index
3616 * \param bufObj the buffer object
3617 * \param offset offset to the start of buffer object region
3618 * \param size size of the buffer object region
3619 */
3620 static void
3621 bind_buffer_range_atomic_buffer_err(struct gl_context *ctx,
3622 GLuint index,
3623 struct gl_buffer_object *bufObj,
3624 GLintptr offset, GLsizeiptr size)
3625 {
3626 if (index >= ctx->Const.MaxAtomicBufferBindings) {
3627 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
3628 return;
3629 }
3630
3631 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
3632 _mesa_error(ctx, GL_INVALID_VALUE,
3633 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
3634 ATOMIC_COUNTER_SIZE);
3635 return;
3636 }
3637
3638 bind_buffer_range_atomic_buffer(ctx, index, bufObj, offset, size);
3639 }
3640
3641 static inline bool
3642 bind_buffers_check_offset_and_size(struct gl_context *ctx,
3643 GLuint index,
3644 const GLintptr *offsets,
3645 const GLsizeiptr *sizes)
3646 {
3647 if (offsets[index] < 0) {
3648 /* The ARB_multi_bind spec says:
3649 *
3650 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3651 * value in <offsets> is less than zero (per binding)."
3652 */
3653 _mesa_error(ctx, GL_INVALID_VALUE,
3654 "glBindBuffersRange(offsets[%u]=%" PRId64 " < 0)",
3655 index, (int64_t) offsets[index]);
3656 return false;
3657 }
3658
3659 if (sizes[index] <= 0) {
3660 /* The ARB_multi_bind spec says:
3661 *
3662 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3663 * value in <sizes> is less than or equal to zero (per binding)."
3664 */
3665 _mesa_error(ctx, GL_INVALID_VALUE,
3666 "glBindBuffersRange(sizes[%u]=%" PRId64 " <= 0)",
3667 index, (int64_t) sizes[index]);
3668 return false;
3669 }
3670
3671 return true;
3672 }
3673
3674 static bool
3675 error_check_bind_uniform_buffers(struct gl_context *ctx,
3676 GLuint first, GLsizei count,
3677 const char *caller)
3678 {
3679 if (!ctx->Extensions.ARB_uniform_buffer_object) {
3680 _mesa_error(ctx, GL_INVALID_ENUM,
3681 "%s(target=GL_UNIFORM_BUFFER)", caller);
3682 return false;
3683 }
3684
3685 /* The ARB_multi_bind_spec says:
3686 *
3687 * "An INVALID_OPERATION error is generated if <first> + <count> is
3688 * greater than the number of target-specific indexed binding points,
3689 * as described in section 6.7.1."
3690 */
3691 if (first + count > ctx->Const.MaxUniformBufferBindings) {
3692 _mesa_error(ctx, GL_INVALID_OPERATION,
3693 "%s(first=%u + count=%d > the value of "
3694 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3695 caller, first, count,
3696 ctx->Const.MaxUniformBufferBindings);
3697 return false;
3698 }
3699
3700 return true;
3701 }
3702
3703 static bool
3704 error_check_bind_shader_storage_buffers(struct gl_context *ctx,
3705 GLuint first, GLsizei count,
3706 const char *caller)
3707 {
3708 if (!ctx->Extensions.ARB_shader_storage_buffer_object) {
3709 _mesa_error(ctx, GL_INVALID_ENUM,
3710 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller);
3711 return false;
3712 }
3713
3714 /* The ARB_multi_bind_spec says:
3715 *
3716 * "An INVALID_OPERATION error is generated if <first> + <count> is
3717 * greater than the number of target-specific indexed binding points,
3718 * as described in section 6.7.1."
3719 */
3720 if (first + count > ctx->Const.MaxShaderStorageBufferBindings) {
3721 _mesa_error(ctx, GL_INVALID_OPERATION,
3722 "%s(first=%u + count=%d > the value of "
3723 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3724 caller, first, count,
3725 ctx->Const.MaxShaderStorageBufferBindings);
3726 return false;
3727 }
3728
3729 return true;
3730 }
3731
3732 /**
3733 * Unbind all uniform buffers in the range
3734 * <first> through <first>+<count>-1
3735 */
3736 static void
3737 unbind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3738 {
3739 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
3740
3741 for (int i = 0; i < count; i++)
3742 set_ubo_binding(ctx, &ctx->UniformBufferBindings[first + i],
3743 bufObj, -1, -1, GL_TRUE);
3744 }
3745
3746 /**
3747 * Unbind all shader storage buffers in the range
3748 * <first> through <first>+<count>-1
3749 */
3750 static void
3751 unbind_shader_storage_buffers(struct gl_context *ctx, GLuint first,
3752 GLsizei count)
3753 {
3754 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
3755
3756 for (int i = 0; i < count; i++)
3757 set_ssbo_binding(ctx, &ctx->ShaderStorageBufferBindings[first + i],
3758 bufObj, -1, -1, GL_TRUE);
3759 }
3760
3761 static void
3762 bind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count,
3763 const GLuint *buffers,
3764 bool range,
3765 const GLintptr *offsets, const GLsizeiptr *sizes,
3766 const char *caller)
3767 {
3768 if (!error_check_bind_uniform_buffers(ctx, first, count, caller))
3769 return;
3770
3771 /* Assume that at least one binding will be changed */
3772 FLUSH_VERTICES(ctx, 0);
3773 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3774
3775 if (!buffers) {
3776 /* The ARB_multi_bind spec says:
3777 *
3778 * "If <buffers> is NULL, all bindings from <first> through
3779 * <first>+<count>-1 are reset to their unbound (zero) state.
3780 * In this case, the offsets and sizes associated with the
3781 * binding points are set to default values, ignoring
3782 * <offsets> and <sizes>."
3783 */
3784 unbind_uniform_buffers(ctx, first, count);
3785 return;
3786 }
3787
3788 /* Note that the error semantics for multi-bind commands differ from
3789 * those of other GL commands.
3790 *
3791 * The Issues section in the ARB_multi_bind spec says:
3792 *
3793 * "(11) Typically, OpenGL specifies that if an error is generated by a
3794 * command, that command has no effect. This is somewhat
3795 * unfortunate for multi-bind commands, because it would require a
3796 * first pass to scan the entire list of bound objects for errors
3797 * and then a second pass to actually perform the bindings.
3798 * Should we have different error semantics?
3799 *
3800 * RESOLVED: Yes. In this specification, when the parameters for
3801 * one of the <count> binding points are invalid, that binding point
3802 * is not updated and an error will be generated. However, other
3803 * binding points in the same command will be updated if their
3804 * parameters are valid and no other error occurs."
3805 */
3806
3807 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
3808
3809 for (int i = 0; i < count; i++) {
3810 struct gl_uniform_buffer_binding *binding =
3811 &ctx->UniformBufferBindings[first + i];
3812 struct gl_buffer_object *bufObj;
3813 GLintptr offset = 0;
3814 GLsizeiptr size = 0;
3815
3816 if (range) {
3817 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3818 continue;
3819
3820 /* The ARB_multi_bind spec says:
3821 *
3822 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3823 * pair of values in <offsets> and <sizes> does not respectively
3824 * satisfy the constraints described for those parameters for the
3825 * specified target, as described in section 6.7.1 (per binding)."
3826 *
3827 * Section 6.7.1 refers to table 6.5, which says:
3828 *
3829 * "┌───────────────────────────────────────────────────────────────┐
3830 * │ Uniform buffer array bindings (see sec. 7.6) │
3831 * ├─────────────────────┬─────────────────────────────────────────┤
3832 * │ ... │ ... │
3833 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3834 * │ │ OFFSET_ALIGNMENT │
3835 * │ ... │ ... │
3836 * │ size restriction │ none │
3837 * └─────────────────────┴─────────────────────────────────────────┘"
3838 */
3839 if (offsets[i] & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3840 _mesa_error(ctx, GL_INVALID_VALUE,
3841 "glBindBuffersRange(offsets[%u]=%" PRId64
3842 " is misaligned; it must be a multiple of the value of "
3843 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3844 "target=GL_UNIFORM_BUFFER)",
3845 i, (int64_t) offsets[i],
3846 ctx->Const.UniformBufferOffsetAlignment);
3847 continue;
3848 }
3849
3850 offset = offsets[i];
3851 size = sizes[i];
3852 }
3853
3854 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3855 bufObj = binding->BufferObject;
3856 else
3857 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller);
3858
3859 if (bufObj) {
3860 if (bufObj == ctx->Shared->NullBufferObj)
3861 set_ubo_binding(ctx, binding, bufObj, -1, -1, !range);
3862 else
3863 set_ubo_binding(ctx, binding, bufObj, offset, size, !range);
3864 }
3865 }
3866
3867 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
3868 }
3869
3870 static void
3871 bind_shader_storage_buffers(struct gl_context *ctx, GLuint first,
3872 GLsizei count, const GLuint *buffers,
3873 bool range,
3874 const GLintptr *offsets,
3875 const GLsizeiptr *sizes,
3876 const char *caller)
3877 {
3878 if (!error_check_bind_shader_storage_buffers(ctx, first, count, caller))
3879 return;
3880
3881 /* Assume that at least one binding will be changed */
3882 FLUSH_VERTICES(ctx, 0);
3883 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
3884
3885 if (!buffers) {
3886 /* The ARB_multi_bind spec says:
3887 *
3888 * "If <buffers> is NULL, all bindings from <first> through
3889 * <first>+<count>-1 are reset to their unbound (zero) state.
3890 * In this case, the offsets and sizes associated with the
3891 * binding points are set to default values, ignoring
3892 * <offsets> and <sizes>."
3893 */
3894 unbind_shader_storage_buffers(ctx, first, count);
3895 return;
3896 }
3897
3898 /* Note that the error semantics for multi-bind commands differ from
3899 * those of other GL commands.
3900 *
3901 * The Issues section in the ARB_multi_bind spec says:
3902 *
3903 * "(11) Typically, OpenGL specifies that if an error is generated by a
3904 * command, that command has no effect. This is somewhat
3905 * unfortunate for multi-bind commands, because it would require a
3906 * first pass to scan the entire list of bound objects for errors
3907 * and then a second pass to actually perform the bindings.
3908 * Should we have different error semantics?
3909 *
3910 * RESOLVED: Yes. In this specification, when the parameters for
3911 * one of the <count> binding points are invalid, that binding point
3912 * is not updated and an error will be generated. However, other
3913 * binding points in the same command will be updated if their
3914 * parameters are valid and no other error occurs."
3915 */
3916
3917 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
3918
3919 for (int i = 0; i < count; i++) {
3920 struct gl_shader_storage_buffer_binding *binding =
3921 &ctx->ShaderStorageBufferBindings[first + i];
3922 struct gl_buffer_object *bufObj;
3923 GLintptr offset = 0;
3924 GLsizeiptr size = 0;
3925
3926 if (range) {
3927 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3928 continue;
3929
3930 /* The ARB_multi_bind spec says:
3931 *
3932 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3933 * pair of values in <offsets> and <sizes> does not respectively
3934 * satisfy the constraints described for those parameters for the
3935 * specified target, as described in section 6.7.1 (per binding)."
3936 *
3937 * Section 6.7.1 refers to table 6.5, which says:
3938 *
3939 * "┌───────────────────────────────────────────────────────────────┐
3940 * │ Shader storage buffer array bindings (see sec. 7.8) │
3941 * ├─────────────────────┬─────────────────────────────────────────┤
3942 * │ ... │ ... │
3943 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3944 * │ │ BUFFER_OFFSET_ALIGNMENT │
3945 * │ ... │ ... │
3946 * │ size restriction │ none │
3947 * └─────────────────────┴─────────────────────────────────────────┘"
3948 */
3949 if (offsets[i] & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
3950 _mesa_error(ctx, GL_INVALID_VALUE,
3951 "glBindBuffersRange(offsets[%u]=%" PRId64
3952 " is misaligned; it must be a multiple of the value of "
3953 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3954 "target=GL_SHADER_STORAGE_BUFFER)",
3955 i, (int64_t) offsets[i],
3956 ctx->Const.ShaderStorageBufferOffsetAlignment);
3957 continue;
3958 }
3959
3960 offset = offsets[i];
3961 size = sizes[i];
3962 }
3963
3964 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3965 bufObj = binding->BufferObject;
3966 else
3967 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller);
3968
3969 if (bufObj) {
3970 if (bufObj == ctx->Shared->NullBufferObj)
3971 set_ssbo_binding(ctx, binding, bufObj, -1, -1, !range);
3972 else
3973 set_ssbo_binding(ctx, binding, bufObj, offset, size, !range);
3974 }
3975 }
3976
3977 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
3978 }
3979
3980 static bool
3981 error_check_bind_xfb_buffers(struct gl_context *ctx,
3982 struct gl_transform_feedback_object *tfObj,
3983 GLuint first, GLsizei count, const char *caller)
3984 {
3985 if (!ctx->Extensions.EXT_transform_feedback) {
3986 _mesa_error(ctx, GL_INVALID_ENUM,
3987 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller);
3988 return false;
3989 }
3990
3991 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3992 *
3993 * "An INVALID_OPERATION error is generated :
3994 *
3995 * ...
3996 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3997 * FEEDBACK_BUFFER and transform feedback is currently active."
3998 *
3999 * We assume that this is also meant to apply to BindBuffersRange
4000 * and BindBuffersBase.
4001 */
4002 if (tfObj->Active) {
4003 _mesa_error(ctx, GL_INVALID_OPERATION,
4004 "%s(Changing transform feedback buffers while "
4005 "transform feedback is active)", caller);
4006 return false;
4007 }
4008
4009 /* The ARB_multi_bind_spec says:
4010 *
4011 * "An INVALID_OPERATION error is generated if <first> + <count> is
4012 * greater than the number of target-specific indexed binding points,
4013 * as described in section 6.7.1."
4014 */
4015 if (first + count > ctx->Const.MaxTransformFeedbackBuffers) {
4016 _mesa_error(ctx, GL_INVALID_OPERATION,
4017 "%s(first=%u + count=%d > the value of "
4018 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
4019 caller, first, count,
4020 ctx->Const.MaxTransformFeedbackBuffers);
4021 return false;
4022 }
4023
4024 return true;
4025 }
4026
4027 /**
4028 * Unbind all transform feedback buffers in the range
4029 * <first> through <first>+<count>-1
4030 */
4031 static void
4032 unbind_xfb_buffers(struct gl_context *ctx,
4033 struct gl_transform_feedback_object *tfObj,
4034 GLuint first, GLsizei count)
4035 {
4036 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
4037
4038 for (int i = 0; i < count; i++)
4039 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
4040 bufObj, 0, 0);
4041 }
4042
4043 static void
4044 bind_xfb_buffers(struct gl_context *ctx,
4045 GLuint first, GLsizei count,
4046 const GLuint *buffers,
4047 bool range,
4048 const GLintptr *offsets,
4049 const GLsizeiptr *sizes,
4050 const char *caller)
4051 {
4052 struct gl_transform_feedback_object *tfObj =
4053 ctx->TransformFeedback.CurrentObject;
4054
4055 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count, caller))
4056 return;
4057
4058 /* Assume that at least one binding will be changed */
4059 FLUSH_VERTICES(ctx, 0);
4060 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
4061
4062 if (!buffers) {
4063 /* The ARB_multi_bind spec says:
4064 *
4065 * "If <buffers> is NULL, all bindings from <first> through
4066 * <first>+<count>-1 are reset to their unbound (zero) state.
4067 * In this case, the offsets and sizes associated with the
4068 * binding points are set to default values, ignoring
4069 * <offsets> and <sizes>."
4070 */
4071 unbind_xfb_buffers(ctx, tfObj, first, count);
4072 return;
4073 }
4074
4075 /* Note that the error semantics for multi-bind commands differ from
4076 * those of other GL commands.
4077 *
4078 * The Issues section in the ARB_multi_bind spec says:
4079 *
4080 * "(11) Typically, OpenGL specifies that if an error is generated by a
4081 * command, that command has no effect. This is somewhat
4082 * unfortunate for multi-bind commands, because it would require a
4083 * first pass to scan the entire list of bound objects for errors
4084 * and then a second pass to actually perform the bindings.
4085 * Should we have different error semantics?
4086 *
4087 * RESOLVED: Yes. In this specification, when the parameters for
4088 * one of the <count> binding points are invalid, that binding point
4089 * is not updated and an error will be generated. However, other
4090 * binding points in the same command will be updated if their
4091 * parameters are valid and no other error occurs."
4092 */
4093
4094 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
4095
4096 for (int i = 0; i < count; i++) {
4097 const GLuint index = first + i;
4098 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[index];
4099 struct gl_buffer_object *bufObj;
4100 GLintptr offset = 0;
4101 GLsizeiptr size = 0;
4102
4103 if (range) {
4104 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
4105 continue;
4106
4107 /* The ARB_multi_bind spec says:
4108 *
4109 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4110 * pair of values in <offsets> and <sizes> does not respectively
4111 * satisfy the constraints described for those parameters for the
4112 * specified target, as described in section 6.7.1 (per binding)."
4113 *
4114 * Section 6.7.1 refers to table 6.5, which says:
4115 *
4116 * "┌───────────────────────────────────────────────────────────────┐
4117 * │ Transform feedback array bindings (see sec. 13.2.2) │
4118 * ├───────────────────────┬───────────────────────────────────────┤
4119 * │ ... │ ... │
4120 * │ offset restriction │ multiple of 4 │
4121 * │ ... │ ... │
4122 * │ size restriction │ multiple of 4 │
4123 * └───────────────────────┴───────────────────────────────────────┘"
4124 */
4125 if (offsets[i] & 0x3) {
4126 _mesa_error(ctx, GL_INVALID_VALUE,
4127 "glBindBuffersRange(offsets[%u]=%" PRId64
4128 " is misaligned; it must be a multiple of 4 when "
4129 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4130 i, (int64_t) offsets[i]);
4131 continue;
4132 }
4133
4134 if (sizes[i] & 0x3) {
4135 _mesa_error(ctx, GL_INVALID_VALUE,
4136 "glBindBuffersRange(sizes[%u]=%" PRId64
4137 " is misaligned; it must be a multiple of 4 when "
4138 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4139 i, (int64_t) sizes[i]);
4140 continue;
4141 }
4142
4143 offset = offsets[i];
4144 size = sizes[i];
4145 }
4146
4147 if (boundBufObj && boundBufObj->Name == buffers[i])
4148 bufObj = boundBufObj;
4149 else
4150 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller);
4151
4152 if (bufObj)
4153 _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj,
4154 offset, size);
4155 }
4156
4157 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
4158 }
4159
4160 static bool
4161 error_check_bind_atomic_buffers(struct gl_context *ctx,
4162 GLuint first, GLsizei count,
4163 const char *caller)
4164 {
4165 if (!ctx->Extensions.ARB_shader_atomic_counters) {
4166 _mesa_error(ctx, GL_INVALID_ENUM,
4167 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller);
4168 return false;
4169 }
4170
4171 /* The ARB_multi_bind_spec says:
4172 *
4173 * "An INVALID_OPERATION error is generated if <first> + <count> is
4174 * greater than the number of target-specific indexed binding points,
4175 * as described in section 6.7.1."
4176 */
4177 if (first + count > ctx->Const.MaxAtomicBufferBindings) {
4178 _mesa_error(ctx, GL_INVALID_OPERATION,
4179 "%s(first=%u + count=%d > the value of "
4180 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4181 caller, first, count, ctx->Const.MaxAtomicBufferBindings);
4182 return false;
4183 }
4184
4185 return true;
4186 }
4187
4188 /**
4189 * Unbind all atomic counter buffers in the range
4190 * <first> through <first>+<count>-1
4191 */
4192 static void
4193 unbind_atomic_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
4194 {
4195 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
4196
4197 for (int i = 0; i < count; i++)
4198 set_atomic_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i],
4199 bufObj, -1, -1, GL_TRUE);
4200 }
4201
4202 static void
4203 bind_atomic_buffers(struct gl_context *ctx,
4204 GLuint first,
4205 GLsizei count,
4206 const GLuint *buffers,
4207 bool range,
4208 const GLintptr *offsets,
4209 const GLsizeiptr *sizes,
4210 const char *caller)
4211 {
4212 if (!error_check_bind_atomic_buffers(ctx, first, count, caller))
4213 return;
4214
4215 /* Assume that at least one binding will be changed */
4216 FLUSH_VERTICES(ctx, 0);
4217 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
4218
4219 if (!buffers) {
4220 /* The ARB_multi_bind spec says:
4221 *
4222 * "If <buffers> is NULL, all bindings from <first> through
4223 * <first>+<count>-1 are reset to their unbound (zero) state.
4224 * In this case, the offsets and sizes associated with the
4225 * binding points are set to default values, ignoring
4226 * <offsets> and <sizes>."
4227 */
4228 unbind_atomic_buffers(ctx, first, count);
4229 return;
4230 }
4231
4232 /* Note that the error semantics for multi-bind commands differ from
4233 * those of other GL commands.
4234 *
4235 * The Issues section in the ARB_multi_bind spec says:
4236 *
4237 * "(11) Typically, OpenGL specifies that if an error is generated by a
4238 * command, that command has no effect. This is somewhat
4239 * unfortunate for multi-bind commands, because it would require a
4240 * first pass to scan the entire list of bound objects for errors
4241 * and then a second pass to actually perform the bindings.
4242 * Should we have different error semantics?
4243 *
4244 * RESOLVED: Yes. In this specification, when the parameters for
4245 * one of the <count> binding points are invalid, that binding point
4246 * is not updated and an error will be generated. However, other
4247 * binding points in the same command will be updated if their
4248 * parameters are valid and no other error occurs."
4249 */
4250
4251 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
4252
4253 for (int i = 0; i < count; i++) {
4254 struct gl_atomic_buffer_binding *binding =
4255 &ctx->AtomicBufferBindings[first + i];
4256 struct gl_buffer_object *bufObj;
4257 GLintptr offset = 0;
4258 GLsizeiptr size = 0;
4259
4260 if (range) {
4261 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
4262 continue;
4263
4264 /* The ARB_multi_bind spec says:
4265 *
4266 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4267 * pair of values in <offsets> and <sizes> does not respectively
4268 * satisfy the constraints described for those parameters for the
4269 * specified target, as described in section 6.7.1 (per binding)."
4270 *
4271 * Section 6.7.1 refers to table 6.5, which says:
4272 *
4273 * "┌───────────────────────────────────────────────────────────────┐
4274 * │ Atomic counter array bindings (see sec. 7.7.2) │
4275 * ├───────────────────────┬───────────────────────────────────────┤
4276 * │ ... │ ... │
4277 * │ offset restriction │ multiple of 4 │
4278 * │ ... │ ... │
4279 * │ size restriction │ none │
4280 * └───────────────────────┴───────────────────────────────────────┘"
4281 */
4282 if (offsets[i] & (ATOMIC_COUNTER_SIZE - 1)) {
4283 _mesa_error(ctx, GL_INVALID_VALUE,
4284 "glBindBuffersRange(offsets[%u]=%" PRId64
4285 " is misaligned; it must be a multiple of %d when "
4286 "target=GL_ATOMIC_COUNTER_BUFFER)",
4287 i, (int64_t) offsets[i], ATOMIC_COUNTER_SIZE);
4288 continue;
4289 }
4290
4291 offset = offsets[i];
4292 size = sizes[i];
4293 }
4294
4295 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
4296 bufObj = binding->BufferObject;
4297 else
4298 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller);
4299
4300 if (bufObj)
4301 if (bufObj == ctx->Shared->NullBufferObj)
4302 set_atomic_buffer_binding(ctx, binding, bufObj, -1, -1, !range);
4303 else
4304 set_atomic_buffer_binding(ctx, binding, bufObj, offset, size, !range);
4305 }
4306
4307 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
4308 }
4309
4310 static ALWAYS_INLINE void
4311 bind_buffer_range(GLenum target, GLuint index, GLuint buffer, GLintptr offset,
4312 GLsizeiptr size, bool no_error)
4313 {
4314 GET_CURRENT_CONTEXT(ctx);
4315 struct gl_buffer_object *bufObj;
4316
4317 if (MESA_VERBOSE & VERBOSE_API) {
4318 _mesa_debug(ctx, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4319 _mesa_enum_to_string(target), index, buffer,
4320 (unsigned long) offset, (unsigned long) size);
4321 }
4322
4323 if (buffer == 0) {
4324 bufObj = ctx->Shared->NullBufferObj;
4325 } else {
4326 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4327 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
4328 &bufObj, "glBindBufferRange"))
4329 return;
4330
4331 if (!no_error && !bufObj) {
4332 _mesa_error(ctx, GL_INVALID_OPERATION,
4333 "glBindBufferRange(invalid buffer=%u)", buffer);
4334 return;
4335 }
4336 }
4337
4338 if (no_error) {
4339 switch (target) {
4340 case GL_TRANSFORM_FEEDBACK_BUFFER:
4341 _mesa_bind_buffer_range_xfb(ctx, ctx->TransformFeedback.CurrentObject,
4342 index, bufObj, offset, size);
4343 return;
4344 case GL_UNIFORM_BUFFER:
4345 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
4346 return;
4347 case GL_SHADER_STORAGE_BUFFER:
4348 bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset,
4349 size);
4350 return;
4351 case GL_ATOMIC_COUNTER_BUFFER:
4352 bind_buffer_range_atomic_buffer(ctx, index, bufObj, offset, size);
4353 return;
4354 default:
4355 unreachable("invalid BindBufferRange target with KHR_no_error");
4356 }
4357 } else {
4358 if (buffer != 0) {
4359 if (size <= 0) {
4360 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
4361 (int) size);
4362 return;
4363 }
4364 }
4365
4366 switch (target) {
4367 case GL_TRANSFORM_FEEDBACK_BUFFER:
4368 if (!_mesa_validate_buffer_range_xfb(ctx,
4369 ctx->TransformFeedback.CurrentObject,
4370 index, bufObj, offset, size,
4371 false))
4372 return;
4373
4374 _mesa_bind_buffer_range_xfb(ctx, ctx->TransformFeedback.CurrentObject,
4375 index, bufObj, offset, size);
4376 return;
4377 case GL_UNIFORM_BUFFER:
4378 bind_buffer_range_uniform_buffer_err(ctx, index, bufObj, offset,
4379 size);
4380 return;
4381 case GL_SHADER_STORAGE_BUFFER:
4382 bind_buffer_range_shader_storage_buffer_err(ctx, index, bufObj,
4383 offset, size);
4384 return;
4385 case GL_ATOMIC_COUNTER_BUFFER:
4386 bind_buffer_range_atomic_buffer_err(ctx, index, bufObj,
4387 offset, size);
4388 return;
4389 default:
4390 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
4391 return;
4392 }
4393 }
4394 }
4395
4396 void GLAPIENTRY
4397 _mesa_BindBufferRange_no_error(GLenum target, GLuint index, GLuint buffer,
4398 GLintptr offset, GLsizeiptr size)
4399 {
4400 bind_buffer_range(target, index, buffer, offset, size, true);
4401 }
4402
4403 void GLAPIENTRY
4404 _mesa_BindBufferRange(GLenum target, GLuint index,
4405 GLuint buffer, GLintptr offset, GLsizeiptr size)
4406 {
4407 bind_buffer_range(target, index, buffer, offset, size, false);
4408 }
4409
4410 void GLAPIENTRY
4411 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
4412 {
4413 GET_CURRENT_CONTEXT(ctx);
4414 struct gl_buffer_object *bufObj;
4415
4416 if (MESA_VERBOSE & VERBOSE_API) {
4417 _mesa_debug(ctx, "glBindBufferBase(%s, %u, %u)\n",
4418 _mesa_enum_to_string(target), index, buffer);
4419 }
4420
4421 if (buffer == 0) {
4422 bufObj = ctx->Shared->NullBufferObj;
4423 } else {
4424 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4425 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
4426 &bufObj, "glBindBufferBase"))
4427 return;
4428
4429 if (!bufObj) {
4430 _mesa_error(ctx, GL_INVALID_OPERATION,
4431 "glBindBufferBase(invalid buffer=%u)", buffer);
4432 return;
4433 }
4434 }
4435
4436 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4437 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4438 *
4439 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4440 * zero and size equal to the size of buffer."
4441 *
4442 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4443 *
4444 * "If the parameter (starting offset or size) was not specified when the
4445 * buffer object was bound, zero is returned."
4446 *
4447 * What happens if the size of the buffer changes? Does the size of the
4448 * buffer at the moment glBindBufferBase was called still play a role, like
4449 * the first quote would imply, or is the size meaningless in the
4450 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4451 * core spec page 45 says:
4452 *
4453 * "It is equivalent to calling BindBufferRange with offset zero, while
4454 * size is determined by the size of the bound buffer at the time the
4455 * binding is used."
4456 *
4457 * My interpretation is that the GL 4.1 spec was a clarification of the
4458 * behavior, not a change. In particular, this choice will only make
4459 * rendering work in cases where it would have had undefined results.
4460 */
4461
4462 switch (target) {
4463 case GL_TRANSFORM_FEEDBACK_BUFFER:
4464 _mesa_bind_buffer_base_transform_feedback(ctx,
4465 ctx->TransformFeedback.CurrentObject,
4466 index, bufObj, false);
4467 return;
4468 case GL_UNIFORM_BUFFER:
4469 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
4470 return;
4471 case GL_SHADER_STORAGE_BUFFER:
4472 bind_buffer_base_shader_storage_buffer(ctx, index, bufObj);
4473 return;
4474 case GL_ATOMIC_COUNTER_BUFFER:
4475 bind_buffer_base_atomic_buffer(ctx, index, bufObj);
4476 return;
4477 default:
4478 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
4479 return;
4480 }
4481 }
4482
4483 void GLAPIENTRY
4484 _mesa_BindBuffersRange(GLenum target, GLuint first, GLsizei count,
4485 const GLuint *buffers,
4486 const GLintptr *offsets, const GLsizeiptr *sizes)
4487 {
4488 GET_CURRENT_CONTEXT(ctx);
4489
4490 if (MESA_VERBOSE & VERBOSE_API) {
4491 _mesa_debug(ctx, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4492 _mesa_enum_to_string(target), first, count,
4493 buffers, offsets, sizes);
4494 }
4495
4496 switch (target) {
4497 case GL_TRANSFORM_FEEDBACK_BUFFER:
4498 bind_xfb_buffers(ctx, first, count, buffers, true, offsets, sizes,
4499 "glBindBuffersRange");
4500 return;
4501 case GL_UNIFORM_BUFFER:
4502 bind_uniform_buffers(ctx, first, count, buffers, true, offsets, sizes,
4503 "glBindBuffersRange");
4504 return;
4505 case GL_SHADER_STORAGE_BUFFER:
4506 bind_shader_storage_buffers(ctx, first, count, buffers, true, offsets, sizes,
4507 "glBindBuffersRange");
4508 return;
4509 case GL_ATOMIC_COUNTER_BUFFER:
4510 bind_atomic_buffers(ctx, first, count, buffers, true, offsets, sizes,
4511 "glBindBuffersRange");
4512 return;
4513 default:
4514 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersRange(target=%s)",
4515 _mesa_enum_to_string(target));
4516 break;
4517 }
4518 }
4519
4520 void GLAPIENTRY
4521 _mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count,
4522 const GLuint *buffers)
4523 {
4524 GET_CURRENT_CONTEXT(ctx);
4525
4526 if (MESA_VERBOSE & VERBOSE_API) {
4527 _mesa_debug(ctx, "glBindBuffersBase(%s, %u, %d, %p)\n",
4528 _mesa_enum_to_string(target), first, count, buffers);
4529 }
4530
4531 switch (target) {
4532 case GL_TRANSFORM_FEEDBACK_BUFFER:
4533 bind_xfb_buffers(ctx, first, count, buffers, false, NULL, NULL,
4534 "glBindBuffersBase");
4535 return;
4536 case GL_UNIFORM_BUFFER:
4537 bind_uniform_buffers(ctx, first, count, buffers, false, NULL, NULL,
4538 "glBindBuffersBase");
4539 return;
4540 case GL_SHADER_STORAGE_BUFFER:
4541 bind_shader_storage_buffers(ctx, first, count, buffers, false, NULL, NULL,
4542 "glBindBuffersBase");
4543 return;
4544 case GL_ATOMIC_COUNTER_BUFFER:
4545 bind_atomic_buffers(ctx, first, count, buffers, false, NULL, NULL,
4546 "glBindBuffersBase");
4547 return;
4548 default:
4549 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersBase(target=%s)",
4550 _mesa_enum_to_string(target));
4551 break;
4552 }
4553 }
4554
4555 static ALWAYS_INLINE void
4556 invalidate_buffer_subdata(struct gl_context *ctx,
4557 struct gl_buffer_object *bufObj, GLintptr offset,
4558 GLsizeiptr length)
4559 {
4560 if (ctx->Driver.InvalidateBufferSubData)
4561 ctx->Driver.InvalidateBufferSubData(ctx, bufObj, offset, length);
4562 }
4563
4564 void GLAPIENTRY
4565 _mesa_InvalidateBufferSubData_no_error(GLuint buffer, GLintptr offset,
4566 GLsizeiptr length)
4567 {
4568 GET_CURRENT_CONTEXT(ctx);
4569
4570 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4571 invalidate_buffer_subdata(ctx, bufObj, offset, length);
4572 }
4573
4574 void GLAPIENTRY
4575 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
4576 GLsizeiptr length)
4577 {
4578 GET_CURRENT_CONTEXT(ctx);
4579 struct gl_buffer_object *bufObj;
4580 const GLintptr end = offset + length;
4581
4582 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4583 * Profile) spec says:
4584 *
4585 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4586 * name of an existing buffer object."
4587 */
4588 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4589 if (!bufObj || bufObj == &DummyBufferObject) {
4590 _mesa_error(ctx, GL_INVALID_VALUE,
4591 "glInvalidateBufferSubData(name = %u) invalid object",
4592 buffer);
4593 return;
4594 }
4595
4596 /* The GL_ARB_invalidate_subdata spec says:
4597 *
4598 * "An INVALID_VALUE error is generated if <offset> or <length> is
4599 * negative, or if <offset> + <length> is greater than the value of
4600 * BUFFER_SIZE."
4601 */
4602 if (offset < 0 || length < 0 || end > bufObj->Size) {
4603 _mesa_error(ctx, GL_INVALID_VALUE,
4604 "glInvalidateBufferSubData(invalid offset or length)");
4605 return;
4606 }
4607
4608 /* The OpenGL 4.4 (Core Profile) spec says:
4609 *
4610 * "An INVALID_OPERATION error is generated if buffer is currently
4611 * mapped by MapBuffer or if the invalidate range intersects the range
4612 * currently mapped by MapBufferRange, unless it was mapped
4613 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4614 */
4615 if (!(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) &&
4616 bufferobj_range_mapped(bufObj, offset, length)) {
4617 _mesa_error(ctx, GL_INVALID_OPERATION,
4618 "glInvalidateBufferSubData(intersection with mapped "
4619 "range)");
4620 return;
4621 }
4622
4623 invalidate_buffer_subdata(ctx, bufObj, offset, length);
4624 }
4625
4626 void GLAPIENTRY
4627 _mesa_InvalidateBufferData_no_error(GLuint buffer)
4628 {
4629 GET_CURRENT_CONTEXT(ctx);
4630
4631 struct gl_buffer_object *bufObj =_mesa_lookup_bufferobj(ctx, buffer);
4632 invalidate_buffer_subdata(ctx, bufObj, 0, bufObj->Size);
4633 }
4634
4635 void GLAPIENTRY
4636 _mesa_InvalidateBufferData(GLuint buffer)
4637 {
4638 GET_CURRENT_CONTEXT(ctx);
4639 struct gl_buffer_object *bufObj;
4640
4641 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4642 * Profile) spec says:
4643 *
4644 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4645 * name of an existing buffer object."
4646 */
4647 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4648 if (!bufObj || bufObj == &DummyBufferObject) {
4649 _mesa_error(ctx, GL_INVALID_VALUE,
4650 "glInvalidateBufferData(name = %u) invalid object",
4651 buffer);
4652 return;
4653 }
4654
4655 /* The OpenGL 4.4 (Core Profile) spec says:
4656 *
4657 * "An INVALID_OPERATION error is generated if buffer is currently
4658 * mapped by MapBuffer or if the invalidate range intersects the range
4659 * currently mapped by MapBufferRange, unless it was mapped
4660 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4661 */
4662 if (_mesa_check_disallowed_mapping(bufObj)) {
4663 _mesa_error(ctx, GL_INVALID_OPERATION,
4664 "glInvalidateBufferData(intersection with mapped "
4665 "range)");
4666 return;
4667 }
4668
4669 invalidate_buffer_subdata(ctx, bufObj, 0, bufObj->Size);
4670 }
4671
4672 static void
4673 buffer_page_commitment(struct gl_context *ctx,
4674 struct gl_buffer_object *bufferObj,
4675 GLintptr offset, GLsizeiptr size,
4676 GLboolean commit, const char *func)
4677 {
4678 if (!(bufferObj->StorageFlags & GL_SPARSE_STORAGE_BIT_ARB)) {
4679 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(not a sparse buffer object)",
4680 func);
4681 return;
4682 }
4683
4684 if (size < 0 || size > bufferObj->Size ||
4685 offset < 0 || offset > bufferObj->Size - size) {
4686 _mesa_error(ctx, GL_INVALID_VALUE, "%s(out of bounds)",
4687 func);
4688 return;
4689 }
4690
4691 /* The GL_ARB_sparse_buffer extension specification says:
4692 *
4693 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4694 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4695 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4696 * not extend to the end of the buffer's data store."
4697 */
4698 if (offset % ctx->Const.SparseBufferPageSize != 0) {
4699 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset not aligned to page size)",
4700 func);
4701 return;
4702 }
4703
4704 if (size % ctx->Const.SparseBufferPageSize != 0 &&
4705 offset + size != bufferObj->Size) {
4706 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size not aligned to page size)",
4707 func);
4708 return;
4709 }
4710
4711 ctx->Driver.BufferPageCommitment(ctx, bufferObj, offset, size, commit);
4712 }
4713
4714 void GLAPIENTRY
4715 _mesa_BufferPageCommitmentARB(GLenum target, GLintptr offset, GLsizeiptr size,
4716 GLboolean commit)
4717 {
4718 GET_CURRENT_CONTEXT(ctx);
4719 struct gl_buffer_object *bufferObj;
4720
4721 bufferObj = get_buffer(ctx, "glBufferPageCommitmentARB", target,
4722 GL_INVALID_ENUM);
4723 if (!bufferObj)
4724 return;
4725
4726 buffer_page_commitment(ctx, bufferObj, offset, size, commit,
4727 "glBufferPageCommitmentARB");
4728 }
4729
4730 void GLAPIENTRY
4731 _mesa_NamedBufferPageCommitmentARB(GLuint buffer, GLintptr offset,
4732 GLsizeiptr size, GLboolean commit)
4733 {
4734 GET_CURRENT_CONTEXT(ctx);
4735 struct gl_buffer_object *bufferObj;
4736
4737 bufferObj = _mesa_lookup_bufferobj(ctx, buffer);
4738 if (!bufferObj || bufferObj == &DummyBufferObject) {
4739 /* Note: the extension spec is not clear about the excpected error value. */
4740 _mesa_error(ctx, GL_INVALID_VALUE,
4741 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4742 buffer);
4743 return;
4744 }
4745
4746 buffer_page_commitment(ctx, bufferObj, offset, size, commit,
4747 "glNamedBufferPageCommitmentARB");
4748 }