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