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