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