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