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