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