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