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