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