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