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