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