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