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