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