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