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