mesa: add copy_texture_sub_image_no_error() helper
[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(GLsizei n, GLuint *buffers, bool dsa)
1420 {
1421 GET_CURRENT_CONTEXT(ctx);
1422 GLuint first;
1423 struct gl_buffer_object *buf;
1424
1425 const char *func = dsa ? "glCreateBuffers" : "glGenBuffers";
1426
1427 if (MESA_VERBOSE & VERBOSE_API)
1428 _mesa_debug(ctx, "%s(%d)\n", func, n);
1429
1430 if (n < 0) {
1431 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n %d < 0)", func, n);
1432 return;
1433 }
1434
1435 if (!buffers) {
1436 return;
1437 }
1438
1439 /*
1440 * This must be atomic (generation and allocation of buffer object IDs)
1441 */
1442 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
1443
1444 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
1445
1446 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1447 * DummyBufferObject. Otherwise, create a new buffer object and insert
1448 * it.
1449 */
1450 for (int i = 0; i < n; i++) {
1451 buffers[i] = first + i;
1452 if (dsa) {
1453 assert(ctx->Driver.NewBufferObject);
1454 buf = ctx->Driver.NewBufferObject(ctx, buffers[i]);
1455 if (!buf) {
1456 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1457 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
1458 return;
1459 }
1460 }
1461 else
1462 buf = &DummyBufferObject;
1463
1464 _mesa_HashInsertLocked(ctx->Shared->BufferObjects, buffers[i], buf);
1465 }
1466
1467 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
1468 }
1469
1470 /**
1471 * Generate a set of unique buffer object IDs and store them in \c buffers.
1472 *
1473 * \param n Number of IDs to generate.
1474 * \param buffers Array of \c n locations to store the IDs.
1475 */
1476 void GLAPIENTRY
1477 _mesa_GenBuffers(GLsizei n, GLuint *buffers)
1478 {
1479 create_buffers(n, buffers, false);
1480 }
1481
1482 /**
1483 * Create a set of buffer objects and store their unique IDs in \c buffers.
1484 *
1485 * \param n Number of IDs to generate.
1486 * \param buffers Array of \c n locations to store the IDs.
1487 */
1488 void GLAPIENTRY
1489 _mesa_CreateBuffers(GLsizei n, GLuint *buffers)
1490 {
1491 create_buffers(n, buffers, true);
1492 }
1493
1494
1495 /**
1496 * Determine if ID is the name of a buffer object.
1497 *
1498 * \param id ID of the potential buffer object.
1499 * \return \c GL_TRUE if \c id is the name of a buffer object,
1500 * \c GL_FALSE otherwise.
1501 */
1502 GLboolean GLAPIENTRY
1503 _mesa_IsBuffer(GLuint id)
1504 {
1505 struct gl_buffer_object *bufObj;
1506 GET_CURRENT_CONTEXT(ctx);
1507 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1508
1509 bufObj = _mesa_lookup_bufferobj(ctx, id);
1510
1511 return bufObj && bufObj != &DummyBufferObject;
1512 }
1513
1514
1515 static bool
1516 validate_buffer_storage(struct gl_context *ctx,
1517 struct gl_buffer_object *bufObj, GLsizeiptr size,
1518 GLbitfield flags, const char *func)
1519 {
1520 if (size <= 0) {
1521 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size <= 0)", func);
1522 return false;
1523 }
1524
1525 GLbitfield valid_flags = GL_MAP_READ_BIT |
1526 GL_MAP_WRITE_BIT |
1527 GL_MAP_PERSISTENT_BIT |
1528 GL_MAP_COHERENT_BIT |
1529 GL_DYNAMIC_STORAGE_BIT |
1530 GL_CLIENT_STORAGE_BIT;
1531
1532 if (ctx->Extensions.ARB_sparse_buffer)
1533 valid_flags |= GL_SPARSE_STORAGE_BIT_ARB;
1534
1535 if (flags & ~valid_flags) {
1536 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid flag bits set)", func);
1537 return false;
1538 }
1539
1540 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1541 *
1542 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1543 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1544 * MAP_READ_BIT or MAP_WRITE_BIT."
1545 */
1546 if (flags & GL_SPARSE_STORAGE_BIT_ARB &&
1547 flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) {
1548 _mesa_error(ctx, GL_INVALID_VALUE, "%s(SPARSE_STORAGE and READ/WRITE)", func);
1549 return false;
1550 }
1551
1552 if (flags & GL_MAP_PERSISTENT_BIT &&
1553 !(flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))) {
1554 _mesa_error(ctx, GL_INVALID_VALUE,
1555 "%s(PERSISTENT and flags!=READ/WRITE)", func);
1556 return false;
1557 }
1558
1559 if (flags & GL_MAP_COHERENT_BIT && !(flags & GL_MAP_PERSISTENT_BIT)) {
1560 _mesa_error(ctx, GL_INVALID_VALUE,
1561 "%s(COHERENT and flags!=PERSISTENT)", func);
1562 return false;
1563 }
1564
1565 if (bufObj->Immutable || bufObj->HandleAllocated) {
1566 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
1567 return false;
1568 }
1569
1570 return true;
1571 }
1572
1573
1574 static void
1575 buffer_storage(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1576 GLenum target, GLsizeiptr size, const GLvoid *data,
1577 GLbitfield flags, const char *func)
1578 {
1579 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1580 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1581
1582 FLUSH_VERTICES(ctx, 0);
1583
1584 bufObj->Written = GL_TRUE;
1585 bufObj->Immutable = GL_TRUE;
1586 bufObj->MinMaxCacheDirty = true;
1587
1588 assert(ctx->Driver.BufferData);
1589 if (!ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
1590 flags, bufObj)) {
1591 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
1592 /* Even though the interaction between AMD_pinned_memory and
1593 * glBufferStorage is not described in the spec, Graham Sellers
1594 * said that it should behave the same as glBufferData.
1595 */
1596 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1597 }
1598 else {
1599 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1600 }
1601 }
1602 }
1603
1604
1605 static ALWAYS_INLINE void
1606 inlined_buffer_storage(GLenum target, GLuint buffer, GLsizeiptr size,
1607 const GLvoid *data, GLbitfield flags, bool dsa,
1608 bool no_error, const char *func)
1609 {
1610 GET_CURRENT_CONTEXT(ctx);
1611 struct gl_buffer_object *bufObj;
1612
1613 if (dsa) {
1614 if (no_error) {
1615 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
1616 } else {
1617 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, func);
1618 if (!bufObj)
1619 return;
1620 }
1621 } else {
1622 if (no_error) {
1623 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
1624 bufObj = *bufObjPtr;
1625 } else {
1626 bufObj = get_buffer(ctx, func, target, GL_INVALID_OPERATION);
1627 if (!bufObj)
1628 return;
1629 }
1630 }
1631
1632 if (no_error || validate_buffer_storage(ctx, bufObj, size, flags, func))
1633 buffer_storage(ctx, bufObj, target, size, data, flags, func);
1634 }
1635
1636
1637 void GLAPIENTRY
1638 _mesa_BufferStorage_no_error(GLenum target, GLsizeiptr size,
1639 const GLvoid *data, GLbitfield flags)
1640 {
1641 inlined_buffer_storage(target, 0, size, data, flags, false, true,
1642 "glBufferStorage");
1643 }
1644
1645
1646 void GLAPIENTRY
1647 _mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data,
1648 GLbitfield flags)
1649 {
1650 inlined_buffer_storage(target, 0, size, data, flags, false, false,
1651 "glBufferStorage");
1652 }
1653
1654
1655 void GLAPIENTRY
1656 _mesa_NamedBufferStorage_no_error(GLuint buffer, GLsizeiptr size,
1657 const GLvoid *data, GLbitfield flags)
1658 {
1659 /* In direct state access, buffer objects have an unspecified target
1660 * since they are not required to be bound.
1661 */
1662 inlined_buffer_storage(GL_NONE, buffer, size, data, flags, true, true,
1663 "glNamedBufferStorage");
1664 }
1665
1666
1667 void GLAPIENTRY
1668 _mesa_NamedBufferStorage(GLuint buffer, GLsizeiptr size, const GLvoid *data,
1669 GLbitfield flags)
1670 {
1671 /* In direct state access, buffer objects have an unspecified target
1672 * since they are not required to be bound.
1673 */
1674 inlined_buffer_storage(GL_NONE, buffer, size, data, flags, true, false,
1675 "glNamedBufferStorage");
1676 }
1677
1678
1679 void
1680 _mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1681 GLenum target, GLsizeiptr size, const GLvoid *data,
1682 GLenum usage, const char *func)
1683 {
1684 bool valid_usage;
1685
1686 if (MESA_VERBOSE & VERBOSE_API) {
1687 _mesa_debug(ctx, "%s(%s, %ld, %p, %s)\n",
1688 func,
1689 _mesa_enum_to_string(target),
1690 (long int) size, data,
1691 _mesa_enum_to_string(usage));
1692 }
1693
1694 if (size < 0) {
1695 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", func);
1696 return;
1697 }
1698
1699 switch (usage) {
1700 case GL_STREAM_DRAW_ARB:
1701 valid_usage = (ctx->API != API_OPENGLES);
1702 break;
1703
1704 case GL_STATIC_DRAW_ARB:
1705 case GL_DYNAMIC_DRAW_ARB:
1706 valid_usage = true;
1707 break;
1708
1709 case GL_STREAM_READ_ARB:
1710 case GL_STREAM_COPY_ARB:
1711 case GL_STATIC_READ_ARB:
1712 case GL_STATIC_COPY_ARB:
1713 case GL_DYNAMIC_READ_ARB:
1714 case GL_DYNAMIC_COPY_ARB:
1715 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1716 break;
1717
1718 default:
1719 valid_usage = false;
1720 break;
1721 }
1722
1723 if (!valid_usage) {
1724 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid usage: %s)", func,
1725 _mesa_enum_to_string(usage));
1726 return;
1727 }
1728
1729 if (bufObj->Immutable || bufObj->HandleAllocated) {
1730 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
1731 return;
1732 }
1733
1734 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1735 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1736
1737 FLUSH_VERTICES(ctx, 0);
1738
1739 bufObj->Written = GL_TRUE;
1740 bufObj->MinMaxCacheDirty = true;
1741
1742 #ifdef VBO_DEBUG
1743 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1744 bufObj->Name, size, data, usage);
1745 #endif
1746
1747 #ifdef BOUNDS_CHECK
1748 size += 100;
1749 #endif
1750
1751 assert(ctx->Driver.BufferData);
1752 if (!ctx->Driver.BufferData(ctx, target, size, data, usage,
1753 GL_MAP_READ_BIT |
1754 GL_MAP_WRITE_BIT |
1755 GL_DYNAMIC_STORAGE_BIT,
1756 bufObj)) {
1757 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
1758 /* From GL_AMD_pinned_memory:
1759 *
1760 * INVALID_OPERATION is generated by BufferData if <target> is
1761 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1762 * mapped to the GPU address space.
1763 */
1764 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1765 }
1766 else {
1767 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1768 }
1769 }
1770 }
1771
1772 void GLAPIENTRY
1773 _mesa_BufferData(GLenum target, GLsizeiptr size,
1774 const GLvoid *data, GLenum usage)
1775 {
1776 GET_CURRENT_CONTEXT(ctx);
1777 struct gl_buffer_object *bufObj;
1778
1779 bufObj = get_buffer(ctx, "glBufferData", target, GL_INVALID_OPERATION);
1780 if (!bufObj)
1781 return;
1782
1783 _mesa_buffer_data(ctx, bufObj, target, size, data, usage,
1784 "glBufferData");
1785 }
1786
1787 void GLAPIENTRY
1788 _mesa_NamedBufferData(GLuint buffer, GLsizeiptr size, const GLvoid *data,
1789 GLenum usage)
1790 {
1791 GET_CURRENT_CONTEXT(ctx);
1792 struct gl_buffer_object *bufObj;
1793
1794 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferData");
1795 if (!bufObj)
1796 return;
1797
1798 /* In direct state access, buffer objects have an unspecified target since
1799 * they are not required to be bound.
1800 */
1801 _mesa_buffer_data(ctx, bufObj, GL_NONE, size, data, usage,
1802 "glNamedBufferData");
1803 }
1804
1805
1806 static bool
1807 validate_buffer_sub_data(struct gl_context *ctx,
1808 struct gl_buffer_object *bufObj,
1809 GLintptr offset, GLsizeiptr size,
1810 const char *func)
1811 {
1812 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
1813 true, func)) {
1814 /* error already recorded */
1815 return false;
1816 }
1817
1818 if (bufObj->Immutable &&
1819 !(bufObj->StorageFlags & GL_DYNAMIC_STORAGE_BIT)) {
1820 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1821 return false;
1822 }
1823
1824 if ((bufObj->Usage == GL_STATIC_DRAW ||
1825 bufObj->Usage == GL_STATIC_COPY) &&
1826 bufObj->NumSubDataCalls >= BUFFER_WARNING_CALL_COUNT - 1) {
1827 /* If the application declared the buffer as static draw/copy or stream
1828 * draw, it should not be frequently modified with glBufferSubData.
1829 */
1830 BUFFER_USAGE_WARNING(ctx,
1831 "using %s(buffer %u, offset %u, size %u) to "
1832 "update a %s buffer",
1833 func, bufObj->Name, offset, size,
1834 _mesa_enum_to_string(bufObj->Usage));
1835 }
1836
1837 return true;
1838 }
1839
1840
1841 /**
1842 * Implementation for glBufferSubData and glNamedBufferSubData.
1843 *
1844 * \param ctx GL context.
1845 * \param bufObj The buffer object.
1846 * \param offset Offset of the first byte of the subdata range.
1847 * \param size Size, in bytes, of the subdata range.
1848 * \param data The data store.
1849 * \param func Name of calling function for recording errors.
1850 *
1851 */
1852 void
1853 _mesa_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1854 GLintptr offset, GLsizeiptr size, const GLvoid *data)
1855 {
1856 if (size == 0)
1857 return;
1858
1859 bufObj->NumSubDataCalls++;
1860 bufObj->Written = GL_TRUE;
1861 bufObj->MinMaxCacheDirty = true;
1862
1863 assert(ctx->Driver.BufferSubData);
1864 ctx->Driver.BufferSubData(ctx, offset, size, data, bufObj);
1865 }
1866
1867
1868 static ALWAYS_INLINE void
1869 buffer_sub_data(GLenum target, GLuint buffer, GLintptr offset,
1870 GLsizeiptr size, const GLvoid *data,
1871 bool dsa, bool no_error, const char *func)
1872 {
1873 GET_CURRENT_CONTEXT(ctx);
1874 struct gl_buffer_object *bufObj;
1875
1876 if (dsa) {
1877 if (no_error) {
1878 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
1879 } else {
1880 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, func);
1881 if (!bufObj)
1882 return;
1883 }
1884 } else {
1885 if (no_error) {
1886 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
1887 bufObj = *bufObjPtr;
1888 } else {
1889 bufObj = get_buffer(ctx, func, target, GL_INVALID_OPERATION);
1890 if (!bufObj)
1891 return;
1892 }
1893 }
1894
1895 if (no_error || validate_buffer_sub_data(ctx, bufObj, offset, size, func))
1896 _mesa_buffer_sub_data(ctx, bufObj, offset, size, data);
1897 }
1898
1899
1900 void GLAPIENTRY
1901 _mesa_BufferSubData_no_error(GLenum target, GLintptr offset,
1902 GLsizeiptr size, const GLvoid *data)
1903 {
1904 buffer_sub_data(target, 0, offset, size, data, false, true,
1905 "glBufferSubData");
1906 }
1907
1908
1909 void GLAPIENTRY
1910 _mesa_BufferSubData(GLenum target, GLintptr offset,
1911 GLsizeiptr size, const GLvoid *data)
1912 {
1913 buffer_sub_data(target, 0, offset, size, data, false, false,
1914 "glBufferSubData");
1915 }
1916
1917 void GLAPIENTRY
1918 _mesa_NamedBufferSubData_no_error(GLuint buffer, GLintptr offset,
1919 GLsizeiptr size, const GLvoid *data)
1920 {
1921 buffer_sub_data(0, buffer, offset, size, data, true, true,
1922 "glNamedBufferSubData");
1923 }
1924
1925 void GLAPIENTRY
1926 _mesa_NamedBufferSubData(GLuint buffer, GLintptr offset,
1927 GLsizeiptr size, const GLvoid *data)
1928 {
1929 buffer_sub_data(0, buffer, offset, size, data, true, false,
1930 "glNamedBufferSubData");
1931 }
1932
1933
1934 void GLAPIENTRY
1935 _mesa_GetBufferSubData(GLenum target, GLintptr offset,
1936 GLsizeiptr size, GLvoid *data)
1937 {
1938 GET_CURRENT_CONTEXT(ctx);
1939 struct gl_buffer_object *bufObj;
1940
1941 bufObj = get_buffer(ctx, "glGetBufferSubData", target,
1942 GL_INVALID_OPERATION);
1943 if (!bufObj)
1944 return;
1945
1946 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
1947 "glGetBufferSubData")) {
1948 return;
1949 }
1950
1951 assert(ctx->Driver.GetBufferSubData);
1952 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
1953 }
1954
1955 void GLAPIENTRY
1956 _mesa_GetNamedBufferSubData(GLuint buffer, GLintptr offset,
1957 GLsizeiptr size, GLvoid *data)
1958 {
1959 GET_CURRENT_CONTEXT(ctx);
1960 struct gl_buffer_object *bufObj;
1961
1962 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
1963 "glGetNamedBufferSubData");
1964 if (!bufObj)
1965 return;
1966
1967 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
1968 "glGetNamedBufferSubData")) {
1969 return;
1970 }
1971
1972 assert(ctx->Driver.GetBufferSubData);
1973 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
1974 }
1975
1976
1977 /**
1978 * \param subdata true if caller is *SubData, false if *Data
1979 */
1980 static void
1981 clear_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1982 GLenum internalformat, GLintptr offset, GLsizeiptr size,
1983 GLenum format, GLenum type, const GLvoid *data,
1984 const char *func, bool subdata)
1985 {
1986 mesa_format mesaFormat;
1987 GLubyte clearValue[MAX_PIXEL_BYTES];
1988 GLsizeiptr clearValueSize;
1989
1990 /* This checks for disallowed mappings. */
1991 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
1992 subdata, func)) {
1993 return;
1994 }
1995
1996 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1997 format, type, func);
1998
1999 if (mesaFormat == MESA_FORMAT_NONE) {
2000 return;
2001 }
2002
2003 clearValueSize = _mesa_get_format_bytes(mesaFormat);
2004 if (offset % clearValueSize != 0 || size % clearValueSize != 0) {
2005 _mesa_error(ctx, GL_INVALID_VALUE,
2006 "%s(offset or size is not a multiple of "
2007 "internalformat size)", func);
2008 return;
2009 }
2010
2011 /* Bail early. Negative size has already been checked. */
2012 if (size == 0)
2013 return;
2014
2015 bufObj->MinMaxCacheDirty = true;
2016
2017 if (data == NULL) {
2018 /* clear to zeros, per the spec */
2019 ctx->Driver.ClearBufferSubData(ctx, offset, size,
2020 NULL, clearValueSize, bufObj);
2021 return;
2022 }
2023
2024 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
2025 format, type, data, func)) {
2026 return;
2027 }
2028
2029 ctx->Driver.ClearBufferSubData(ctx, offset, size,
2030 clearValue, clearValueSize, bufObj);
2031 }
2032
2033 void GLAPIENTRY
2034 _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
2035 GLenum type, const GLvoid *data)
2036 {
2037 GET_CURRENT_CONTEXT(ctx);
2038 struct gl_buffer_object *bufObj;
2039
2040 bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
2041 if (!bufObj)
2042 return;
2043
2044 clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size,
2045 format, type, data, "glClearBufferData", false);
2046 }
2047
2048 void GLAPIENTRY
2049 _mesa_ClearNamedBufferData(GLuint buffer, GLenum internalformat,
2050 GLenum format, GLenum type, const GLvoid *data)
2051 {
2052 GET_CURRENT_CONTEXT(ctx);
2053 struct gl_buffer_object *bufObj;
2054
2055 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glClearNamedBufferData");
2056 if (!bufObj)
2057 return;
2058
2059 clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size,
2060 format, type, data, "glClearNamedBufferData", false);
2061 }
2062
2063
2064 void GLAPIENTRY
2065 _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
2066 GLintptr offset, GLsizeiptr size,
2067 GLenum format, GLenum type,
2068 const GLvoid *data)
2069 {
2070 GET_CURRENT_CONTEXT(ctx);
2071 struct gl_buffer_object *bufObj;
2072
2073 bufObj = get_buffer(ctx, "glClearBufferSubData", target, GL_INVALID_VALUE);
2074 if (!bufObj)
2075 return;
2076
2077 clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size,
2078 format, type, data, "glClearBufferSubData", true);
2079 }
2080
2081 void GLAPIENTRY
2082 _mesa_ClearNamedBufferSubData(GLuint buffer, GLenum internalformat,
2083 GLintptr offset, GLsizeiptr size,
2084 GLenum format, GLenum type,
2085 const GLvoid *data)
2086 {
2087 GET_CURRENT_CONTEXT(ctx);
2088 struct gl_buffer_object *bufObj;
2089
2090 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2091 "glClearNamedBufferSubData");
2092 if (!bufObj)
2093 return;
2094
2095 clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size, format,
2096 type, data, "glClearNamedBufferSubData", true);
2097 }
2098
2099 static GLboolean
2100 unmap_buffer(struct gl_context *ctx, struct gl_buffer_object *bufObj)
2101 {
2102 GLboolean status = ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_USER);
2103 bufObj->Mappings[MAP_USER].AccessFlags = 0;
2104 assert(bufObj->Mappings[MAP_USER].Pointer == NULL);
2105 assert(bufObj->Mappings[MAP_USER].Offset == 0);
2106 assert(bufObj->Mappings[MAP_USER].Length == 0);
2107
2108 return status;
2109 }
2110
2111 static GLboolean
2112 validate_and_unmap_buffer(struct gl_context *ctx,
2113 struct gl_buffer_object *bufObj,
2114 const char *func)
2115 {
2116 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
2117
2118 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2119 _mesa_error(ctx, GL_INVALID_OPERATION,
2120 "%s(buffer is not mapped)", func);
2121 return GL_FALSE;
2122 }
2123
2124 #ifdef BOUNDS_CHECK
2125 if (bufObj->Access != GL_READ_ONLY_ARB) {
2126 GLubyte *buf = (GLubyte *) bufObj->Pointer;
2127 GLuint i;
2128 /* check that last 100 bytes are still = magic value */
2129 for (i = 0; i < 100; i++) {
2130 GLuint pos = bufObj->Size - i - 1;
2131 if (buf[pos] != 123) {
2132 _mesa_warning(ctx, "Out of bounds buffer object write detected"
2133 " at position %d (value = %u)\n",
2134 pos, buf[pos]);
2135 }
2136 }
2137 }
2138 #endif
2139
2140 #ifdef VBO_DEBUG
2141 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
2142 GLuint i, unchanged = 0;
2143 GLubyte *b = (GLubyte *) bufObj->Pointer;
2144 GLint pos = -1;
2145 /* check which bytes changed */
2146 for (i = 0; i < bufObj->Size - 1; i++) {
2147 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
2148 unchanged++;
2149 if (pos == -1)
2150 pos = i;
2151 }
2152 }
2153 if (unchanged) {
2154 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2155 bufObj->Name, unchanged, bufObj->Size, pos);
2156 }
2157 }
2158 #endif
2159
2160 return unmap_buffer(ctx, bufObj);
2161 }
2162
2163 GLboolean GLAPIENTRY
2164 _mesa_UnmapBuffer_no_error(GLenum target)
2165 {
2166 GET_CURRENT_CONTEXT(ctx);
2167 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
2168 struct gl_buffer_object *bufObj = *bufObjPtr;
2169
2170 return unmap_buffer(ctx, bufObj);
2171 }
2172
2173 GLboolean GLAPIENTRY
2174 _mesa_UnmapBuffer(GLenum target)
2175 {
2176 GET_CURRENT_CONTEXT(ctx);
2177 struct gl_buffer_object *bufObj;
2178
2179 bufObj = get_buffer(ctx, "glUnmapBuffer", target, GL_INVALID_OPERATION);
2180 if (!bufObj)
2181 return GL_FALSE;
2182
2183 return validate_and_unmap_buffer(ctx, bufObj, "glUnmapBuffer");
2184 }
2185
2186 GLboolean GLAPIENTRY
2187 _mesa_UnmapNamedBuffer_no_error(GLuint buffer)
2188 {
2189 GET_CURRENT_CONTEXT(ctx);
2190 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2191
2192 return unmap_buffer(ctx, bufObj);
2193 }
2194
2195 GLboolean GLAPIENTRY
2196 _mesa_UnmapNamedBuffer(GLuint buffer)
2197 {
2198 GET_CURRENT_CONTEXT(ctx);
2199 struct gl_buffer_object *bufObj;
2200
2201 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glUnmapNamedBuffer");
2202 if (!bufObj)
2203 return GL_FALSE;
2204
2205 return validate_and_unmap_buffer(ctx, bufObj, "glUnmapNamedBuffer");
2206 }
2207
2208
2209 static bool
2210 get_buffer_parameter(struct gl_context *ctx,
2211 struct gl_buffer_object *bufObj, GLenum pname,
2212 GLint64 *params, const char *func)
2213 {
2214 switch (pname) {
2215 case GL_BUFFER_SIZE_ARB:
2216 *params = bufObj->Size;
2217 break;
2218 case GL_BUFFER_USAGE_ARB:
2219 *params = bufObj->Usage;
2220 break;
2221 case GL_BUFFER_ACCESS_ARB:
2222 *params = simplified_access_mode(ctx,
2223 bufObj->Mappings[MAP_USER].AccessFlags);
2224 break;
2225 case GL_BUFFER_MAPPED_ARB:
2226 *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
2227 break;
2228 case GL_BUFFER_ACCESS_FLAGS:
2229 if (!ctx->Extensions.ARB_map_buffer_range)
2230 goto invalid_pname;
2231 *params = bufObj->Mappings[MAP_USER].AccessFlags;
2232 break;
2233 case GL_BUFFER_MAP_OFFSET:
2234 if (!ctx->Extensions.ARB_map_buffer_range)
2235 goto invalid_pname;
2236 *params = bufObj->Mappings[MAP_USER].Offset;
2237 break;
2238 case GL_BUFFER_MAP_LENGTH:
2239 if (!ctx->Extensions.ARB_map_buffer_range)
2240 goto invalid_pname;
2241 *params = bufObj->Mappings[MAP_USER].Length;
2242 break;
2243 case GL_BUFFER_IMMUTABLE_STORAGE:
2244 if (!ctx->Extensions.ARB_buffer_storage)
2245 goto invalid_pname;
2246 *params = bufObj->Immutable;
2247 break;
2248 case GL_BUFFER_STORAGE_FLAGS:
2249 if (!ctx->Extensions.ARB_buffer_storage)
2250 goto invalid_pname;
2251 *params = bufObj->StorageFlags;
2252 break;
2253 default:
2254 goto invalid_pname;
2255 }
2256
2257 return true;
2258
2259 invalid_pname:
2260 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname: %s)", func,
2261 _mesa_enum_to_string(pname));
2262 return false;
2263 }
2264
2265 void GLAPIENTRY
2266 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
2267 {
2268 GET_CURRENT_CONTEXT(ctx);
2269 struct gl_buffer_object *bufObj;
2270 GLint64 parameter;
2271
2272 bufObj = get_buffer(ctx, "glGetBufferParameteriv", target,
2273 GL_INVALID_OPERATION);
2274 if (!bufObj)
2275 return;
2276
2277 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2278 "glGetBufferParameteriv"))
2279 return; /* Error already recorded. */
2280
2281 *params = (GLint) parameter;
2282 }
2283
2284 void GLAPIENTRY
2285 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
2286 {
2287 GET_CURRENT_CONTEXT(ctx);
2288 struct gl_buffer_object *bufObj;
2289 GLint64 parameter;
2290
2291 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
2292 GL_INVALID_OPERATION);
2293 if (!bufObj)
2294 return;
2295
2296 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2297 "glGetBufferParameteri64v"))
2298 return; /* Error already recorded. */
2299
2300 *params = parameter;
2301 }
2302
2303 void GLAPIENTRY
2304 _mesa_GetNamedBufferParameteriv(GLuint buffer, GLenum pname, GLint *params)
2305 {
2306 GET_CURRENT_CONTEXT(ctx);
2307 struct gl_buffer_object *bufObj;
2308 GLint64 parameter;
2309
2310 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2311 "glGetNamedBufferParameteriv");
2312 if (!bufObj)
2313 return;
2314
2315 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2316 "glGetNamedBufferParameteriv"))
2317 return; /* Error already recorded. */
2318
2319 *params = (GLint) parameter;
2320 }
2321
2322 void GLAPIENTRY
2323 _mesa_GetNamedBufferParameteri64v(GLuint buffer, GLenum pname,
2324 GLint64 *params)
2325 {
2326 GET_CURRENT_CONTEXT(ctx);
2327 struct gl_buffer_object *bufObj;
2328 GLint64 parameter;
2329
2330 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2331 "glGetNamedBufferParameteri64v");
2332 if (!bufObj)
2333 return;
2334
2335 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2336 "glGetNamedBufferParameteri64v"))
2337 return; /* Error already recorded. */
2338
2339 *params = parameter;
2340 }
2341
2342
2343 void GLAPIENTRY
2344 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
2345 {
2346 GET_CURRENT_CONTEXT(ctx);
2347 struct gl_buffer_object *bufObj;
2348
2349 if (pname != GL_BUFFER_MAP_POINTER) {
2350 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointerv(pname != "
2351 "GL_BUFFER_MAP_POINTER)");
2352 return;
2353 }
2354
2355 bufObj = get_buffer(ctx, "glGetBufferPointerv", target,
2356 GL_INVALID_OPERATION);
2357 if (!bufObj)
2358 return;
2359
2360 *params = bufObj->Mappings[MAP_USER].Pointer;
2361 }
2362
2363 void GLAPIENTRY
2364 _mesa_GetNamedBufferPointerv(GLuint buffer, GLenum pname, GLvoid **params)
2365 {
2366 GET_CURRENT_CONTEXT(ctx);
2367 struct gl_buffer_object *bufObj;
2368
2369 if (pname != GL_BUFFER_MAP_POINTER) {
2370 _mesa_error(ctx, GL_INVALID_ENUM, "glGetNamedBufferPointerv(pname != "
2371 "GL_BUFFER_MAP_POINTER)");
2372 return;
2373 }
2374
2375 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2376 "glGetNamedBufferPointerv");
2377 if (!bufObj)
2378 return;
2379
2380 *params = bufObj->Mappings[MAP_USER].Pointer;
2381 }
2382
2383
2384 static void
2385 copy_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *src,
2386 struct gl_buffer_object *dst, GLintptr readOffset,
2387 GLintptr writeOffset, GLsizeiptr size, const char *func)
2388 {
2389 if (_mesa_check_disallowed_mapping(src)) {
2390 _mesa_error(ctx, GL_INVALID_OPERATION,
2391 "%s(readBuffer is mapped)", func);
2392 return;
2393 }
2394
2395 if (_mesa_check_disallowed_mapping(dst)) {
2396 _mesa_error(ctx, GL_INVALID_OPERATION,
2397 "%s(writeBuffer is mapped)", func);
2398 return;
2399 }
2400
2401 if (readOffset < 0) {
2402 _mesa_error(ctx, GL_INVALID_VALUE,
2403 "%s(readOffset %d < 0)", func, (int) readOffset);
2404 return;
2405 }
2406
2407 if (writeOffset < 0) {
2408 _mesa_error(ctx, GL_INVALID_VALUE,
2409 "%s(writeOffset %d < 0)", func, (int) writeOffset);
2410 return;
2411 }
2412
2413 if (size < 0) {
2414 _mesa_error(ctx, GL_INVALID_VALUE,
2415 "%s(size %d < 0)", func, (int) size);
2416 return;
2417 }
2418
2419 if (readOffset + size > src->Size) {
2420 _mesa_error(ctx, GL_INVALID_VALUE,
2421 "%s(readOffset %d + size %d > src_buffer_size %d)", func,
2422 (int) readOffset, (int) size, (int) src->Size);
2423 return;
2424 }
2425
2426 if (writeOffset + size > dst->Size) {
2427 _mesa_error(ctx, GL_INVALID_VALUE,
2428 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func,
2429 (int) writeOffset, (int) size, (int) dst->Size);
2430 return;
2431 }
2432
2433 if (src == dst) {
2434 if (readOffset + size <= writeOffset) {
2435 /* OK */
2436 }
2437 else if (writeOffset + size <= readOffset) {
2438 /* OK */
2439 }
2440 else {
2441 /* overlapping src/dst is illegal */
2442 _mesa_error(ctx, GL_INVALID_VALUE,
2443 "%s(overlapping src/dst)", func);
2444 return;
2445 }
2446 }
2447
2448 dst->MinMaxCacheDirty = true;
2449
2450 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
2451 }
2452
2453 void GLAPIENTRY
2454 _mesa_CopyBufferSubData_no_error(GLenum readTarget, GLenum writeTarget,
2455 GLintptr readOffset, GLintptr writeOffset,
2456 GLsizeiptr size)
2457 {
2458 GET_CURRENT_CONTEXT(ctx);
2459
2460 struct gl_buffer_object **src_ptr = get_buffer_target(ctx, readTarget);
2461 struct gl_buffer_object *src = *src_ptr;
2462
2463 struct gl_buffer_object **dst_ptr = get_buffer_target(ctx, writeTarget);
2464 struct gl_buffer_object *dst = *dst_ptr;
2465
2466 dst->MinMaxCacheDirty = true;
2467 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset,
2468 size);
2469 }
2470
2471 void GLAPIENTRY
2472 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
2473 GLintptr readOffset, GLintptr writeOffset,
2474 GLsizeiptr size)
2475 {
2476 GET_CURRENT_CONTEXT(ctx);
2477 struct gl_buffer_object *src, *dst;
2478
2479 src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
2480 GL_INVALID_OPERATION);
2481 if (!src)
2482 return;
2483
2484 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
2485 GL_INVALID_OPERATION);
2486 if (!dst)
2487 return;
2488
2489 copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
2490 "glCopyBufferSubData");
2491 }
2492
2493 void GLAPIENTRY
2494 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer, GLuint writeBuffer,
2495 GLintptr readOffset,
2496 GLintptr writeOffset, GLsizeiptr size)
2497 {
2498 GET_CURRENT_CONTEXT(ctx);
2499
2500 struct gl_buffer_object *src = _mesa_lookup_bufferobj(ctx, readBuffer);
2501 struct gl_buffer_object *dst = _mesa_lookup_bufferobj(ctx, writeBuffer);
2502
2503 dst->MinMaxCacheDirty = true;
2504 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset,
2505 size);
2506 }
2507
2508 void GLAPIENTRY
2509 _mesa_CopyNamedBufferSubData(GLuint readBuffer, GLuint writeBuffer,
2510 GLintptr readOffset, GLintptr writeOffset,
2511 GLsizeiptr size)
2512 {
2513 GET_CURRENT_CONTEXT(ctx);
2514 struct gl_buffer_object *src, *dst;
2515
2516 src = _mesa_lookup_bufferobj_err(ctx, readBuffer,
2517 "glCopyNamedBufferSubData");
2518 if (!src)
2519 return;
2520
2521 dst = _mesa_lookup_bufferobj_err(ctx, writeBuffer,
2522 "glCopyNamedBufferSubData");
2523 if (!dst)
2524 return;
2525
2526 copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
2527 "glCopyNamedBufferSubData");
2528 }
2529
2530 static bool
2531 validate_map_buffer_range(struct gl_context *ctx,
2532 struct gl_buffer_object *bufObj, GLintptr offset,
2533 GLsizeiptr length, GLbitfield access,
2534 const char *func)
2535 {
2536 GLbitfield allowed_access;
2537
2538 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, false);
2539
2540 if (offset < 0) {
2541 _mesa_error(ctx, GL_INVALID_VALUE,
2542 "%s(offset %ld < 0)", func, (long) offset);
2543 return false;
2544 }
2545
2546 if (length < 0) {
2547 _mesa_error(ctx, GL_INVALID_VALUE,
2548 "%s(length %ld < 0)", func, (long) length);
2549 return false;
2550 }
2551
2552 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2553 *
2554 * "An INVALID_OPERATION error is generated for any of the following
2555 * conditions:
2556 *
2557 * * <length> is zero."
2558 *
2559 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2560 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2561 * either.
2562 */
2563 if (length == 0) {
2564 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(length = 0)", func);
2565 return false;
2566 }
2567
2568 allowed_access = GL_MAP_READ_BIT |
2569 GL_MAP_WRITE_BIT |
2570 GL_MAP_INVALIDATE_RANGE_BIT |
2571 GL_MAP_INVALIDATE_BUFFER_BIT |
2572 GL_MAP_FLUSH_EXPLICIT_BIT |
2573 GL_MAP_UNSYNCHRONIZED_BIT;
2574
2575 if (ctx->Extensions.ARB_buffer_storage) {
2576 allowed_access |= GL_MAP_PERSISTENT_BIT |
2577 GL_MAP_COHERENT_BIT;
2578 }
2579
2580 if (access & ~allowed_access) {
2581 /* generate an error if any bits other than those allowed are set */
2582 _mesa_error(ctx, GL_INVALID_VALUE,
2583 "%s(access has undefined bits set)", func);
2584 return false;
2585 }
2586
2587 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
2588 _mesa_error(ctx, GL_INVALID_OPERATION,
2589 "%s(access indicates neither read or write)", func);
2590 return false;
2591 }
2592
2593 if ((access & GL_MAP_READ_BIT) &&
2594 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
2595 GL_MAP_INVALIDATE_BUFFER_BIT |
2596 GL_MAP_UNSYNCHRONIZED_BIT))) {
2597 _mesa_error(ctx, GL_INVALID_OPERATION,
2598 "%s(read access with disallowed bits)", func);
2599 return false;
2600 }
2601
2602 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
2603 ((access & GL_MAP_WRITE_BIT) == 0)) {
2604 _mesa_error(ctx, GL_INVALID_OPERATION,
2605 "%s(access has flush explicit without write)", func);
2606 return false;
2607 }
2608
2609 if (access & GL_MAP_READ_BIT &&
2610 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
2611 _mesa_error(ctx, GL_INVALID_OPERATION,
2612 "%s(buffer does not allow read access)", func);
2613 return false;
2614 }
2615
2616 if (access & GL_MAP_WRITE_BIT &&
2617 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
2618 _mesa_error(ctx, GL_INVALID_OPERATION,
2619 "%s(buffer does not allow write access)", func);
2620 return false;
2621 }
2622
2623 if (access & GL_MAP_COHERENT_BIT &&
2624 !(bufObj->StorageFlags & GL_MAP_COHERENT_BIT)) {
2625 _mesa_error(ctx, GL_INVALID_OPERATION,
2626 "%s(buffer does not allow coherent access)", func);
2627 return false;
2628 }
2629
2630 if (access & GL_MAP_PERSISTENT_BIT &&
2631 !(bufObj->StorageFlags & GL_MAP_PERSISTENT_BIT)) {
2632 _mesa_error(ctx, GL_INVALID_OPERATION,
2633 "%s(buffer does not allow persistent access)", func);
2634 return false;
2635 }
2636
2637 if (offset + length > bufObj->Size) {
2638 _mesa_error(ctx, GL_INVALID_VALUE,
2639 "%s(offset %lu + length %lu > buffer_size %lu)", func,
2640 (unsigned long) offset, (unsigned long) length,
2641 (unsigned long) bufObj->Size);
2642 return false;
2643 }
2644
2645 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2646 _mesa_error(ctx, GL_INVALID_OPERATION,
2647 "%s(buffer already mapped)", func);
2648 return false;
2649 }
2650
2651 if (access & GL_MAP_WRITE_BIT) {
2652 bufObj->NumMapBufferWriteCalls++;
2653 if ((bufObj->Usage == GL_STATIC_DRAW ||
2654 bufObj->Usage == GL_STATIC_COPY) &&
2655 bufObj->NumMapBufferWriteCalls >= BUFFER_WARNING_CALL_COUNT) {
2656 BUFFER_USAGE_WARNING(ctx,
2657 "using %s(buffer %u, offset %u, length %u) to "
2658 "update a %s buffer",
2659 func, bufObj->Name, offset, length,
2660 _mesa_enum_to_string(bufObj->Usage));
2661 }
2662 }
2663
2664 return true;
2665 }
2666
2667 static void *
2668 map_buffer_range(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2669 GLintptr offset, GLsizeiptr length, GLbitfield access,
2670 const char *func)
2671 {
2672 if (!bufObj->Size) {
2673 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(buffer size = 0)", func);
2674 return NULL;
2675 }
2676
2677 assert(ctx->Driver.MapBufferRange);
2678 void *map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj,
2679 MAP_USER);
2680 if (!map) {
2681 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(map failed)", func);
2682 }
2683 else {
2684 /* The driver callback should have set all these fields.
2685 * This is important because other modules (like VBO) might call
2686 * the driver function directly.
2687 */
2688 assert(bufObj->Mappings[MAP_USER].Pointer == map);
2689 assert(bufObj->Mappings[MAP_USER].Length == length);
2690 assert(bufObj->Mappings[MAP_USER].Offset == offset);
2691 assert(bufObj->Mappings[MAP_USER].AccessFlags == access);
2692 }
2693
2694 if (access & GL_MAP_WRITE_BIT) {
2695 bufObj->Written = GL_TRUE;
2696 bufObj->MinMaxCacheDirty = true;
2697 }
2698
2699 #ifdef VBO_DEBUG
2700 if (strstr(func, "Range") == NULL) { /* If not MapRange */
2701 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2702 bufObj->Name, bufObj->Size, access);
2703 /* Access must be write only */
2704 if ((access & GL_MAP_WRITE_BIT) && (!(access & ~GL_MAP_WRITE_BIT))) {
2705 GLuint i;
2706 GLubyte *b = (GLubyte *) bufObj->Pointer;
2707 for (i = 0; i < bufObj->Size; i++)
2708 b[i] = i & 0xff;
2709 }
2710 }
2711 #endif
2712
2713 #ifdef BOUNDS_CHECK
2714 if (strstr(func, "Range") == NULL) { /* If not MapRange */
2715 GLubyte *buf = (GLubyte *) bufObj->Pointer;
2716 GLuint i;
2717 /* buffer is 100 bytes larger than requested, fill with magic value */
2718 for (i = 0; i < 100; i++) {
2719 buf[bufObj->Size - i - 1] = 123;
2720 }
2721 }
2722 #endif
2723
2724 return map;
2725 }
2726
2727 void * GLAPIENTRY
2728 _mesa_MapBufferRange_no_error(GLenum target, GLintptr offset,
2729 GLsizeiptr length, GLbitfield access)
2730 {
2731 GET_CURRENT_CONTEXT(ctx);
2732
2733 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
2734 struct gl_buffer_object *bufObj = *bufObjPtr;
2735
2736 return map_buffer_range(ctx, bufObj, offset, length, access,
2737 "glMapBufferRange");
2738 }
2739
2740 void * GLAPIENTRY
2741 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
2742 GLbitfield access)
2743 {
2744 GET_CURRENT_CONTEXT(ctx);
2745 struct gl_buffer_object *bufObj;
2746
2747 if (!ctx->Extensions.ARB_map_buffer_range) {
2748 _mesa_error(ctx, GL_INVALID_OPERATION,
2749 "glMapBufferRange(ARB_map_buffer_range not supported)");
2750 return NULL;
2751 }
2752
2753 bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
2754 if (!bufObj)
2755 return NULL;
2756
2757 if (!validate_map_buffer_range(ctx, bufObj, offset, length, access,
2758 "glMapBufferRange"))
2759 return NULL;
2760
2761 return map_buffer_range(ctx, bufObj, offset, length, access,
2762 "glMapBufferRange");
2763 }
2764
2765 void * GLAPIENTRY
2766 _mesa_MapNamedBufferRange_no_error(GLuint buffer, GLintptr offset,
2767 GLsizeiptr length, GLbitfield access)
2768 {
2769 GET_CURRENT_CONTEXT(ctx);
2770 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2771
2772 return map_buffer_range(ctx, bufObj, offset, length, access,
2773 "glMapNamedBufferRange");
2774 }
2775
2776 void * GLAPIENTRY
2777 _mesa_MapNamedBufferRange(GLuint buffer, GLintptr offset, GLsizeiptr length,
2778 GLbitfield access)
2779 {
2780 GET_CURRENT_CONTEXT(ctx);
2781 struct gl_buffer_object *bufObj;
2782
2783 if (!ctx->Extensions.ARB_map_buffer_range) {
2784 _mesa_error(ctx, GL_INVALID_OPERATION,
2785 "glMapNamedBufferRange("
2786 "ARB_map_buffer_range not supported)");
2787 return NULL;
2788 }
2789
2790 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBufferRange");
2791 if (!bufObj)
2792 return NULL;
2793
2794 if (!validate_map_buffer_range(ctx, bufObj, offset, length, access,
2795 "glMapNamedBufferRange"))
2796 return NULL;
2797
2798 return map_buffer_range(ctx, bufObj, offset, length, access,
2799 "glMapNamedBufferRange");
2800 }
2801
2802 /**
2803 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2804 * flags for input to map_buffer_range.
2805 *
2806 * \return true if the type of requested access is permissible.
2807 */
2808 static bool
2809 get_map_buffer_access_flags(struct gl_context *ctx, GLenum access,
2810 GLbitfield *flags)
2811 {
2812 switch (access) {
2813 case GL_READ_ONLY_ARB:
2814 *flags = GL_MAP_READ_BIT;
2815 return _mesa_is_desktop_gl(ctx);
2816 case GL_WRITE_ONLY_ARB:
2817 *flags = GL_MAP_WRITE_BIT;
2818 return true;
2819 case GL_READ_WRITE_ARB:
2820 *flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
2821 return _mesa_is_desktop_gl(ctx);
2822 default:
2823 return false;
2824 }
2825 }
2826
2827 void * GLAPIENTRY
2828 _mesa_MapBuffer_no_error(GLenum target, GLenum access)
2829 {
2830 GET_CURRENT_CONTEXT(ctx);
2831
2832 GLbitfield accessFlags;
2833 get_map_buffer_access_flags(ctx, access, &accessFlags);
2834
2835 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
2836 struct gl_buffer_object *bufObj = *bufObjPtr;
2837
2838 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2839 "glMapBuffer");
2840 }
2841
2842 void * GLAPIENTRY
2843 _mesa_MapBuffer(GLenum target, GLenum access)
2844 {
2845 GET_CURRENT_CONTEXT(ctx);
2846 struct gl_buffer_object *bufObj;
2847 GLbitfield accessFlags;
2848
2849 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
2850 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBuffer(invalid access)");
2851 return NULL;
2852 }
2853
2854 bufObj = get_buffer(ctx, "glMapBuffer", target, GL_INVALID_OPERATION);
2855 if (!bufObj)
2856 return NULL;
2857
2858 if (!validate_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2859 "glMapBuffer"))
2860 return NULL;
2861
2862 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2863 "glMapBuffer");
2864 }
2865
2866 void * GLAPIENTRY
2867 _mesa_MapNamedBuffer_no_error(GLuint buffer, GLenum access)
2868 {
2869 GET_CURRENT_CONTEXT(ctx);
2870
2871 GLbitfield accessFlags;
2872 get_map_buffer_access_flags(ctx, access, &accessFlags);
2873
2874 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2875
2876 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2877 "glMapNamedBuffer");
2878 }
2879
2880 void * GLAPIENTRY
2881 _mesa_MapNamedBuffer(GLuint buffer, GLenum access)
2882 {
2883 GET_CURRENT_CONTEXT(ctx);
2884 struct gl_buffer_object *bufObj;
2885 GLbitfield accessFlags;
2886
2887 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
2888 _mesa_error(ctx, GL_INVALID_ENUM, "glMapNamedBuffer(invalid access)");
2889 return NULL;
2890 }
2891
2892 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBuffer");
2893 if (!bufObj)
2894 return NULL;
2895
2896 if (!validate_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2897 "glMapNamedBuffer"))
2898 return NULL;
2899
2900 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2901 "glMapNamedBuffer");
2902 }
2903
2904
2905 static void
2906 flush_mapped_buffer_range(struct gl_context *ctx,
2907 struct gl_buffer_object *bufObj,
2908 GLintptr offset, GLsizeiptr length,
2909 const char *func)
2910 {
2911 if (!ctx->Extensions.ARB_map_buffer_range) {
2912 _mesa_error(ctx, GL_INVALID_OPERATION,
2913 "%s(ARB_map_buffer_range not supported)", func);
2914 return;
2915 }
2916
2917 if (offset < 0) {
2918 _mesa_error(ctx, GL_INVALID_VALUE,
2919 "%s(offset %ld < 0)", func, (long) offset);
2920 return;
2921 }
2922
2923 if (length < 0) {
2924 _mesa_error(ctx, GL_INVALID_VALUE,
2925 "%s(length %ld < 0)", func, (long) length);
2926 return;
2927 }
2928
2929 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2930 /* buffer is not mapped */
2931 _mesa_error(ctx, GL_INVALID_OPERATION,
2932 "%s(buffer is not mapped)", func);
2933 return;
2934 }
2935
2936 if ((bufObj->Mappings[MAP_USER].AccessFlags &
2937 GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
2938 _mesa_error(ctx, GL_INVALID_OPERATION,
2939 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func);
2940 return;
2941 }
2942
2943 if (offset + length > bufObj->Mappings[MAP_USER].Length) {
2944 _mesa_error(ctx, GL_INVALID_VALUE,
2945 "%s(offset %ld + length %ld > mapped length %ld)", func,
2946 (long) offset, (long) length,
2947 (long) bufObj->Mappings[MAP_USER].Length);
2948 return;
2949 }
2950
2951 assert(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_WRITE_BIT);
2952
2953 if (ctx->Driver.FlushMappedBufferRange)
2954 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
2955 MAP_USER);
2956 }
2957
2958 void GLAPIENTRY
2959 _mesa_FlushMappedBufferRange_no_error(GLenum target, GLintptr offset,
2960 GLsizeiptr length)
2961 {
2962 GET_CURRENT_CONTEXT(ctx);
2963 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
2964 struct gl_buffer_object *bufObj = *bufObjPtr;
2965
2966 if (ctx->Driver.FlushMappedBufferRange)
2967 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
2968 MAP_USER);
2969 }
2970
2971 void GLAPIENTRY
2972 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset,
2973 GLsizeiptr length)
2974 {
2975 GET_CURRENT_CONTEXT(ctx);
2976 struct gl_buffer_object *bufObj;
2977
2978 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
2979 GL_INVALID_OPERATION);
2980 if (!bufObj)
2981 return;
2982
2983 flush_mapped_buffer_range(ctx, bufObj, offset, length,
2984 "glFlushMappedBufferRange");
2985 }
2986
2987 void GLAPIENTRY
2988 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer, GLintptr offset,
2989 GLsizeiptr length)
2990 {
2991 GET_CURRENT_CONTEXT(ctx);
2992 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2993
2994 if (ctx->Driver.FlushMappedBufferRange)
2995 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
2996 MAP_USER);
2997 }
2998
2999 void GLAPIENTRY
3000 _mesa_FlushMappedNamedBufferRange(GLuint buffer, GLintptr offset,
3001 GLsizeiptr length)
3002 {
3003 GET_CURRENT_CONTEXT(ctx);
3004 struct gl_buffer_object *bufObj;
3005
3006 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
3007 "glFlushMappedNamedBufferRange");
3008 if (!bufObj)
3009 return;
3010
3011 flush_mapped_buffer_range(ctx, bufObj, offset, length,
3012 "glFlushMappedNamedBufferRange");
3013 }
3014
3015
3016 /**
3017 * Binds a buffer object to a uniform buffer binding point.
3018 *
3019 * The caller is responsible for flushing vertices and updating
3020 * NewDriverState.
3021 */
3022 static void
3023 set_ubo_binding(struct gl_context *ctx,
3024 struct gl_uniform_buffer_binding *binding,
3025 struct gl_buffer_object *bufObj,
3026 GLintptr offset,
3027 GLsizeiptr size,
3028 GLboolean autoSize)
3029 {
3030 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
3031
3032 binding->Offset = offset;
3033 binding->Size = size;
3034 binding->AutomaticSize = autoSize;
3035
3036 /* If this is a real buffer object, mark it has having been used
3037 * at some point as a UBO.
3038 */
3039 if (size >= 0)
3040 bufObj->UsageHistory |= USAGE_UNIFORM_BUFFER;
3041 }
3042
3043 /**
3044 * Binds a buffer object to a shader storage buffer binding point.
3045 *
3046 * The caller is responsible for flushing vertices and updating
3047 * NewDriverState.
3048 */
3049 static void
3050 set_ssbo_binding(struct gl_context *ctx,
3051 struct gl_shader_storage_buffer_binding *binding,
3052 struct gl_buffer_object *bufObj,
3053 GLintptr offset,
3054 GLsizeiptr size,
3055 GLboolean autoSize)
3056 {
3057 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
3058
3059 binding->Offset = offset;
3060 binding->Size = size;
3061 binding->AutomaticSize = autoSize;
3062
3063 /* If this is a real buffer object, mark it has having been used
3064 * at some point as a SSBO.
3065 */
3066 if (size >= 0)
3067 bufObj->UsageHistory |= USAGE_SHADER_STORAGE_BUFFER;
3068 }
3069
3070 /**
3071 * Binds a buffer object to a uniform buffer binding point.
3072 *
3073 * Unlike set_ubo_binding(), this function also flushes vertices
3074 * and updates NewDriverState. It also checks if the binding
3075 * has actually changed before updating it.
3076 */
3077 static void
3078 bind_uniform_buffer(struct gl_context *ctx,
3079 GLuint index,
3080 struct gl_buffer_object *bufObj,
3081 GLintptr offset,
3082 GLsizeiptr size,
3083 GLboolean autoSize)
3084 {
3085 struct gl_uniform_buffer_binding *binding =
3086 &ctx->UniformBufferBindings[index];
3087
3088 if (binding->BufferObject == bufObj &&
3089 binding->Offset == offset &&
3090 binding->Size == size &&
3091 binding->AutomaticSize == autoSize) {
3092 return;
3093 }
3094
3095 FLUSH_VERTICES(ctx, 0);
3096 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3097
3098 set_ubo_binding(ctx, binding, bufObj, offset, size, autoSize);
3099 }
3100
3101 /**
3102 * Binds a buffer object to a shader storage buffer binding point.
3103 *
3104 * Unlike set_ssbo_binding(), this function also flushes vertices
3105 * and updates NewDriverState. It also checks if the binding
3106 * has actually changed before updating it.
3107 */
3108 static void
3109 bind_shader_storage_buffer(struct gl_context *ctx,
3110 GLuint index,
3111 struct gl_buffer_object *bufObj,
3112 GLintptr offset,
3113 GLsizeiptr size,
3114 GLboolean autoSize)
3115 {
3116 struct gl_shader_storage_buffer_binding *binding =
3117 &ctx->ShaderStorageBufferBindings[index];
3118
3119 if (binding->BufferObject == bufObj &&
3120 binding->Offset == offset &&
3121 binding->Size == size &&
3122 binding->AutomaticSize == autoSize) {
3123 return;
3124 }
3125
3126 FLUSH_VERTICES(ctx, 0);
3127 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
3128
3129 set_ssbo_binding(ctx, binding, bufObj, offset, size, autoSize);
3130 }
3131
3132 static void
3133 bind_buffer_range_uniform_buffer(struct gl_context *ctx, GLuint index,
3134 struct gl_buffer_object *bufObj,
3135 GLintptr offset, GLsizeiptr size)
3136 {
3137 if (bufObj == ctx->Shared->NullBufferObj) {
3138 offset = -1;
3139 size = -1;
3140 }
3141
3142 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
3143 bind_uniform_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
3144 }
3145
3146 /**
3147 * Bind a region of a buffer object to a uniform block binding point.
3148 * \param index the uniform buffer binding point index
3149 * \param bufObj the buffer object
3150 * \param offset offset to the start of buffer object region
3151 * \param size size of the buffer object region
3152 */
3153 static void
3154 bind_buffer_range_uniform_buffer_err(struct gl_context *ctx, GLuint index,
3155 struct gl_buffer_object *bufObj,
3156 GLintptr offset, GLsizeiptr size)
3157 {
3158 if (index >= ctx->Const.MaxUniformBufferBindings) {
3159 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
3160 return;
3161 }
3162
3163 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3164 _mesa_error(ctx, GL_INVALID_VALUE,
3165 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
3166 ctx->Const.UniformBufferOffsetAlignment);
3167 return;
3168 }
3169
3170 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
3171 }
3172
3173 static void
3174 bind_buffer_range_shader_storage_buffer(struct gl_context *ctx,
3175 GLuint index,
3176 struct gl_buffer_object *bufObj,
3177 GLintptr offset,
3178 GLsizeiptr size)
3179 {
3180 if (bufObj == ctx->Shared->NullBufferObj) {
3181 offset = -1;
3182 size = -1;
3183 }
3184
3185 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
3186 bind_shader_storage_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
3187 }
3188
3189 /**
3190 * Bind a region of a buffer object to a shader storage block binding point.
3191 * \param index the shader storage buffer binding point index
3192 * \param bufObj the buffer object
3193 * \param offset offset to the start of buffer object region
3194 * \param size size of the buffer object region
3195 */
3196 static void
3197 bind_buffer_range_shader_storage_buffer_err(struct gl_context *ctx,
3198 GLuint index,
3199 struct gl_buffer_object *bufObj,
3200 GLintptr offset, GLsizeiptr size)
3201 {
3202 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
3203 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
3204 return;
3205 }
3206
3207 if (offset & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
3208 _mesa_error(ctx, GL_INVALID_VALUE,
3209 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
3210 ctx->Const.ShaderStorageBufferOffsetAlignment);
3211 return;
3212 }
3213
3214 bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset, size);
3215 }
3216
3217 /**
3218 * Bind a buffer object to a uniform block binding point.
3219 * As above, but offset = 0.
3220 */
3221 static void
3222 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
3223 GLuint index,
3224 struct gl_buffer_object *bufObj)
3225 {
3226 if (index >= ctx->Const.MaxUniformBufferBindings) {
3227 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
3228 return;
3229 }
3230
3231 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
3232
3233 if (bufObj == ctx->Shared->NullBufferObj)
3234 bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
3235 else
3236 bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
3237 }
3238
3239 /**
3240 * Bind a buffer object to a shader storage block binding point.
3241 * As above, but offset = 0.
3242 */
3243 static void
3244 bind_buffer_base_shader_storage_buffer(struct gl_context *ctx,
3245 GLuint index,
3246 struct gl_buffer_object *bufObj)
3247 {
3248 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
3249 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
3250 return;
3251 }
3252
3253 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
3254
3255 if (bufObj == ctx->Shared->NullBufferObj)
3256 bind_shader_storage_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
3257 else
3258 bind_shader_storage_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
3259 }
3260
3261 /**
3262 * Binds a buffer object to an atomic buffer binding point.
3263 *
3264 * The caller is responsible for validating the offset,
3265 * flushing the vertices and updating NewDriverState.
3266 */
3267 static void
3268 set_atomic_buffer_binding(struct gl_context *ctx,
3269 struct gl_atomic_buffer_binding *binding,
3270 struct gl_buffer_object *bufObj,
3271 GLintptr offset,
3272 GLsizeiptr size)
3273 {
3274 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
3275
3276 if (bufObj == ctx->Shared->NullBufferObj) {
3277 binding->Offset = 0;
3278 binding->Size = 0;
3279 } else {
3280 binding->Offset = offset;
3281 binding->Size = size;
3282 bufObj->UsageHistory |= USAGE_ATOMIC_COUNTER_BUFFER;
3283 }
3284 }
3285
3286 static void
3287 bind_atomic_buffer(struct gl_context *ctx, unsigned index,
3288 struct gl_buffer_object *bufObj, GLintptr offset,
3289 GLsizeiptr size)
3290 {
3291 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
3292
3293 struct gl_atomic_buffer_binding *binding =
3294 &ctx->AtomicBufferBindings[index];
3295 if (binding->BufferObject == bufObj &&
3296 binding->Offset == offset &&
3297 binding->Size == size) {
3298 return;
3299 }
3300
3301 FLUSH_VERTICES(ctx, 0);
3302 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3303
3304 set_atomic_buffer_binding(ctx, binding, bufObj, offset, size);
3305 }
3306
3307 /**
3308 * Binds a buffer object to an atomic buffer binding point.
3309 *
3310 * Unlike set_atomic_buffer_binding(), this function also validates the
3311 * index and offset, flushes vertices, and updates NewDriverState.
3312 * It also checks if the binding has actually changing before
3313 * updating it.
3314 */
3315 static void
3316 bind_atomic_buffer_err(struct gl_context *ctx, unsigned index,
3317 struct gl_buffer_object *bufObj, GLintptr offset,
3318 GLsizeiptr size, const char *name)
3319 {
3320 if (index >= ctx->Const.MaxAtomicBufferBindings) {
3321 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
3322 return;
3323 }
3324
3325 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
3326 _mesa_error(ctx, GL_INVALID_VALUE,
3327 "%s(offset misaligned %d/%d)", name, (int) offset,
3328 ATOMIC_COUNTER_SIZE);
3329 return;
3330 }
3331
3332 bind_atomic_buffer(ctx, index, bufObj, offset, size);
3333 }
3334
3335 static inline bool
3336 bind_buffers_check_offset_and_size(struct gl_context *ctx,
3337 GLuint index,
3338 const GLintptr *offsets,
3339 const GLsizeiptr *sizes)
3340 {
3341 if (offsets[index] < 0) {
3342 /* The ARB_multi_bind spec says:
3343 *
3344 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3345 * value in <offsets> is less than zero (per binding)."
3346 */
3347 _mesa_error(ctx, GL_INVALID_VALUE,
3348 "glBindBuffersRange(offsets[%u]=%" PRId64 " < 0)",
3349 index, (int64_t) offsets[index]);
3350 return false;
3351 }
3352
3353 if (sizes[index] <= 0) {
3354 /* The ARB_multi_bind spec says:
3355 *
3356 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3357 * value in <sizes> is less than or equal to zero (per binding)."
3358 */
3359 _mesa_error(ctx, GL_INVALID_VALUE,
3360 "glBindBuffersRange(sizes[%u]=%" PRId64 " <= 0)",
3361 index, (int64_t) sizes[index]);
3362 return false;
3363 }
3364
3365 return true;
3366 }
3367
3368 static bool
3369 error_check_bind_uniform_buffers(struct gl_context *ctx,
3370 GLuint first, GLsizei count,
3371 const char *caller)
3372 {
3373 if (!ctx->Extensions.ARB_uniform_buffer_object) {
3374 _mesa_error(ctx, GL_INVALID_ENUM,
3375 "%s(target=GL_UNIFORM_BUFFER)", caller);
3376 return false;
3377 }
3378
3379 /* The ARB_multi_bind_spec says:
3380 *
3381 * "An INVALID_OPERATION error is generated if <first> + <count> is
3382 * greater than the number of target-specific indexed binding points,
3383 * as described in section 6.7.1."
3384 */
3385 if (first + count > ctx->Const.MaxUniformBufferBindings) {
3386 _mesa_error(ctx, GL_INVALID_OPERATION,
3387 "%s(first=%u + count=%d > the value of "
3388 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3389 caller, first, count,
3390 ctx->Const.MaxUniformBufferBindings);
3391 return false;
3392 }
3393
3394 return true;
3395 }
3396
3397 static bool
3398 error_check_bind_shader_storage_buffers(struct gl_context *ctx,
3399 GLuint first, GLsizei count,
3400 const char *caller)
3401 {
3402 if (!ctx->Extensions.ARB_shader_storage_buffer_object) {
3403 _mesa_error(ctx, GL_INVALID_ENUM,
3404 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller);
3405 return false;
3406 }
3407
3408 /* The ARB_multi_bind_spec says:
3409 *
3410 * "An INVALID_OPERATION error is generated if <first> + <count> is
3411 * greater than the number of target-specific indexed binding points,
3412 * as described in section 6.7.1."
3413 */
3414 if (first + count > ctx->Const.MaxShaderStorageBufferBindings) {
3415 _mesa_error(ctx, GL_INVALID_OPERATION,
3416 "%s(first=%u + count=%d > the value of "
3417 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3418 caller, first, count,
3419 ctx->Const.MaxShaderStorageBufferBindings);
3420 return false;
3421 }
3422
3423 return true;
3424 }
3425
3426 /**
3427 * Unbind all uniform buffers in the range
3428 * <first> through <first>+<count>-1
3429 */
3430 static void
3431 unbind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3432 {
3433 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
3434
3435 for (int i = 0; i < count; i++)
3436 set_ubo_binding(ctx, &ctx->UniformBufferBindings[first + i],
3437 bufObj, -1, -1, GL_TRUE);
3438 }
3439
3440 /**
3441 * Unbind all shader storage buffers in the range
3442 * <first> through <first>+<count>-1
3443 */
3444 static void
3445 unbind_shader_storage_buffers(struct gl_context *ctx, GLuint first,
3446 GLsizei count)
3447 {
3448 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
3449
3450 for (int i = 0; i < count; i++)
3451 set_ssbo_binding(ctx, &ctx->ShaderStorageBufferBindings[first + i],
3452 bufObj, -1, -1, GL_TRUE);
3453 }
3454
3455 static void
3456 bind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count,
3457 const GLuint *buffers,
3458 bool range,
3459 const GLintptr *offsets, const GLsizeiptr *sizes,
3460 const char *caller)
3461 {
3462 if (!error_check_bind_uniform_buffers(ctx, first, count, caller))
3463 return;
3464
3465 /* Assume that at least one binding will be changed */
3466 FLUSH_VERTICES(ctx, 0);
3467 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3468
3469 if (!buffers) {
3470 /* The ARB_multi_bind spec says:
3471 *
3472 * "If <buffers> is NULL, all bindings from <first> through
3473 * <first>+<count>-1 are reset to their unbound (zero) state.
3474 * In this case, the offsets and sizes associated with the
3475 * binding points are set to default values, ignoring
3476 * <offsets> and <sizes>."
3477 */
3478 unbind_uniform_buffers(ctx, first, count);
3479 return;
3480 }
3481
3482 /* Note that the error semantics for multi-bind commands differ from
3483 * those of other GL commands.
3484 *
3485 * The Issues section in the ARB_multi_bind spec says:
3486 *
3487 * "(11) Typically, OpenGL specifies that if an error is generated by a
3488 * command, that command has no effect. This is somewhat
3489 * unfortunate for multi-bind commands, because it would require a
3490 * first pass to scan the entire list of bound objects for errors
3491 * and then a second pass to actually perform the bindings.
3492 * Should we have different error semantics?
3493 *
3494 * RESOLVED: Yes. In this specification, when the parameters for
3495 * one of the <count> binding points are invalid, that binding point
3496 * is not updated and an error will be generated. However, other
3497 * binding points in the same command will be updated if their
3498 * parameters are valid and no other error occurs."
3499 */
3500
3501 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
3502
3503 for (int i = 0; i < count; i++) {
3504 struct gl_uniform_buffer_binding *binding =
3505 &ctx->UniformBufferBindings[first + i];
3506 struct gl_buffer_object *bufObj;
3507 GLintptr offset = 0;
3508 GLsizeiptr size = 0;
3509
3510 if (range) {
3511 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3512 continue;
3513
3514 /* The ARB_multi_bind spec says:
3515 *
3516 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3517 * pair of values in <offsets> and <sizes> does not respectively
3518 * satisfy the constraints described for those parameters for the
3519 * specified target, as described in section 6.7.1 (per binding)."
3520 *
3521 * Section 6.7.1 refers to table 6.5, which says:
3522 *
3523 * "┌───────────────────────────────────────────────────────────────┐
3524 * │ Uniform buffer array bindings (see sec. 7.6) │
3525 * ├─────────────────────┬─────────────────────────────────────────┤
3526 * │ ... │ ... │
3527 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3528 * │ │ OFFSET_ALIGNMENT │
3529 * │ ... │ ... │
3530 * │ size restriction │ none │
3531 * └─────────────────────┴─────────────────────────────────────────┘"
3532 */
3533 if (offsets[i] & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3534 _mesa_error(ctx, GL_INVALID_VALUE,
3535 "glBindBuffersRange(offsets[%u]=%" PRId64
3536 " is misaligned; it must be a multiple of the value of "
3537 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3538 "target=GL_UNIFORM_BUFFER)",
3539 i, (int64_t) offsets[i],
3540 ctx->Const.UniformBufferOffsetAlignment);
3541 continue;
3542 }
3543
3544 offset = offsets[i];
3545 size = sizes[i];
3546 }
3547
3548 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3549 bufObj = binding->BufferObject;
3550 else
3551 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller);
3552
3553 if (bufObj) {
3554 if (bufObj == ctx->Shared->NullBufferObj)
3555 set_ubo_binding(ctx, binding, bufObj, -1, -1, !range);
3556 else
3557 set_ubo_binding(ctx, binding, bufObj, offset, size, !range);
3558 }
3559 }
3560
3561 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
3562 }
3563
3564 static void
3565 bind_shader_storage_buffers(struct gl_context *ctx, GLuint first,
3566 GLsizei count, const GLuint *buffers,
3567 bool range,
3568 const GLintptr *offsets,
3569 const GLsizeiptr *sizes,
3570 const char *caller)
3571 {
3572 if (!error_check_bind_shader_storage_buffers(ctx, first, count, caller))
3573 return;
3574
3575 /* Assume that at least one binding will be changed */
3576 FLUSH_VERTICES(ctx, 0);
3577 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
3578
3579 if (!buffers) {
3580 /* The ARB_multi_bind spec says:
3581 *
3582 * "If <buffers> is NULL, all bindings from <first> through
3583 * <first>+<count>-1 are reset to their unbound (zero) state.
3584 * In this case, the offsets and sizes associated with the
3585 * binding points are set to default values, ignoring
3586 * <offsets> and <sizes>."
3587 */
3588 unbind_shader_storage_buffers(ctx, first, count);
3589 return;
3590 }
3591
3592 /* Note that the error semantics for multi-bind commands differ from
3593 * those of other GL commands.
3594 *
3595 * The Issues section in the ARB_multi_bind spec says:
3596 *
3597 * "(11) Typically, OpenGL specifies that if an error is generated by a
3598 * command, that command has no effect. This is somewhat
3599 * unfortunate for multi-bind commands, because it would require a
3600 * first pass to scan the entire list of bound objects for errors
3601 * and then a second pass to actually perform the bindings.
3602 * Should we have different error semantics?
3603 *
3604 * RESOLVED: Yes. In this specification, when the parameters for
3605 * one of the <count> binding points are invalid, that binding point
3606 * is not updated and an error will be generated. However, other
3607 * binding points in the same command will be updated if their
3608 * parameters are valid and no other error occurs."
3609 */
3610
3611 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
3612
3613 for (int i = 0; i < count; i++) {
3614 struct gl_shader_storage_buffer_binding *binding =
3615 &ctx->ShaderStorageBufferBindings[first + i];
3616 struct gl_buffer_object *bufObj;
3617 GLintptr offset = 0;
3618 GLsizeiptr size = 0;
3619
3620 if (range) {
3621 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3622 continue;
3623
3624 /* The ARB_multi_bind spec says:
3625 *
3626 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3627 * pair of values in <offsets> and <sizes> does not respectively
3628 * satisfy the constraints described for those parameters for the
3629 * specified target, as described in section 6.7.1 (per binding)."
3630 *
3631 * Section 6.7.1 refers to table 6.5, which says:
3632 *
3633 * "┌───────────────────────────────────────────────────────────────┐
3634 * │ Shader storage buffer array bindings (see sec. 7.8) │
3635 * ├─────────────────────┬─────────────────────────────────────────┤
3636 * │ ... │ ... │
3637 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3638 * │ │ BUFFER_OFFSET_ALIGNMENT │
3639 * │ ... │ ... │
3640 * │ size restriction │ none │
3641 * └─────────────────────┴─────────────────────────────────────────┘"
3642 */
3643 if (offsets[i] & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
3644 _mesa_error(ctx, GL_INVALID_VALUE,
3645 "glBindBuffersRange(offsets[%u]=%" PRId64
3646 " is misaligned; it must be a multiple of the value of "
3647 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3648 "target=GL_SHADER_STORAGE_BUFFER)",
3649 i, (int64_t) offsets[i],
3650 ctx->Const.ShaderStorageBufferOffsetAlignment);
3651 continue;
3652 }
3653
3654 offset = offsets[i];
3655 size = sizes[i];
3656 }
3657
3658 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3659 bufObj = binding->BufferObject;
3660 else
3661 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller);
3662
3663 if (bufObj) {
3664 if (bufObj == ctx->Shared->NullBufferObj)
3665 set_ssbo_binding(ctx, binding, bufObj, -1, -1, !range);
3666 else
3667 set_ssbo_binding(ctx, binding, bufObj, offset, size, !range);
3668 }
3669 }
3670
3671 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
3672 }
3673
3674 static bool
3675 error_check_bind_xfb_buffers(struct gl_context *ctx,
3676 struct gl_transform_feedback_object *tfObj,
3677 GLuint first, GLsizei count, const char *caller)
3678 {
3679 if (!ctx->Extensions.EXT_transform_feedback) {
3680 _mesa_error(ctx, GL_INVALID_ENUM,
3681 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller);
3682 return false;
3683 }
3684
3685 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3686 *
3687 * "An INVALID_OPERATION error is generated :
3688 *
3689 * ...
3690 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3691 * FEEDBACK_BUFFER and transform feedback is currently active."
3692 *
3693 * We assume that this is also meant to apply to BindBuffersRange
3694 * and BindBuffersBase.
3695 */
3696 if (tfObj->Active) {
3697 _mesa_error(ctx, GL_INVALID_OPERATION,
3698 "%s(Changing transform feedback buffers while "
3699 "transform feedback is active)", caller);
3700 return false;
3701 }
3702
3703 /* The ARB_multi_bind_spec says:
3704 *
3705 * "An INVALID_OPERATION error is generated if <first> + <count> is
3706 * greater than the number of target-specific indexed binding points,
3707 * as described in section 6.7.1."
3708 */
3709 if (first + count > ctx->Const.MaxTransformFeedbackBuffers) {
3710 _mesa_error(ctx, GL_INVALID_OPERATION,
3711 "%s(first=%u + count=%d > the value of "
3712 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3713 caller, first, count,
3714 ctx->Const.MaxTransformFeedbackBuffers);
3715 return false;
3716 }
3717
3718 return true;
3719 }
3720
3721 /**
3722 * Unbind all transform feedback buffers in the range
3723 * <first> through <first>+<count>-1
3724 */
3725 static void
3726 unbind_xfb_buffers(struct gl_context *ctx,
3727 struct gl_transform_feedback_object *tfObj,
3728 GLuint first, GLsizei count)
3729 {
3730 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3731
3732 for (int i = 0; i < count; i++)
3733 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
3734 bufObj, 0, 0);
3735 }
3736
3737 static void
3738 bind_xfb_buffers(struct gl_context *ctx,
3739 GLuint first, GLsizei count,
3740 const GLuint *buffers,
3741 bool range,
3742 const GLintptr *offsets,
3743 const GLsizeiptr *sizes,
3744 const char *caller)
3745 {
3746 struct gl_transform_feedback_object *tfObj =
3747 ctx->TransformFeedback.CurrentObject;
3748
3749 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count, caller))
3750 return;
3751
3752 /* Assume that at least one binding will be changed */
3753 FLUSH_VERTICES(ctx, 0);
3754 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
3755
3756 if (!buffers) {
3757 /* The ARB_multi_bind spec says:
3758 *
3759 * "If <buffers> is NULL, all bindings from <first> through
3760 * <first>+<count>-1 are reset to their unbound (zero) state.
3761 * In this case, the offsets and sizes associated with the
3762 * binding points are set to default values, ignoring
3763 * <offsets> and <sizes>."
3764 */
3765 unbind_xfb_buffers(ctx, tfObj, first, count);
3766 return;
3767 }
3768
3769 /* Note that the error semantics for multi-bind commands differ from
3770 * those of other GL commands.
3771 *
3772 * The Issues section in the ARB_multi_bind spec says:
3773 *
3774 * "(11) Typically, OpenGL specifies that if an error is generated by a
3775 * command, that command has no effect. This is somewhat
3776 * unfortunate for multi-bind commands, because it would require a
3777 * first pass to scan the entire list of bound objects for errors
3778 * and then a second pass to actually perform the bindings.
3779 * Should we have different error semantics?
3780 *
3781 * RESOLVED: Yes. In this specification, when the parameters for
3782 * one of the <count> binding points are invalid, that binding point
3783 * is not updated and an error will be generated. However, other
3784 * binding points in the same command will be updated if their
3785 * parameters are valid and no other error occurs."
3786 */
3787
3788 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
3789
3790 for (int i = 0; i < count; i++) {
3791 const GLuint index = first + i;
3792 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[index];
3793 struct gl_buffer_object *bufObj;
3794 GLintptr offset = 0;
3795 GLsizeiptr size = 0;
3796
3797 if (range) {
3798 offset = offsets[i];
3799 size = sizes[i];
3800
3801 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3802 continue;
3803
3804 /* The ARB_multi_bind spec says:
3805 *
3806 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3807 * pair of values in <offsets> and <sizes> does not respectively
3808 * satisfy the constraints described for those parameters for the
3809 * specified target, as described in section 6.7.1 (per binding)."
3810 *
3811 * Section 6.7.1 refers to table 6.5, which says:
3812 *
3813 * "┌───────────────────────────────────────────────────────────────┐
3814 * │ Transform feedback array bindings (see sec. 13.2.2) │
3815 * ├───────────────────────┬───────────────────────────────────────┤
3816 * │ ... │ ... │
3817 * │ offset restriction │ multiple of 4 │
3818 * │ ... │ ... │
3819 * │ size restriction │ multiple of 4 │
3820 * └───────────────────────┴───────────────────────────────────────┘"
3821 */
3822 if (offsets[i] & 0x3) {
3823 _mesa_error(ctx, GL_INVALID_VALUE,
3824 "glBindBuffersRange(offsets[%u]=%" PRId64
3825 " is misaligned; it must be a multiple of 4 when "
3826 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3827 i, (int64_t) offsets[i]);
3828 continue;
3829 }
3830
3831 if (sizes[i] & 0x3) {
3832 _mesa_error(ctx, GL_INVALID_VALUE,
3833 "glBindBuffersRange(sizes[%u]=%" PRId64
3834 " is misaligned; it must be a multiple of 4 when "
3835 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3836 i, (int64_t) sizes[i]);
3837 continue;
3838 }
3839
3840 offset = offsets[i];
3841 size = sizes[i];
3842 }
3843
3844 if (boundBufObj && boundBufObj->Name == buffers[i])
3845 bufObj = boundBufObj;
3846 else
3847 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller);
3848
3849 if (bufObj)
3850 _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj,
3851 offset, size);
3852 }
3853
3854 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
3855 }
3856
3857 static bool
3858 error_check_bind_atomic_buffers(struct gl_context *ctx,
3859 GLuint first, GLsizei count,
3860 const char *caller)
3861 {
3862 if (!ctx->Extensions.ARB_shader_atomic_counters) {
3863 _mesa_error(ctx, GL_INVALID_ENUM,
3864 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller);
3865 return false;
3866 }
3867
3868 /* The ARB_multi_bind_spec says:
3869 *
3870 * "An INVALID_OPERATION error is generated if <first> + <count> is
3871 * greater than the number of target-specific indexed binding points,
3872 * as described in section 6.7.1."
3873 */
3874 if (first + count > ctx->Const.MaxAtomicBufferBindings) {
3875 _mesa_error(ctx, GL_INVALID_OPERATION,
3876 "%s(first=%u + count=%d > the value of "
3877 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3878 caller, first, count, ctx->Const.MaxAtomicBufferBindings);
3879 return false;
3880 }
3881
3882 return true;
3883 }
3884
3885 /**
3886 * Unbind all atomic counter buffers in the range
3887 * <first> through <first>+<count>-1
3888 */
3889 static void
3890 unbind_atomic_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3891 {
3892 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3893
3894 for (int i = 0; i < count; i++)
3895 set_atomic_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i],
3896 bufObj, -1, -1);
3897 }
3898
3899 static void
3900 bind_atomic_buffers(struct gl_context *ctx,
3901 GLuint first,
3902 GLsizei count,
3903 const GLuint *buffers,
3904 bool range,
3905 const GLintptr *offsets,
3906 const GLsizeiptr *sizes,
3907 const char *caller)
3908 {
3909 if (!error_check_bind_atomic_buffers(ctx, first, count, caller))
3910 return;
3911
3912 /* Assume that at least one binding will be changed */
3913 FLUSH_VERTICES(ctx, 0);
3914 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3915
3916 if (!buffers) {
3917 /* The ARB_multi_bind spec says:
3918 *
3919 * "If <buffers> is NULL, all bindings from <first> through
3920 * <first>+<count>-1 are reset to their unbound (zero) state.
3921 * In this case, the offsets and sizes associated with the
3922 * binding points are set to default values, ignoring
3923 * <offsets> and <sizes>."
3924 */
3925 unbind_atomic_buffers(ctx, first, count);
3926 return;
3927 }
3928
3929 /* Note that the error semantics for multi-bind commands differ from
3930 * those of other GL commands.
3931 *
3932 * The Issues section in the ARB_multi_bind spec says:
3933 *
3934 * "(11) Typically, OpenGL specifies that if an error is generated by a
3935 * command, that command has no effect. This is somewhat
3936 * unfortunate for multi-bind commands, because it would require a
3937 * first pass to scan the entire list of bound objects for errors
3938 * and then a second pass to actually perform the bindings.
3939 * Should we have different error semantics?
3940 *
3941 * RESOLVED: Yes. In this specification, when the parameters for
3942 * one of the <count> binding points are invalid, that binding point
3943 * is not updated and an error will be generated. However, other
3944 * binding points in the same command will be updated if their
3945 * parameters are valid and no other error occurs."
3946 */
3947
3948 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
3949
3950 for (int i = 0; i < count; i++) {
3951 struct gl_atomic_buffer_binding *binding =
3952 &ctx->AtomicBufferBindings[first + i];
3953 struct gl_buffer_object *bufObj;
3954 GLintptr offset = 0;
3955 GLsizeiptr size = 0;
3956
3957 if (range) {
3958 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3959 continue;
3960
3961 /* The ARB_multi_bind spec says:
3962 *
3963 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3964 * pair of values in <offsets> and <sizes> does not respectively
3965 * satisfy the constraints described for those parameters for the
3966 * specified target, as described in section 6.7.1 (per binding)."
3967 *
3968 * Section 6.7.1 refers to table 6.5, which says:
3969 *
3970 * "┌───────────────────────────────────────────────────────────────┐
3971 * │ Atomic counter array bindings (see sec. 7.7.2) │
3972 * ├───────────────────────┬───────────────────────────────────────┤
3973 * │ ... │ ... │
3974 * │ offset restriction │ multiple of 4 │
3975 * │ ... │ ... │
3976 * │ size restriction │ none │
3977 * └───────────────────────┴───────────────────────────────────────┘"
3978 */
3979 if (offsets[i] & (ATOMIC_COUNTER_SIZE - 1)) {
3980 _mesa_error(ctx, GL_INVALID_VALUE,
3981 "glBindBuffersRange(offsets[%u]=%" PRId64
3982 " is misaligned; it must be a multiple of %d when "
3983 "target=GL_ATOMIC_COUNTER_BUFFER)",
3984 i, (int64_t) offsets[i], ATOMIC_COUNTER_SIZE);
3985 continue;
3986 }
3987
3988 offset = offsets[i];
3989 size = sizes[i];
3990 }
3991
3992 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3993 bufObj = binding->BufferObject;
3994 else
3995 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller);
3996
3997 if (bufObj)
3998 set_atomic_buffer_binding(ctx, binding, bufObj, offset, size);
3999 }
4000
4001 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
4002 }
4003
4004 static ALWAYS_INLINE void
4005 bind_buffer_range(GLenum target, GLuint index, GLuint buffer, GLintptr offset,
4006 GLsizeiptr size, bool no_error)
4007 {
4008 GET_CURRENT_CONTEXT(ctx);
4009 struct gl_buffer_object *bufObj;
4010
4011 if (MESA_VERBOSE & VERBOSE_API) {
4012 _mesa_debug(ctx, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4013 _mesa_enum_to_string(target), index, buffer,
4014 (unsigned long) offset, (unsigned long) size);
4015 }
4016
4017 if (buffer == 0) {
4018 bufObj = ctx->Shared->NullBufferObj;
4019 } else {
4020 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4021 }
4022 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
4023 &bufObj, "glBindBufferRange"))
4024 return;
4025
4026 if (no_error) {
4027 switch (target) {
4028 case GL_TRANSFORM_FEEDBACK_BUFFER:
4029 _mesa_bind_buffer_range_xfb(ctx, ctx->TransformFeedback.CurrentObject,
4030 index, bufObj, offset, size);
4031 return;
4032 case GL_UNIFORM_BUFFER:
4033 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
4034 return;
4035 case GL_SHADER_STORAGE_BUFFER:
4036 bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset,
4037 size);
4038 return;
4039 case GL_ATOMIC_COUNTER_BUFFER:
4040 bind_atomic_buffer(ctx, index, bufObj, offset, size);
4041 return;
4042 default:
4043 unreachable("invalid BindBufferRange target with KHR_no_error");
4044 }
4045 } else {
4046 if (!bufObj) {
4047 _mesa_error(ctx, GL_INVALID_OPERATION,
4048 "glBindBufferRange(invalid buffer=%u)", buffer);
4049 return;
4050 }
4051
4052 if (buffer != 0) {
4053 if (size <= 0) {
4054 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
4055 (int) size);
4056 return;
4057 }
4058 }
4059
4060 switch (target) {
4061 case GL_TRANSFORM_FEEDBACK_BUFFER:
4062 if (!_mesa_validate_buffer_range_xfb(ctx,
4063 ctx->TransformFeedback.CurrentObject,
4064 index, bufObj, offset, size,
4065 false))
4066 return;
4067
4068 _mesa_bind_buffer_range_xfb(ctx, ctx->TransformFeedback.CurrentObject,
4069 index, bufObj, offset, size);
4070 return;
4071 case GL_UNIFORM_BUFFER:
4072 bind_buffer_range_uniform_buffer_err(ctx, index, bufObj, offset,
4073 size);
4074 return;
4075 case GL_SHADER_STORAGE_BUFFER:
4076 bind_buffer_range_shader_storage_buffer_err(ctx, index, bufObj,
4077 offset, size);
4078 return;
4079 case GL_ATOMIC_COUNTER_BUFFER:
4080 bind_atomic_buffer_err(ctx, index, bufObj, offset, size,
4081 "glBindBufferRange");
4082 return;
4083 default:
4084 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
4085 return;
4086 }
4087 }
4088 }
4089
4090 void GLAPIENTRY
4091 _mesa_BindBufferRange_no_error(GLenum target, GLuint index, GLuint buffer,
4092 GLintptr offset, GLsizeiptr size)
4093 {
4094 bind_buffer_range(target, index, buffer, offset, size, true);
4095 }
4096
4097 void GLAPIENTRY
4098 _mesa_BindBufferRange(GLenum target, GLuint index,
4099 GLuint buffer, GLintptr offset, GLsizeiptr size)
4100 {
4101 bind_buffer_range(target, index, buffer, offset, size, false);
4102 }
4103
4104 void GLAPIENTRY
4105 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
4106 {
4107 GET_CURRENT_CONTEXT(ctx);
4108 struct gl_buffer_object *bufObj;
4109
4110 if (MESA_VERBOSE & VERBOSE_API) {
4111 _mesa_debug(ctx, "glBindBufferBase(%s, %u, %u)\n",
4112 _mesa_enum_to_string(target), index, buffer);
4113 }
4114
4115 if (buffer == 0) {
4116 bufObj = ctx->Shared->NullBufferObj;
4117 } else {
4118 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4119 }
4120 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
4121 &bufObj, "glBindBufferBase"))
4122 return;
4123
4124 if (!bufObj) {
4125 _mesa_error(ctx, GL_INVALID_OPERATION,
4126 "glBindBufferBase(invalid buffer=%u)", buffer);
4127 return;
4128 }
4129
4130 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4131 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4132 *
4133 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4134 * zero and size equal to the size of buffer."
4135 *
4136 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4137 *
4138 * "If the parameter (starting offset or size) was not specified when the
4139 * buffer object was bound, zero is returned."
4140 *
4141 * What happens if the size of the buffer changes? Does the size of the
4142 * buffer at the moment glBindBufferBase was called still play a role, like
4143 * the first quote would imply, or is the size meaningless in the
4144 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4145 * core spec page 45 says:
4146 *
4147 * "It is equivalent to calling BindBufferRange with offset zero, while
4148 * size is determined by the size of the bound buffer at the time the
4149 * binding is used."
4150 *
4151 * My interpretation is that the GL 4.1 spec was a clarification of the
4152 * behavior, not a change. In particular, this choice will only make
4153 * rendering work in cases where it would have had undefined results.
4154 */
4155
4156 switch (target) {
4157 case GL_TRANSFORM_FEEDBACK_BUFFER:
4158 _mesa_bind_buffer_base_transform_feedback(ctx,
4159 ctx->TransformFeedback.CurrentObject,
4160 index, bufObj, false);
4161 return;
4162 case GL_UNIFORM_BUFFER:
4163 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
4164 return;
4165 case GL_SHADER_STORAGE_BUFFER:
4166 bind_buffer_base_shader_storage_buffer(ctx, index, bufObj);
4167 return;
4168 case GL_ATOMIC_COUNTER_BUFFER:
4169 bind_atomic_buffer_err(ctx, index, bufObj, 0, 0,
4170 "glBindBufferBase");
4171 return;
4172 default:
4173 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
4174 return;
4175 }
4176 }
4177
4178 void GLAPIENTRY
4179 _mesa_BindBuffersRange(GLenum target, GLuint first, GLsizei count,
4180 const GLuint *buffers,
4181 const GLintptr *offsets, const GLsizeiptr *sizes)
4182 {
4183 GET_CURRENT_CONTEXT(ctx);
4184
4185 if (MESA_VERBOSE & VERBOSE_API) {
4186 _mesa_debug(ctx, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4187 _mesa_enum_to_string(target), first, count,
4188 buffers, offsets, sizes);
4189 }
4190
4191 switch (target) {
4192 case GL_TRANSFORM_FEEDBACK_BUFFER:
4193 bind_xfb_buffers(ctx, first, count, buffers, true, offsets, sizes,
4194 "glBindBuffersRange");
4195 return;
4196 case GL_UNIFORM_BUFFER:
4197 bind_uniform_buffers(ctx, first, count, buffers, true, offsets, sizes,
4198 "glBindBuffersRange");
4199 return;
4200 case GL_SHADER_STORAGE_BUFFER:
4201 bind_shader_storage_buffers(ctx, first, count, buffers, true, offsets, sizes,
4202 "glBindBuffersRange");
4203 return;
4204 case GL_ATOMIC_COUNTER_BUFFER:
4205 bind_atomic_buffers(ctx, first, count, buffers, true, offsets, sizes,
4206 "glBindBuffersRange");
4207 return;
4208 default:
4209 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersRange(target=%s)",
4210 _mesa_enum_to_string(target));
4211 break;
4212 }
4213 }
4214
4215 void GLAPIENTRY
4216 _mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count,
4217 const GLuint *buffers)
4218 {
4219 GET_CURRENT_CONTEXT(ctx);
4220
4221 if (MESA_VERBOSE & VERBOSE_API) {
4222 _mesa_debug(ctx, "glBindBuffersBase(%s, %u, %d, %p)\n",
4223 _mesa_enum_to_string(target), first, count, buffers);
4224 }
4225
4226 switch (target) {
4227 case GL_TRANSFORM_FEEDBACK_BUFFER:
4228 bind_xfb_buffers(ctx, first, count, buffers, false, NULL, NULL,
4229 "glBindBuffersBase");
4230 return;
4231 case GL_UNIFORM_BUFFER:
4232 bind_uniform_buffers(ctx, first, count, buffers, false, NULL, NULL,
4233 "glBindBuffersBase");
4234 return;
4235 case GL_SHADER_STORAGE_BUFFER:
4236 bind_shader_storage_buffers(ctx, first, count, buffers, false, NULL, NULL,
4237 "glBindBuffersBase");
4238 return;
4239 case GL_ATOMIC_COUNTER_BUFFER:
4240 bind_atomic_buffers(ctx, first, count, buffers, false, NULL, NULL,
4241 "glBindBuffersBase");
4242 return;
4243 default:
4244 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersBase(target=%s)",
4245 _mesa_enum_to_string(target));
4246 break;
4247 }
4248 }
4249
4250 static ALWAYS_INLINE void
4251 invalidate_buffer_subdata(struct gl_context *ctx,
4252 struct gl_buffer_object *bufObj, GLintptr offset,
4253 GLsizeiptr length)
4254 {
4255 if (ctx->Driver.InvalidateBufferSubData)
4256 ctx->Driver.InvalidateBufferSubData(ctx, bufObj, offset, length);
4257 }
4258
4259 void GLAPIENTRY
4260 _mesa_InvalidateBufferSubData_no_error(GLuint buffer, GLintptr offset,
4261 GLsizeiptr length)
4262 {
4263 GET_CURRENT_CONTEXT(ctx);
4264
4265 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4266 invalidate_buffer_subdata(ctx, bufObj, offset, length);
4267 }
4268
4269 void GLAPIENTRY
4270 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
4271 GLsizeiptr length)
4272 {
4273 GET_CURRENT_CONTEXT(ctx);
4274 struct gl_buffer_object *bufObj;
4275 const GLintptr end = offset + length;
4276
4277 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4278 * Profile) spec says:
4279 *
4280 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4281 * name of an existing buffer object."
4282 */
4283 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4284 if (!bufObj || bufObj == &DummyBufferObject) {
4285 _mesa_error(ctx, GL_INVALID_VALUE,
4286 "glInvalidateBufferSubData(name = %u) invalid object",
4287 buffer);
4288 return;
4289 }
4290
4291 /* The GL_ARB_invalidate_subdata spec says:
4292 *
4293 * "An INVALID_VALUE error is generated if <offset> or <length> is
4294 * negative, or if <offset> + <length> is greater than the value of
4295 * BUFFER_SIZE."
4296 */
4297 if (offset < 0 || length < 0 || end > bufObj->Size) {
4298 _mesa_error(ctx, GL_INVALID_VALUE,
4299 "glInvalidateBufferSubData(invalid offset or length)");
4300 return;
4301 }
4302
4303 /* The OpenGL 4.4 (Core Profile) spec says:
4304 *
4305 * "An INVALID_OPERATION error is generated if buffer is currently
4306 * mapped by MapBuffer or if the invalidate range intersects the range
4307 * currently mapped by MapBufferRange, unless it was mapped
4308 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4309 */
4310 if (!(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) &&
4311 bufferobj_range_mapped(bufObj, offset, length)) {
4312 _mesa_error(ctx, GL_INVALID_OPERATION,
4313 "glInvalidateBufferSubData(intersection with mapped "
4314 "range)");
4315 return;
4316 }
4317
4318 invalidate_buffer_subdata(ctx, bufObj, offset, length);
4319 }
4320
4321 void GLAPIENTRY
4322 _mesa_InvalidateBufferData_no_error(GLuint buffer)
4323 {
4324 GET_CURRENT_CONTEXT(ctx);
4325
4326 struct gl_buffer_object *bufObj =_mesa_lookup_bufferobj(ctx, buffer);
4327 invalidate_buffer_subdata(ctx, bufObj, 0, bufObj->Size);
4328 }
4329
4330 void GLAPIENTRY
4331 _mesa_InvalidateBufferData(GLuint buffer)
4332 {
4333 GET_CURRENT_CONTEXT(ctx);
4334 struct gl_buffer_object *bufObj;
4335
4336 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4337 * Profile) spec says:
4338 *
4339 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4340 * name of an existing buffer object."
4341 */
4342 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4343 if (!bufObj || bufObj == &DummyBufferObject) {
4344 _mesa_error(ctx, GL_INVALID_VALUE,
4345 "glInvalidateBufferData(name = %u) invalid object",
4346 buffer);
4347 return;
4348 }
4349
4350 /* The OpenGL 4.4 (Core Profile) spec says:
4351 *
4352 * "An INVALID_OPERATION error is generated if buffer is currently
4353 * mapped by MapBuffer or if the invalidate range intersects the range
4354 * currently mapped by MapBufferRange, unless it was mapped
4355 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4356 */
4357 if (_mesa_check_disallowed_mapping(bufObj)) {
4358 _mesa_error(ctx, GL_INVALID_OPERATION,
4359 "glInvalidateBufferData(intersection with mapped "
4360 "range)");
4361 return;
4362 }
4363
4364 invalidate_buffer_subdata(ctx, bufObj, 0, bufObj->Size);
4365 }
4366
4367 static void
4368 buffer_page_commitment(struct gl_context *ctx,
4369 struct gl_buffer_object *bufferObj,
4370 GLintptr offset, GLsizeiptr size,
4371 GLboolean commit, const char *func)
4372 {
4373 if (!(bufferObj->StorageFlags & GL_SPARSE_STORAGE_BIT_ARB)) {
4374 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(not a sparse buffer object)",
4375 func);
4376 return;
4377 }
4378
4379 if (size < 0 || size > bufferObj->Size ||
4380 offset < 0 || offset > bufferObj->Size - size) {
4381 _mesa_error(ctx, GL_INVALID_VALUE, "%s(out of bounds)",
4382 func);
4383 return;
4384 }
4385
4386 /* The GL_ARB_sparse_buffer extension specification says:
4387 *
4388 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4389 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4390 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4391 * not extend to the end of the buffer's data store."
4392 */
4393 if (offset % ctx->Const.SparseBufferPageSize != 0) {
4394 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset not aligned to page size)",
4395 func);
4396 return;
4397 }
4398
4399 if (size % ctx->Const.SparseBufferPageSize != 0 &&
4400 offset + size != bufferObj->Size) {
4401 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size not aligned to page size)",
4402 func);
4403 return;
4404 }
4405
4406 ctx->Driver.BufferPageCommitment(ctx, bufferObj, offset, size, commit);
4407 }
4408
4409 void GLAPIENTRY
4410 _mesa_BufferPageCommitmentARB(GLenum target, GLintptr offset, GLsizeiptr size,
4411 GLboolean commit)
4412 {
4413 GET_CURRENT_CONTEXT(ctx);
4414 struct gl_buffer_object *bufferObj;
4415
4416 bufferObj = get_buffer(ctx, "glBufferPageCommitmentARB", target,
4417 GL_INVALID_ENUM);
4418 if (!bufferObj)
4419 return;
4420
4421 buffer_page_commitment(ctx, bufferObj, offset, size, commit,
4422 "glBufferPageCommitmentARB");
4423 }
4424
4425 void GLAPIENTRY
4426 _mesa_NamedBufferPageCommitmentARB(GLuint buffer, GLintptr offset,
4427 GLsizeiptr size, GLboolean commit)
4428 {
4429 GET_CURRENT_CONTEXT(ctx);
4430 struct gl_buffer_object *bufferObj;
4431
4432 bufferObj = _mesa_lookup_bufferobj(ctx, buffer);
4433 if (!bufferObj || bufferObj == &DummyBufferObject) {
4434 /* Note: the extension spec is not clear about the excpected error value. */
4435 _mesa_error(ctx, GL_INVALID_VALUE,
4436 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4437 buffer);
4438 return;
4439 }
4440
4441 buffer_page_commitment(ctx, bufferObj, offset, size, commit,
4442 "glNamedBufferPageCommitmentARB");
4443 }