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