mesa: Fix some signed-unsigned comparison warnings
[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 default:
121 return NULL;
122 }
123 return NULL;
124 }
125
126
127 /**
128 * Get the buffer object bound to the specified target in a GL context.
129 * \param ctx the GL context
130 * \param target the buffer object target to be retrieved.
131 * \param error the GL error to record if target is illegal.
132 * \return pointer to the buffer object bound to \c target in the
133 * specified context or \c NULL if \c target is invalid.
134 */
135 static inline struct gl_buffer_object *
136 get_buffer(struct gl_context *ctx, const char *func, GLenum target,
137 GLenum error)
138 {
139 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
140
141 if (!bufObj) {
142 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
143 return NULL;
144 }
145
146 if (!_mesa_is_bufferobj(*bufObj)) {
147 _mesa_error(ctx, error, "%s(no buffer bound)", func);
148 return NULL;
149 }
150
151 return *bufObj;
152 }
153
154
155 /**
156 * Convert a GLbitfield describing the mapped buffer access flags
157 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
158 */
159 static GLenum
160 simplified_access_mode(struct gl_context *ctx, GLbitfield access)
161 {
162 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
163 if ((access & rwFlags) == rwFlags)
164 return GL_READ_WRITE;
165 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
166 return GL_READ_ONLY;
167 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
168 return GL_WRITE_ONLY;
169
170 /* Otherwise, AccessFlags is zero (the default state).
171 *
172 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
173 *
174 * Name Type Initial Value Legal Values
175 * ... ... ... ...
176 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
177 * READ_WRITE
178 *
179 * However, table 6.8 in the GL_OES_mapbuffer extension says:
180 *
181 * Get Value Type Get Command Value Description
182 * --------- ---- ----------- ----- -----------
183 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
184 *
185 * The difference is because GL_OES_mapbuffer only supports mapping buffers
186 * write-only.
187 */
188 assert(access == 0);
189
190 return _mesa_is_gles(ctx) ? GL_WRITE_ONLY : GL_READ_WRITE;
191 }
192
193
194 /**
195 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
196 * given range.
197 * The regions do not overlap if and only if the end of the given
198 * region is before the mapped region or the start of the given region
199 * is after the mapped region.
200 *
201 * \param obj Buffer object target on which to operate.
202 * \param offset Offset of the first byte of the subdata range.
203 * \param size Size, in bytes, of the subdata range.
204 * \return true if ranges overlap, false otherwise
205 *
206 */
207 static bool
208 bufferobj_range_mapped(const struct gl_buffer_object *obj,
209 GLintptr offset, GLsizeiptr size)
210 {
211 if (_mesa_bufferobj_mapped(obj, MAP_USER)) {
212 const GLintptr end = offset + size;
213 const GLintptr mapEnd = obj->Mappings[MAP_USER].Offset +
214 obj->Mappings[MAP_USER].Length;
215
216 if (!(end <= obj->Mappings[MAP_USER].Offset || offset >= mapEnd)) {
217 return true;
218 }
219 }
220 return false;
221 }
222
223
224 /**
225 * Tests the subdata range parameters and sets the GL error code for
226 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
227 * \c glClearBufferSubData.
228 *
229 * \param ctx GL context.
230 * \param target Buffer object target on which to operate.
231 * \param offset Offset of the first byte of the subdata range.
232 * \param size Size, in bytes, of the subdata range.
233 * \param mappedRange If true, checks if an overlapping range is mapped.
234 * If false, checks if buffer is mapped.
235 * \param errorNoBuffer Error code if no buffer is bound to target.
236 * \param caller Name of calling function for recording errors.
237 * \return A pointer to the buffer object bound to \c target in the
238 * specified context or \c NULL if any of the parameter or state
239 * conditions are invalid.
240 *
241 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
242 */
243 static struct gl_buffer_object *
244 buffer_object_subdata_range_good(struct gl_context * ctx, GLenum target,
245 GLintptrARB offset, GLsizeiptrARB size,
246 bool mappedRange, GLenum errorNoBuffer,
247 const char *caller)
248 {
249 struct gl_buffer_object *bufObj;
250
251 if (size < 0) {
252 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
253 return NULL;
254 }
255
256 if (offset < 0) {
257 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
258 return NULL;
259 }
260
261 bufObj = get_buffer(ctx, caller, target, errorNoBuffer);
262 if (!bufObj)
263 return NULL;
264
265 if (offset + size > bufObj->Size) {
266 _mesa_error(ctx, GL_INVALID_VALUE,
267 "%s(offset %lu + size %lu > buffer size %lu)", caller,
268 (unsigned long) offset,
269 (unsigned long) size,
270 (unsigned long) bufObj->Size);
271 return NULL;
272 }
273
274 if (bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT)
275 return bufObj;
276
277 if (mappedRange) {
278 if (bufferobj_range_mapped(bufObj, offset, size)) {
279 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
280 return NULL;
281 }
282 }
283 else {
284 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
285 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
286 return NULL;
287 }
288 }
289
290 return bufObj;
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 _mesa_buffer_data( struct gl_context *ctx, GLenum target, GLsizeiptrARB 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 _mesa_buffer_subdata( struct gl_context *ctx, GLintptrARB offset,
606 GLsizeiptrARB 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_buffer_clear_subdata(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 _mesa_buffer_map_range( 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 _mesa_buffer_flush_mapped_range( 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::MapBuffer().
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 _mesa_buffer_unmap(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 _mesa_copy_buffer_subdata(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 void
1006 _mesa_begin_bufferobj_lookups(struct gl_context *ctx)
1007 {
1008 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
1009 }
1010
1011
1012 void
1013 _mesa_end_bufferobj_lookups(struct gl_context *ctx)
1014 {
1015 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
1016 }
1017
1018
1019 /**
1020 * Look up a buffer object for a multi-bind function.
1021 *
1022 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1023 * of generating an error if the buffer ID is not zero or the name
1024 * of an existing buffer object.
1025 *
1026 * If the buffer ID refers to an existing buffer object, a pointer
1027 * to the buffer object is returned. If the ID is zero, a pointer
1028 * to the shared NullBufferObj is returned. If the ID is not zero
1029 * and does not refer to a valid buffer object, this function
1030 * returns NULL.
1031 *
1032 * This function assumes that the caller has already locked the
1033 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1034 */
1035 struct gl_buffer_object *
1036 _mesa_multi_bind_lookup_bufferobj(struct gl_context *ctx,
1037 const GLuint *buffers,
1038 GLuint index, const char *caller)
1039 {
1040 struct gl_buffer_object *bufObj;
1041
1042 if (buffers[index] != 0) {
1043 bufObj = _mesa_lookup_bufferobj_locked(ctx, buffers[index]);
1044
1045 /* The multi-bind functions don't create the buffer objects
1046 when they don't exist. */
1047 if (bufObj == &DummyBufferObject)
1048 bufObj = NULL;
1049 } else
1050 bufObj = ctx->Shared->NullBufferObj;
1051
1052 if (!bufObj) {
1053 /* The ARB_multi_bind spec says:
1054 *
1055 * "An INVALID_OPERATION error is generated if any value
1056 * in <buffers> is not zero or the name of an existing
1057 * buffer object (per binding)."
1058 */
1059 _mesa_error(ctx, GL_INVALID_OPERATION,
1060 "%s(buffers[%u]=%u is not zero or the name "
1061 "of an existing buffer object)",
1062 caller, index, buffers[index]);
1063 }
1064
1065 return bufObj;
1066 }
1067
1068
1069 /**
1070 * If *ptr points to obj, set ptr = the Null/default buffer object.
1071 * This is a helper for buffer object deletion.
1072 * The GL spec says that deleting a buffer object causes it to get
1073 * unbound from all arrays in the current context.
1074 */
1075 static void
1076 unbind(struct gl_context *ctx,
1077 struct gl_buffer_object **ptr,
1078 struct gl_buffer_object *obj)
1079 {
1080 if (*ptr == obj) {
1081 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
1082 }
1083 }
1084
1085
1086 /**
1087 * Plug default/fallback buffer object functions into the device
1088 * driver hooks.
1089 */
1090 void
1091 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
1092 {
1093 /* GL_ARB_vertex/pixel_buffer_object */
1094 driver->NewBufferObject = _mesa_new_buffer_object;
1095 driver->DeleteBuffer = _mesa_delete_buffer_object;
1096 driver->BufferData = _mesa_buffer_data;
1097 driver->BufferSubData = _mesa_buffer_subdata;
1098 driver->GetBufferSubData = _mesa_buffer_get_subdata;
1099 driver->UnmapBuffer = _mesa_buffer_unmap;
1100
1101 /* GL_ARB_clear_buffer_object */
1102 driver->ClearBufferSubData = _mesa_buffer_clear_subdata;
1103
1104 /* GL_ARB_map_buffer_range */
1105 driver->MapBufferRange = _mesa_buffer_map_range;
1106 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
1107
1108 /* GL_ARB_copy_buffer */
1109 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
1110 }
1111
1112
1113 void
1114 _mesa_buffer_unmap_all_mappings(struct gl_context *ctx,
1115 struct gl_buffer_object *bufObj)
1116 {
1117 int i;
1118
1119 for (i = 0; i < MAP_COUNT; i++) {
1120 if (_mesa_bufferobj_mapped(bufObj, i)) {
1121 ctx->Driver.UnmapBuffer(ctx, bufObj, i);
1122 ASSERT(bufObj->Mappings[i].Pointer == NULL);
1123 bufObj->Mappings[i].AccessFlags = 0;
1124 }
1125 }
1126 }
1127
1128
1129 /**********************************************************************/
1130 /* API Functions */
1131 /**********************************************************************/
1132
1133 void GLAPIENTRY
1134 _mesa_BindBuffer(GLenum target, GLuint buffer)
1135 {
1136 GET_CURRENT_CONTEXT(ctx);
1137
1138 if (MESA_VERBOSE & VERBOSE_API)
1139 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
1140 _mesa_lookup_enum_by_nr(target), buffer);
1141
1142 bind_buffer_object(ctx, target, buffer);
1143 }
1144
1145
1146 /**
1147 * Delete a set of buffer objects.
1148 *
1149 * \param n Number of buffer objects to delete.
1150 * \param ids Array of \c n buffer object IDs.
1151 */
1152 void GLAPIENTRY
1153 _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
1154 {
1155 GET_CURRENT_CONTEXT(ctx);
1156 GLsizei i;
1157 FLUSH_VERTICES(ctx, 0);
1158
1159 if (n < 0) {
1160 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
1161 return;
1162 }
1163
1164 mtx_lock(&ctx->Shared->Mutex);
1165
1166 for (i = 0; i < n; i++) {
1167 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
1168 if (bufObj) {
1169 struct gl_vertex_array_object *vao = ctx->Array.VAO;
1170 GLuint j;
1171
1172 ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
1173
1174 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1175
1176 /* unbind any vertex pointers bound to this buffer */
1177 for (j = 0; j < Elements(vao->VertexBinding); j++) {
1178 unbind(ctx, &vao->VertexBinding[j].BufferObj, bufObj);
1179 }
1180
1181 if (ctx->Array.ArrayBufferObj == bufObj) {
1182 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
1183 }
1184 if (vao->IndexBufferObj == bufObj) {
1185 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
1186 }
1187
1188 /* unbind ARB_draw_indirect binding point */
1189 if (ctx->DrawIndirectBuffer == bufObj) {
1190 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER, 0 );
1191 }
1192
1193 /* unbind ARB_copy_buffer binding points */
1194 if (ctx->CopyReadBuffer == bufObj) {
1195 _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
1196 }
1197 if (ctx->CopyWriteBuffer == bufObj) {
1198 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
1199 }
1200
1201 /* unbind transform feedback binding points */
1202 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
1203 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
1204 }
1205 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
1206 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
1207 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
1208 }
1209 }
1210
1211 /* unbind UBO binding points */
1212 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
1213 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
1214 _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
1215 }
1216 }
1217
1218 if (ctx->UniformBuffer == bufObj) {
1219 _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
1220 }
1221
1222 /* unbind Atomci Buffer binding points */
1223 for (j = 0; j < ctx->Const.MaxAtomicBufferBindings; j++) {
1224 if (ctx->AtomicBufferBindings[j].BufferObject == bufObj) {
1225 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER, j, 0 );
1226 }
1227 }
1228
1229 if (ctx->UniformBuffer == bufObj) {
1230 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER, 0 );
1231 }
1232
1233 /* unbind any pixel pack/unpack pointers bound to this buffer */
1234 if (ctx->Pack.BufferObj == bufObj) {
1235 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
1236 }
1237 if (ctx->Unpack.BufferObj == bufObj) {
1238 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
1239 }
1240
1241 if (ctx->Texture.BufferObject == bufObj) {
1242 _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
1243 }
1244
1245 /* The ID is immediately freed for re-use */
1246 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
1247 /* Make sure we do not run into the classic ABA problem on bind.
1248 * We don't want to allow re-binding a buffer object that's been
1249 * "deleted" by glDeleteBuffers().
1250 *
1251 * The explicit rebinding to the default object in the current context
1252 * prevents the above in the current context, but another context
1253 * sharing the same objects might suffer from this problem.
1254 * The alternative would be to do the hash lookup in any case on bind
1255 * which would introduce more runtime overhead than this.
1256 */
1257 bufObj->DeletePending = GL_TRUE;
1258 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
1259 }
1260 }
1261
1262 mtx_unlock(&ctx->Shared->Mutex);
1263 }
1264
1265
1266 /**
1267 * Generate a set of unique buffer object IDs and store them in \c buffer.
1268 *
1269 * \param n Number of IDs to generate.
1270 * \param buffer Array of \c n locations to store the IDs.
1271 */
1272 void GLAPIENTRY
1273 _mesa_GenBuffers(GLsizei n, GLuint *buffer)
1274 {
1275 GET_CURRENT_CONTEXT(ctx);
1276 GLuint first;
1277 GLint i;
1278
1279 if (MESA_VERBOSE & VERBOSE_API)
1280 _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
1281
1282 if (n < 0) {
1283 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
1284 return;
1285 }
1286
1287 if (!buffer) {
1288 return;
1289 }
1290
1291 /*
1292 * This must be atomic (generation and allocation of buffer object IDs)
1293 */
1294 mtx_lock(&ctx->Shared->Mutex);
1295
1296 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
1297
1298 /* Insert the ID and pointer to dummy buffer object into hash table */
1299 for (i = 0; i < n; i++) {
1300 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
1301 &DummyBufferObject);
1302 buffer[i] = first + i;
1303 }
1304
1305 mtx_unlock(&ctx->Shared->Mutex);
1306 }
1307
1308
1309 /**
1310 * Determine if ID is the name of a buffer object.
1311 *
1312 * \param id ID of the potential buffer object.
1313 * \return \c GL_TRUE if \c id is the name of a buffer object,
1314 * \c GL_FALSE otherwise.
1315 */
1316 GLboolean GLAPIENTRY
1317 _mesa_IsBuffer(GLuint id)
1318 {
1319 struct gl_buffer_object *bufObj;
1320 GET_CURRENT_CONTEXT(ctx);
1321 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1322
1323 mtx_lock(&ctx->Shared->Mutex);
1324 bufObj = _mesa_lookup_bufferobj(ctx, id);
1325 mtx_unlock(&ctx->Shared->Mutex);
1326
1327 return bufObj && bufObj != &DummyBufferObject;
1328 }
1329
1330
1331 void GLAPIENTRY
1332 _mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data,
1333 GLbitfield flags)
1334 {
1335 GET_CURRENT_CONTEXT(ctx);
1336 struct gl_buffer_object *bufObj;
1337
1338 if (size <= 0) {
1339 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(size <= 0)");
1340 return;
1341 }
1342
1343 if (flags & ~(GL_MAP_READ_BIT |
1344 GL_MAP_WRITE_BIT |
1345 GL_MAP_PERSISTENT_BIT |
1346 GL_MAP_COHERENT_BIT |
1347 GL_DYNAMIC_STORAGE_BIT |
1348 GL_CLIENT_STORAGE_BIT)) {
1349 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(flags)");
1350 return;
1351 }
1352
1353 if (flags & GL_MAP_PERSISTENT_BIT &&
1354 !(flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))) {
1355 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(flags!=READ/WRITE)");
1356 return;
1357 }
1358
1359 if (flags & GL_MAP_COHERENT_BIT && !(flags & GL_MAP_PERSISTENT_BIT)) {
1360 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(flags!=PERSISTENT)");
1361 return;
1362 }
1363
1364 bufObj = get_buffer(ctx, "glBufferStorage", target, GL_INVALID_OPERATION);
1365 if (!bufObj)
1366 return;
1367
1368 if (bufObj->Immutable) {
1369 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferStorage(immutable)");
1370 return;
1371 }
1372
1373 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1374 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1375
1376 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1377
1378 bufObj->Written = GL_TRUE;
1379 bufObj->Immutable = GL_TRUE;
1380
1381 ASSERT(ctx->Driver.BufferData);
1382 if (!ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
1383 flags, bufObj)) {
1384 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferStorage()");
1385 }
1386 }
1387
1388
1389 void GLAPIENTRY
1390 _mesa_BufferData(GLenum target, GLsizeiptrARB size,
1391 const GLvoid * data, GLenum usage)
1392 {
1393 GET_CURRENT_CONTEXT(ctx);
1394 struct gl_buffer_object *bufObj;
1395 bool valid_usage;
1396
1397 if (MESA_VERBOSE & VERBOSE_API)
1398 _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
1399 _mesa_lookup_enum_by_nr(target),
1400 (long int) size, data,
1401 _mesa_lookup_enum_by_nr(usage));
1402
1403 if (size < 0) {
1404 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
1405 return;
1406 }
1407
1408 switch (usage) {
1409 case GL_STREAM_DRAW_ARB:
1410 valid_usage = (ctx->API != API_OPENGLES);
1411 break;
1412
1413 case GL_STATIC_DRAW_ARB:
1414 case GL_DYNAMIC_DRAW_ARB:
1415 valid_usage = true;
1416 break;
1417
1418 case GL_STREAM_READ_ARB:
1419 case GL_STREAM_COPY_ARB:
1420 case GL_STATIC_READ_ARB:
1421 case GL_STATIC_COPY_ARB:
1422 case GL_DYNAMIC_READ_ARB:
1423 case GL_DYNAMIC_COPY_ARB:
1424 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1425 break;
1426
1427 default:
1428 valid_usage = false;
1429 break;
1430 }
1431
1432 if (!valid_usage) {
1433 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferData(usage)");
1434 return;
1435 }
1436
1437 bufObj = get_buffer(ctx, "glBufferDataARB", target, GL_INVALID_OPERATION);
1438 if (!bufObj)
1439 return;
1440
1441 if (bufObj->Immutable) {
1442 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferData(immutable)");
1443 return;
1444 }
1445
1446 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1447 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1448
1449 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1450
1451 bufObj->Written = GL_TRUE;
1452
1453 #ifdef VBO_DEBUG
1454 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1455 bufObj->Name, size, data, usage);
1456 #endif
1457
1458 #ifdef BOUNDS_CHECK
1459 size += 100;
1460 #endif
1461
1462 ASSERT(ctx->Driver.BufferData);
1463 if (!ctx->Driver.BufferData(ctx, target, size, data, usage,
1464 GL_MAP_READ_BIT |
1465 GL_MAP_WRITE_BIT |
1466 GL_DYNAMIC_STORAGE_BIT,
1467 bufObj)) {
1468 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
1469 }
1470 }
1471
1472
1473 void GLAPIENTRY
1474 _mesa_BufferSubData(GLenum target, GLintptrARB offset,
1475 GLsizeiptrARB size, const GLvoid * data)
1476 {
1477 GET_CURRENT_CONTEXT(ctx);
1478 struct gl_buffer_object *bufObj;
1479
1480 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1481 false, GL_INVALID_OPERATION,
1482 "glBufferSubDataARB" );
1483 if (!bufObj) {
1484 /* error already recorded */
1485 return;
1486 }
1487
1488 if (bufObj->Immutable &&
1489 !(bufObj->StorageFlags & GL_DYNAMIC_STORAGE_BIT)) {
1490 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferSubData");
1491 return;
1492 }
1493
1494 if (size == 0)
1495 return;
1496
1497 bufObj->Written = GL_TRUE;
1498
1499 ASSERT(ctx->Driver.BufferSubData);
1500 ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
1501 }
1502
1503
1504 void GLAPIENTRY
1505 _mesa_GetBufferSubData(GLenum target, GLintptrARB offset,
1506 GLsizeiptrARB size, void * data)
1507 {
1508 GET_CURRENT_CONTEXT(ctx);
1509 struct gl_buffer_object *bufObj;
1510
1511 bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
1512 false, GL_INVALID_OPERATION,
1513 "glGetBufferSubDataARB");
1514 if (!bufObj) {
1515 /* error already recorded */
1516 return;
1517 }
1518
1519 ASSERT(ctx->Driver.GetBufferSubData);
1520 ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
1521 }
1522
1523
1524 void GLAPIENTRY
1525 _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
1526 GLenum type, const GLvoid* data)
1527 {
1528 GET_CURRENT_CONTEXT(ctx);
1529 struct gl_buffer_object* bufObj;
1530 mesa_format mesaFormat;
1531 GLubyte clearValue[MAX_PIXEL_BYTES];
1532 GLsizeiptr clearValueSize;
1533
1534 bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
1535 if (!bufObj) {
1536 return;
1537 }
1538
1539 if (_mesa_check_disallowed_mapping(bufObj)) {
1540 _mesa_error(ctx, GL_INVALID_OPERATION,
1541 "glClearBufferData(buffer currently mapped)");
1542 return;
1543 }
1544
1545 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1546 format, type,
1547 "glClearBufferData");
1548 if (mesaFormat == MESA_FORMAT_NONE) {
1549 return;
1550 }
1551
1552 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1553 if (bufObj->Size % clearValueSize != 0) {
1554 _mesa_error(ctx, GL_INVALID_VALUE,
1555 "glClearBufferData(size is not a multiple of "
1556 "internalformat size)");
1557 return;
1558 }
1559
1560 if (data == NULL) {
1561 /* clear to zeros, per the spec */
1562 ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
1563 NULL, clearValueSize, bufObj);
1564 return;
1565 }
1566
1567 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1568 format, type, data, "glClearBufferData")) {
1569 return;
1570 }
1571
1572 ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
1573 clearValue, clearValueSize, bufObj);
1574 }
1575
1576
1577 void GLAPIENTRY
1578 _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
1579 GLintptr offset, GLsizeiptr size,
1580 GLenum format, GLenum type,
1581 const GLvoid* data)
1582 {
1583 GET_CURRENT_CONTEXT(ctx);
1584 struct gl_buffer_object* bufObj;
1585 mesa_format mesaFormat;
1586 GLubyte clearValue[MAX_PIXEL_BYTES];
1587 GLsizeiptr clearValueSize;
1588
1589 bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
1590 true, GL_INVALID_VALUE,
1591 "glClearBufferSubData");
1592 if (!bufObj) {
1593 return;
1594 }
1595
1596 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1597 format, type,
1598 "glClearBufferSubData");
1599 if (mesaFormat == MESA_FORMAT_NONE) {
1600 return;
1601 }
1602
1603 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1604 if (offset % clearValueSize != 0 || size % clearValueSize != 0) {
1605 _mesa_error(ctx, GL_INVALID_VALUE,
1606 "glClearBufferSubData(offset or size is not a multiple of "
1607 "internalformat size)");
1608 return;
1609 }
1610
1611 if (data == NULL) {
1612 /* clear to zeros, per the spec */
1613 if (size > 0) {
1614 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1615 NULL, clearValueSize, bufObj);
1616 }
1617 return;
1618 }
1619
1620 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1621 format, type, data,
1622 "glClearBufferSubData")) {
1623 return;
1624 }
1625
1626 if (size > 0) {
1627 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1628 clearValue, clearValueSize, bufObj);
1629 }
1630 }
1631
1632
1633 void * GLAPIENTRY
1634 _mesa_MapBuffer(GLenum target, GLenum access)
1635 {
1636 GET_CURRENT_CONTEXT(ctx);
1637 struct gl_buffer_object * bufObj;
1638 GLbitfield accessFlags;
1639 void *map;
1640 bool valid_access;
1641
1642 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1643
1644 switch (access) {
1645 case GL_READ_ONLY_ARB:
1646 accessFlags = GL_MAP_READ_BIT;
1647 valid_access = _mesa_is_desktop_gl(ctx);
1648 break;
1649 case GL_WRITE_ONLY_ARB:
1650 accessFlags = GL_MAP_WRITE_BIT;
1651 valid_access = true;
1652 break;
1653 case GL_READ_WRITE_ARB:
1654 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1655 valid_access = _mesa_is_desktop_gl(ctx);
1656 break;
1657 default:
1658 valid_access = false;
1659 break;
1660 }
1661
1662 if (!valid_access) {
1663 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1664 return NULL;
1665 }
1666
1667 bufObj = get_buffer(ctx, "glMapBufferARB", target, GL_INVALID_OPERATION);
1668 if (!bufObj)
1669 return NULL;
1670
1671 if (accessFlags & GL_MAP_READ_BIT &&
1672 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
1673 _mesa_error(ctx, GL_INVALID_OPERATION,
1674 "glMapBuffer(invalid read flag)");
1675 return NULL;
1676 }
1677
1678 if (accessFlags & GL_MAP_WRITE_BIT &&
1679 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
1680 _mesa_error(ctx, GL_INVALID_OPERATION,
1681 "glMapBuffer(invalid write flag)");
1682 return NULL;
1683 }
1684
1685 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
1686 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1687 return NULL;
1688 }
1689
1690 if (!bufObj->Size) {
1691 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1692 "glMapBuffer(buffer size = 0)");
1693 return NULL;
1694 }
1695
1696 ASSERT(ctx->Driver.MapBufferRange);
1697 map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj,
1698 MAP_USER);
1699 if (!map) {
1700 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1701 return NULL;
1702 }
1703 else {
1704 /* The driver callback should have set these fields.
1705 * This is important because other modules (like VBO) might call
1706 * the driver function directly.
1707 */
1708 ASSERT(bufObj->Mappings[MAP_USER].Pointer == map);
1709 ASSERT(bufObj->Mappings[MAP_USER].Length == bufObj->Size);
1710 ASSERT(bufObj->Mappings[MAP_USER].Offset == 0);
1711 bufObj->Mappings[MAP_USER].AccessFlags = accessFlags;
1712 }
1713
1714 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1715 bufObj->Written = GL_TRUE;
1716
1717 #ifdef VBO_DEBUG
1718 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1719 bufObj->Name, bufObj->Size, access);
1720 if (access == GL_WRITE_ONLY_ARB) {
1721 GLuint i;
1722 GLubyte *b = (GLubyte *) bufObj->Pointer;
1723 for (i = 0; i < bufObj->Size; i++)
1724 b[i] = i & 0xff;
1725 }
1726 #endif
1727
1728 #ifdef BOUNDS_CHECK
1729 {
1730 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1731 GLuint i;
1732 /* buffer is 100 bytes larger than requested, fill with magic value */
1733 for (i = 0; i < 100; i++) {
1734 buf[bufObj->Size - i - 1] = 123;
1735 }
1736 }
1737 #endif
1738
1739 return bufObj->Mappings[MAP_USER].Pointer;
1740 }
1741
1742
1743 GLboolean GLAPIENTRY
1744 _mesa_UnmapBuffer(GLenum target)
1745 {
1746 GET_CURRENT_CONTEXT(ctx);
1747 struct gl_buffer_object *bufObj;
1748 GLboolean status = GL_TRUE;
1749 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1750
1751 bufObj = get_buffer(ctx, "glUnmapBufferARB", target, GL_INVALID_OPERATION);
1752 if (!bufObj)
1753 return GL_FALSE;
1754
1755 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
1756 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1757 return GL_FALSE;
1758 }
1759
1760 #ifdef BOUNDS_CHECK
1761 if (bufObj->Access != GL_READ_ONLY_ARB) {
1762 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1763 GLuint i;
1764 /* check that last 100 bytes are still = magic value */
1765 for (i = 0; i < 100; i++) {
1766 GLuint pos = bufObj->Size - i - 1;
1767 if (buf[pos] != 123) {
1768 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1769 " at position %d (value = %u)\n",
1770 pos, buf[pos]);
1771 }
1772 }
1773 }
1774 #endif
1775
1776 #ifdef VBO_DEBUG
1777 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1778 GLuint i, unchanged = 0;
1779 GLubyte *b = (GLubyte *) bufObj->Pointer;
1780 GLint pos = -1;
1781 /* check which bytes changed */
1782 for (i = 0; i < bufObj->Size - 1; i++) {
1783 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1784 unchanged++;
1785 if (pos == -1)
1786 pos = i;
1787 }
1788 }
1789 if (unchanged) {
1790 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1791 bufObj->Name, unchanged, bufObj->Size, pos);
1792 }
1793 }
1794 #endif
1795
1796 status = ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_USER);
1797 bufObj->Mappings[MAP_USER].AccessFlags = 0;
1798 ASSERT(bufObj->Mappings[MAP_USER].Pointer == NULL);
1799 ASSERT(bufObj->Mappings[MAP_USER].Offset == 0);
1800 ASSERT(bufObj->Mappings[MAP_USER].Length == 0);
1801
1802 return status;
1803 }
1804
1805
1806 void GLAPIENTRY
1807 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
1808 {
1809 GET_CURRENT_CONTEXT(ctx);
1810 struct gl_buffer_object *bufObj;
1811
1812 bufObj = get_buffer(ctx, "glGetBufferParameterivARB", target,
1813 GL_INVALID_OPERATION);
1814 if (!bufObj)
1815 return;
1816
1817 switch (pname) {
1818 case GL_BUFFER_SIZE_ARB:
1819 *params = (GLint) bufObj->Size;
1820 return;
1821 case GL_BUFFER_USAGE_ARB:
1822 *params = bufObj->Usage;
1823 return;
1824 case GL_BUFFER_ACCESS_ARB:
1825 *params = simplified_access_mode(ctx,
1826 bufObj->Mappings[MAP_USER].AccessFlags);
1827 return;
1828 case GL_BUFFER_MAPPED_ARB:
1829 *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
1830 return;
1831 case GL_BUFFER_ACCESS_FLAGS:
1832 if (!ctx->Extensions.ARB_map_buffer_range)
1833 goto invalid_pname;
1834 *params = bufObj->Mappings[MAP_USER].AccessFlags;
1835 return;
1836 case GL_BUFFER_MAP_OFFSET:
1837 if (!ctx->Extensions.ARB_map_buffer_range)
1838 goto invalid_pname;
1839 *params = (GLint) bufObj->Mappings[MAP_USER].Offset;
1840 return;
1841 case GL_BUFFER_MAP_LENGTH:
1842 if (!ctx->Extensions.ARB_map_buffer_range)
1843 goto invalid_pname;
1844 *params = (GLint) bufObj->Mappings[MAP_USER].Length;
1845 return;
1846 case GL_BUFFER_IMMUTABLE_STORAGE:
1847 if (!ctx->Extensions.ARB_buffer_storage)
1848 goto invalid_pname;
1849 *params = bufObj->Immutable;
1850 return;
1851 case GL_BUFFER_STORAGE_FLAGS:
1852 if (!ctx->Extensions.ARB_buffer_storage)
1853 goto invalid_pname;
1854 *params = bufObj->StorageFlags;
1855 return;
1856 default:
1857 ; /* fall-through */
1858 }
1859
1860 invalid_pname:
1861 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1862 _mesa_lookup_enum_by_nr(pname));
1863 }
1864
1865
1866 /**
1867 * New in GL 3.2
1868 * This is pretty much a duplicate of GetBufferParameteriv() but the
1869 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1870 */
1871 void GLAPIENTRY
1872 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1873 {
1874 GET_CURRENT_CONTEXT(ctx);
1875 struct gl_buffer_object *bufObj;
1876
1877 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
1878 GL_INVALID_OPERATION);
1879 if (!bufObj)
1880 return;
1881
1882 switch (pname) {
1883 case GL_BUFFER_SIZE_ARB:
1884 *params = bufObj->Size;
1885 return;
1886 case GL_BUFFER_USAGE_ARB:
1887 *params = bufObj->Usage;
1888 return;
1889 case GL_BUFFER_ACCESS_ARB:
1890 *params = simplified_access_mode(ctx,
1891 bufObj->Mappings[MAP_USER].AccessFlags);
1892 return;
1893 case GL_BUFFER_ACCESS_FLAGS:
1894 if (!ctx->Extensions.ARB_map_buffer_range)
1895 goto invalid_pname;
1896 *params = bufObj->Mappings[MAP_USER].AccessFlags;
1897 return;
1898 case GL_BUFFER_MAPPED_ARB:
1899 *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
1900 return;
1901 case GL_BUFFER_MAP_OFFSET:
1902 if (!ctx->Extensions.ARB_map_buffer_range)
1903 goto invalid_pname;
1904 *params = bufObj->Mappings[MAP_USER].Offset;
1905 return;
1906 case GL_BUFFER_MAP_LENGTH:
1907 if (!ctx->Extensions.ARB_map_buffer_range)
1908 goto invalid_pname;
1909 *params = bufObj->Mappings[MAP_USER].Length;
1910 return;
1911 case GL_BUFFER_IMMUTABLE_STORAGE:
1912 if (!ctx->Extensions.ARB_buffer_storage)
1913 goto invalid_pname;
1914 *params = bufObj->Immutable;
1915 return;
1916 case GL_BUFFER_STORAGE_FLAGS:
1917 if (!ctx->Extensions.ARB_buffer_storage)
1918 goto invalid_pname;
1919 *params = bufObj->StorageFlags;
1920 return;
1921 default:
1922 ; /* fall-through */
1923 }
1924
1925 invalid_pname:
1926 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1927 _mesa_lookup_enum_by_nr(pname));
1928 }
1929
1930
1931 void GLAPIENTRY
1932 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
1933 {
1934 GET_CURRENT_CONTEXT(ctx);
1935 struct gl_buffer_object * bufObj;
1936
1937 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1938 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1939 return;
1940 }
1941
1942 bufObj = get_buffer(ctx, "glGetBufferPointervARB", target,
1943 GL_INVALID_OPERATION);
1944 if (!bufObj)
1945 return;
1946
1947 *params = bufObj->Mappings[MAP_USER].Pointer;
1948 }
1949
1950
1951 void GLAPIENTRY
1952 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1953 GLintptr readOffset, GLintptr writeOffset,
1954 GLsizeiptr size)
1955 {
1956 GET_CURRENT_CONTEXT(ctx);
1957 struct gl_buffer_object *src, *dst;
1958
1959 src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
1960 GL_INVALID_OPERATION);
1961 if (!src)
1962 return;
1963
1964 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
1965 GL_INVALID_OPERATION);
1966 if (!dst)
1967 return;
1968
1969 if (_mesa_check_disallowed_mapping(src)) {
1970 _mesa_error(ctx, GL_INVALID_OPERATION,
1971 "glCopyBufferSubData(readBuffer is mapped)");
1972 return;
1973 }
1974
1975 if (_mesa_check_disallowed_mapping(dst)) {
1976 _mesa_error(ctx, GL_INVALID_OPERATION,
1977 "glCopyBufferSubData(writeBuffer is mapped)");
1978 return;
1979 }
1980
1981 if (readOffset < 0) {
1982 _mesa_error(ctx, GL_INVALID_VALUE,
1983 "glCopyBufferSubData(readOffset = %d)", (int) readOffset);
1984 return;
1985 }
1986
1987 if (writeOffset < 0) {
1988 _mesa_error(ctx, GL_INVALID_VALUE,
1989 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset);
1990 return;
1991 }
1992
1993 if (size < 0) {
1994 _mesa_error(ctx, GL_INVALID_VALUE,
1995 "glCopyBufferSubData(writeOffset = %d)", (int) size);
1996 return;
1997 }
1998
1999 if (readOffset + size > src->Size) {
2000 _mesa_error(ctx, GL_INVALID_VALUE,
2001 "glCopyBufferSubData(readOffset + size = %d)",
2002 (int) (readOffset + size));
2003 return;
2004 }
2005
2006 if (writeOffset + size > dst->Size) {
2007 _mesa_error(ctx, GL_INVALID_VALUE,
2008 "glCopyBufferSubData(writeOffset + size = %d)",
2009 (int) (writeOffset + size));
2010 return;
2011 }
2012
2013 if (src == dst) {
2014 if (readOffset + size <= writeOffset) {
2015 /* OK */
2016 }
2017 else if (writeOffset + size <= readOffset) {
2018 /* OK */
2019 }
2020 else {
2021 /* overlapping src/dst is illegal */
2022 _mesa_error(ctx, GL_INVALID_VALUE,
2023 "glCopyBufferSubData(overlapping src/dst)");
2024 return;
2025 }
2026 }
2027
2028 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
2029 }
2030
2031
2032 /**
2033 * See GL_ARB_map_buffer_range spec
2034 */
2035 void * GLAPIENTRY
2036 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
2037 GLbitfield access)
2038 {
2039 GET_CURRENT_CONTEXT(ctx);
2040 struct gl_buffer_object *bufObj;
2041 void *map;
2042 GLbitfield allowed_access;
2043
2044 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
2045
2046 if (!ctx->Extensions.ARB_map_buffer_range) {
2047 _mesa_error(ctx, GL_INVALID_OPERATION,
2048 "glMapBufferRange(extension not supported)");
2049 return NULL;
2050 }
2051
2052 if (offset < 0) {
2053 _mesa_error(ctx, GL_INVALID_VALUE,
2054 "glMapBufferRange(offset = %ld)", (long)offset);
2055 return NULL;
2056 }
2057
2058 if (length < 0) {
2059 _mesa_error(ctx, GL_INVALID_VALUE,
2060 "glMapBufferRange(length = %ld)", (long)length);
2061 return NULL;
2062 }
2063
2064 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2065 *
2066 * "An INVALID_OPERATION error is generated for any of the following
2067 * conditions:
2068 *
2069 * * <length> is zero."
2070 */
2071 if (_mesa_is_gles(ctx) && length == 0) {
2072 _mesa_error(ctx, GL_INVALID_OPERATION,
2073 "glMapBufferRange(length = 0)");
2074 return NULL;
2075 }
2076
2077 allowed_access = GL_MAP_READ_BIT |
2078 GL_MAP_WRITE_BIT |
2079 GL_MAP_INVALIDATE_RANGE_BIT |
2080 GL_MAP_INVALIDATE_BUFFER_BIT |
2081 GL_MAP_FLUSH_EXPLICIT_BIT |
2082 GL_MAP_UNSYNCHRONIZED_BIT;
2083
2084 if (ctx->Extensions.ARB_buffer_storage) {
2085 allowed_access |= GL_MAP_PERSISTENT_BIT |
2086 GL_MAP_COHERENT_BIT;
2087 }
2088
2089 if (access & ~allowed_access) {
2090 /* generate an error if any other than allowed bit is set */
2091 _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
2092 return NULL;
2093 }
2094
2095 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
2096 _mesa_error(ctx, GL_INVALID_OPERATION,
2097 "glMapBufferRange(access indicates neither read or write)");
2098 return NULL;
2099 }
2100
2101 if ((access & GL_MAP_READ_BIT) &&
2102 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
2103 GL_MAP_INVALIDATE_BUFFER_BIT |
2104 GL_MAP_UNSYNCHRONIZED_BIT))) {
2105 _mesa_error(ctx, GL_INVALID_OPERATION,
2106 "glMapBufferRange(invalid access flags)");
2107 return NULL;
2108 }
2109
2110 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
2111 ((access & GL_MAP_WRITE_BIT) == 0)) {
2112 _mesa_error(ctx, GL_INVALID_OPERATION,
2113 "glMapBufferRange(invalid access flags)");
2114 return NULL;
2115 }
2116
2117 bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
2118 if (!bufObj)
2119 return NULL;
2120
2121 if (access & GL_MAP_READ_BIT &&
2122 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
2123 _mesa_error(ctx, GL_INVALID_OPERATION,
2124 "glMapBufferRange(invalid read flag)");
2125 return NULL;
2126 }
2127
2128 if (access & GL_MAP_WRITE_BIT &&
2129 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
2130 _mesa_error(ctx, GL_INVALID_OPERATION,
2131 "glMapBufferRange(invalid write flag)");
2132 return NULL;
2133 }
2134
2135 if (access & GL_MAP_COHERENT_BIT &&
2136 !(bufObj->StorageFlags & GL_MAP_COHERENT_BIT)) {
2137 _mesa_error(ctx, GL_INVALID_OPERATION,
2138 "glMapBufferRange(invalid coherent flag)");
2139 return NULL;
2140 }
2141
2142 if (access & GL_MAP_PERSISTENT_BIT &&
2143 !(bufObj->StorageFlags & GL_MAP_PERSISTENT_BIT)) {
2144 _mesa_error(ctx, GL_INVALID_OPERATION,
2145 "glMapBufferRange(invalid persistent flag)");
2146 return NULL;
2147 }
2148
2149 if (offset + length > bufObj->Size) {
2150 _mesa_error(ctx, GL_INVALID_VALUE,
2151 "glMapBufferRange(offset + length > size)");
2152 return NULL;
2153 }
2154
2155 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2156 _mesa_error(ctx, GL_INVALID_OPERATION,
2157 "glMapBufferRange(buffer already mapped)");
2158 return NULL;
2159 }
2160
2161 if (!bufObj->Size) {
2162 _mesa_error(ctx, GL_OUT_OF_MEMORY,
2163 "glMapBufferRange(buffer size = 0)");
2164 return NULL;
2165 }
2166
2167 /* Mapping zero bytes should return a non-null pointer. */
2168 if (!length) {
2169 static long dummy = 0;
2170 bufObj->Mappings[MAP_USER].Pointer = &dummy;
2171 bufObj->Mappings[MAP_USER].Length = length;
2172 bufObj->Mappings[MAP_USER].Offset = offset;
2173 bufObj->Mappings[MAP_USER].AccessFlags = access;
2174 return bufObj->Mappings[MAP_USER].Pointer;
2175 }
2176
2177 ASSERT(ctx->Driver.MapBufferRange);
2178 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj,
2179 MAP_USER);
2180 if (!map) {
2181 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
2182 }
2183 else {
2184 /* The driver callback should have set all these fields.
2185 * This is important because other modules (like VBO) might call
2186 * the driver function directly.
2187 */
2188 ASSERT(bufObj->Mappings[MAP_USER].Pointer == map);
2189 ASSERT(bufObj->Mappings[MAP_USER].Length == length);
2190 ASSERT(bufObj->Mappings[MAP_USER].Offset == offset);
2191 ASSERT(bufObj->Mappings[MAP_USER].AccessFlags == access);
2192 }
2193
2194 return map;
2195 }
2196
2197
2198 /**
2199 * See GL_ARB_map_buffer_range spec
2200 */
2201 void GLAPIENTRY
2202 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
2203 {
2204 GET_CURRENT_CONTEXT(ctx);
2205 struct gl_buffer_object *bufObj;
2206
2207 if (!ctx->Extensions.ARB_map_buffer_range) {
2208 _mesa_error(ctx, GL_INVALID_OPERATION,
2209 "glFlushMappedBufferRange(extension not supported)");
2210 return;
2211 }
2212
2213 if (offset < 0) {
2214 _mesa_error(ctx, GL_INVALID_VALUE,
2215 "glFlushMappedBufferRange(offset = %ld)", (long)offset);
2216 return;
2217 }
2218
2219 if (length < 0) {
2220 _mesa_error(ctx, GL_INVALID_VALUE,
2221 "glFlushMappedBufferRange(length = %ld)", (long)length);
2222 return;
2223 }
2224
2225 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
2226 GL_INVALID_OPERATION);
2227 if (!bufObj)
2228 return;
2229
2230 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2231 /* buffer is not mapped */
2232 _mesa_error(ctx, GL_INVALID_OPERATION,
2233 "glFlushMappedBufferRange(buffer is not mapped)");
2234 return;
2235 }
2236
2237 if ((bufObj->Mappings[MAP_USER].AccessFlags &
2238 GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
2239 _mesa_error(ctx, GL_INVALID_OPERATION,
2240 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
2241 return;
2242 }
2243
2244 if (offset + length > bufObj->Mappings[MAP_USER].Length) {
2245 _mesa_error(ctx, GL_INVALID_VALUE,
2246 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
2247 (long)offset, (long)length,
2248 (long)bufObj->Mappings[MAP_USER].Length);
2249 return;
2250 }
2251
2252 ASSERT(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_WRITE_BIT);
2253
2254 if (ctx->Driver.FlushMappedBufferRange)
2255 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
2256 MAP_USER);
2257 }
2258
2259
2260 static GLenum
2261 buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2262 {
2263 struct gl_buffer_object *bufObj;
2264 GLenum retval;
2265
2266 bufObj = _mesa_lookup_bufferobj(ctx, name);
2267 if (!bufObj) {
2268 _mesa_error(ctx, GL_INVALID_VALUE,
2269 "glObjectPurgeable(name = 0x%x)", name);
2270 return 0;
2271 }
2272 if (!_mesa_is_bufferobj(bufObj)) {
2273 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
2274 return 0;
2275 }
2276
2277 if (bufObj->Purgeable) {
2278 _mesa_error(ctx, GL_INVALID_OPERATION,
2279 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2280 return GL_VOLATILE_APPLE;
2281 }
2282
2283 bufObj->Purgeable = GL_TRUE;
2284
2285 retval = GL_VOLATILE_APPLE;
2286 if (ctx->Driver.BufferObjectPurgeable)
2287 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
2288
2289 return retval;
2290 }
2291
2292
2293 static GLenum
2294 renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2295 {
2296 struct gl_renderbuffer *bufObj;
2297 GLenum retval;
2298
2299 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2300 if (!bufObj) {
2301 _mesa_error(ctx, GL_INVALID_VALUE,
2302 "glObjectUnpurgeable(name = 0x%x)", name);
2303 return 0;
2304 }
2305
2306 if (bufObj->Purgeable) {
2307 _mesa_error(ctx, GL_INVALID_OPERATION,
2308 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2309 return GL_VOLATILE_APPLE;
2310 }
2311
2312 bufObj->Purgeable = GL_TRUE;
2313
2314 retval = GL_VOLATILE_APPLE;
2315 if (ctx->Driver.RenderObjectPurgeable)
2316 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
2317
2318 return retval;
2319 }
2320
2321
2322 static GLenum
2323 texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2324 {
2325 struct gl_texture_object *bufObj;
2326 GLenum retval;
2327
2328 bufObj = _mesa_lookup_texture(ctx, name);
2329 if (!bufObj) {
2330 _mesa_error(ctx, GL_INVALID_VALUE,
2331 "glObjectPurgeable(name = 0x%x)", name);
2332 return 0;
2333 }
2334
2335 if (bufObj->Purgeable) {
2336 _mesa_error(ctx, GL_INVALID_OPERATION,
2337 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2338 return GL_VOLATILE_APPLE;
2339 }
2340
2341 bufObj->Purgeable = GL_TRUE;
2342
2343 retval = GL_VOLATILE_APPLE;
2344 if (ctx->Driver.TextureObjectPurgeable)
2345 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
2346
2347 return retval;
2348 }
2349
2350
2351 GLenum GLAPIENTRY
2352 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2353 {
2354 GLenum retval;
2355
2356 GET_CURRENT_CONTEXT(ctx);
2357 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2358
2359 if (name == 0) {
2360 _mesa_error(ctx, GL_INVALID_VALUE,
2361 "glObjectPurgeable(name = 0x%x)", name);
2362 return 0;
2363 }
2364
2365 switch (option) {
2366 case GL_VOLATILE_APPLE:
2367 case GL_RELEASED_APPLE:
2368 /* legal */
2369 break;
2370 default:
2371 _mesa_error(ctx, GL_INVALID_ENUM,
2372 "glObjectPurgeable(name = 0x%x) invalid option: %d",
2373 name, option);
2374 return 0;
2375 }
2376
2377 switch (objectType) {
2378 case GL_TEXTURE:
2379 retval = texture_object_purgeable(ctx, name, option);
2380 break;
2381 case GL_RENDERBUFFER_EXT:
2382 retval = renderbuffer_purgeable(ctx, name, option);
2383 break;
2384 case GL_BUFFER_OBJECT_APPLE:
2385 retval = buffer_object_purgeable(ctx, name, option);
2386 break;
2387 default:
2388 _mesa_error(ctx, GL_INVALID_ENUM,
2389 "glObjectPurgeable(name = 0x%x) invalid type: %d",
2390 name, objectType);
2391 return 0;
2392 }
2393
2394 /* In strict conformance to the spec, we must only return VOLATILE when
2395 * when passed the VOLATILE option. Madness.
2396 *
2397 * XXX First fix the spec, then fix me.
2398 */
2399 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
2400 }
2401
2402
2403 static GLenum
2404 buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2405 {
2406 struct gl_buffer_object *bufObj;
2407 GLenum retval;
2408
2409 bufObj = _mesa_lookup_bufferobj(ctx, name);
2410 if (!bufObj) {
2411 _mesa_error(ctx, GL_INVALID_VALUE,
2412 "glObjectUnpurgeable(name = 0x%x)", name);
2413 return 0;
2414 }
2415
2416 if (! bufObj->Purgeable) {
2417 _mesa_error(ctx, GL_INVALID_OPERATION,
2418 "glObjectUnpurgeable(name = 0x%x) object is "
2419 " already \"unpurged\"", name);
2420 return 0;
2421 }
2422
2423 bufObj->Purgeable = GL_FALSE;
2424
2425 retval = option;
2426 if (ctx->Driver.BufferObjectUnpurgeable)
2427 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
2428
2429 return retval;
2430 }
2431
2432
2433 static GLenum
2434 renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2435 {
2436 struct gl_renderbuffer *bufObj;
2437 GLenum retval;
2438
2439 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2440 if (!bufObj) {
2441 _mesa_error(ctx, GL_INVALID_VALUE,
2442 "glObjectUnpurgeable(name = 0x%x)", name);
2443 return 0;
2444 }
2445
2446 if (! bufObj->Purgeable) {
2447 _mesa_error(ctx, GL_INVALID_OPERATION,
2448 "glObjectUnpurgeable(name = 0x%x) object is "
2449 " already \"unpurged\"", name);
2450 return 0;
2451 }
2452
2453 bufObj->Purgeable = GL_FALSE;
2454
2455 retval = option;
2456 if (ctx->Driver.RenderObjectUnpurgeable)
2457 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
2458
2459 return retval;
2460 }
2461
2462
2463 static GLenum
2464 texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2465 {
2466 struct gl_texture_object *bufObj;
2467 GLenum retval;
2468
2469 bufObj = _mesa_lookup_texture(ctx, name);
2470 if (!bufObj) {
2471 _mesa_error(ctx, GL_INVALID_VALUE,
2472 "glObjectUnpurgeable(name = 0x%x)", name);
2473 return 0;
2474 }
2475
2476 if (! bufObj->Purgeable) {
2477 _mesa_error(ctx, GL_INVALID_OPERATION,
2478 "glObjectUnpurgeable(name = 0x%x) object is"
2479 " already \"unpurged\"", name);
2480 return 0;
2481 }
2482
2483 bufObj->Purgeable = GL_FALSE;
2484
2485 retval = option;
2486 if (ctx->Driver.TextureObjectUnpurgeable)
2487 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
2488
2489 return retval;
2490 }
2491
2492
2493 GLenum GLAPIENTRY
2494 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2495 {
2496 GET_CURRENT_CONTEXT(ctx);
2497 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2498
2499 if (name == 0) {
2500 _mesa_error(ctx, GL_INVALID_VALUE,
2501 "glObjectUnpurgeable(name = 0x%x)", name);
2502 return 0;
2503 }
2504
2505 switch (option) {
2506 case GL_RETAINED_APPLE:
2507 case GL_UNDEFINED_APPLE:
2508 /* legal */
2509 break;
2510 default:
2511 _mesa_error(ctx, GL_INVALID_ENUM,
2512 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2513 name, option);
2514 return 0;
2515 }
2516
2517 switch (objectType) {
2518 case GL_BUFFER_OBJECT_APPLE:
2519 return buffer_object_unpurgeable(ctx, name, option);
2520 case GL_TEXTURE:
2521 return texture_object_unpurgeable(ctx, name, option);
2522 case GL_RENDERBUFFER_EXT:
2523 return renderbuffer_unpurgeable(ctx, name, option);
2524 default:
2525 _mesa_error(ctx, GL_INVALID_ENUM,
2526 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2527 name, objectType);
2528 return 0;
2529 }
2530 }
2531
2532
2533 static void
2534 get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
2535 GLenum pname, GLint *params)
2536 {
2537 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
2538 if (!bufObj) {
2539 _mesa_error(ctx, GL_INVALID_VALUE,
2540 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
2541 return;
2542 }
2543
2544 switch (pname) {
2545 case GL_PURGEABLE_APPLE:
2546 *params = bufObj->Purgeable;
2547 break;
2548 default:
2549 _mesa_error(ctx, GL_INVALID_ENUM,
2550 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2551 name, pname);
2552 break;
2553 }
2554 }
2555
2556
2557 static void
2558 get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
2559 GLenum pname, GLint *params)
2560 {
2561 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
2562 if (!rb) {
2563 _mesa_error(ctx, GL_INVALID_VALUE,
2564 "glObjectUnpurgeable(name = 0x%x)", name);
2565 return;
2566 }
2567
2568 switch (pname) {
2569 case GL_PURGEABLE_APPLE:
2570 *params = rb->Purgeable;
2571 break;
2572 default:
2573 _mesa_error(ctx, GL_INVALID_ENUM,
2574 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2575 name, pname);
2576 break;
2577 }
2578 }
2579
2580
2581 static void
2582 get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
2583 GLenum pname, GLint *params)
2584 {
2585 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
2586 if (!texObj) {
2587 _mesa_error(ctx, GL_INVALID_VALUE,
2588 "glObjectUnpurgeable(name = 0x%x)", name);
2589 return;
2590 }
2591
2592 switch (pname) {
2593 case GL_PURGEABLE_APPLE:
2594 *params = texObj->Purgeable;
2595 break;
2596 default:
2597 _mesa_error(ctx, GL_INVALID_ENUM,
2598 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2599 name, pname);
2600 break;
2601 }
2602 }
2603
2604
2605 void GLAPIENTRY
2606 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
2607 GLint *params)
2608 {
2609 GET_CURRENT_CONTEXT(ctx);
2610
2611 if (name == 0) {
2612 _mesa_error(ctx, GL_INVALID_VALUE,
2613 "glGetObjectParameteriv(name = 0x%x)", name);
2614 return;
2615 }
2616
2617 switch (objectType) {
2618 case GL_TEXTURE:
2619 get_texture_object_parameteriv(ctx, name, pname, params);
2620 break;
2621 case GL_BUFFER_OBJECT_APPLE:
2622 get_buffer_object_parameteriv(ctx, name, pname, params);
2623 break;
2624 case GL_RENDERBUFFER_EXT:
2625 get_renderbuffer_parameteriv(ctx, name, pname, params);
2626 break;
2627 default:
2628 _mesa_error(ctx, GL_INVALID_ENUM,
2629 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2630 name, objectType);
2631 }
2632 }
2633
2634 /**
2635 * Binds a buffer object to a uniform buffer binding point.
2636 *
2637 * The caller is responsible for flushing vertices and updating
2638 * NewDriverState.
2639 */
2640 static void
2641 set_ubo_binding(struct gl_context *ctx,
2642 struct gl_uniform_buffer_binding *binding,
2643 struct gl_buffer_object *bufObj,
2644 GLintptr offset,
2645 GLsizeiptr size,
2646 GLboolean autoSize)
2647 {
2648 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2649
2650 binding->Offset = offset;
2651 binding->Size = size;
2652 binding->AutomaticSize = autoSize;
2653
2654 /* If this is a real buffer object, mark it has having been used
2655 * at some point as a UBO.
2656 */
2657 if (size >= 0)
2658 bufObj->UsageHistory |= USAGE_UNIFORM_BUFFER;
2659 }
2660
2661 /**
2662 * Binds a buffer object to a uniform buffer binding point.
2663 *
2664 * Unlike set_ubo_binding(), this function also flushes vertices
2665 * and updates NewDriverState. It also checks if the binding
2666 * has actually changed before updating it.
2667 */
2668 static void
2669 bind_uniform_buffer(struct gl_context *ctx,
2670 GLuint index,
2671 struct gl_buffer_object *bufObj,
2672 GLintptr offset,
2673 GLsizeiptr size,
2674 GLboolean autoSize)
2675 {
2676 struct gl_uniform_buffer_binding *binding =
2677 &ctx->UniformBufferBindings[index];
2678
2679 if (binding->BufferObject == bufObj &&
2680 binding->Offset == offset &&
2681 binding->Size == size &&
2682 binding->AutomaticSize == autoSize) {
2683 return;
2684 }
2685
2686 FLUSH_VERTICES(ctx, 0);
2687 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
2688
2689 set_ubo_binding(ctx, binding, bufObj, offset, size, autoSize);
2690 }
2691
2692 /**
2693 * Bind a region of a buffer object to a uniform block binding point.
2694 * \param index the uniform buffer binding point index
2695 * \param bufObj the buffer object
2696 * \param offset offset to the start of buffer object region
2697 * \param size size of the buffer object region
2698 */
2699 static void
2700 bind_buffer_range_uniform_buffer(struct gl_context *ctx,
2701 GLuint index,
2702 struct gl_buffer_object *bufObj,
2703 GLintptr offset,
2704 GLsizeiptr size)
2705 {
2706 if (index >= ctx->Const.MaxUniformBufferBindings) {
2707 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
2708 return;
2709 }
2710
2711 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
2712 _mesa_error(ctx, GL_INVALID_VALUE,
2713 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
2714 ctx->Const.UniformBufferOffsetAlignment);
2715 return;
2716 }
2717
2718 if (bufObj == ctx->Shared->NullBufferObj) {
2719 offset = -1;
2720 size = -1;
2721 }
2722
2723 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2724 bind_uniform_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
2725 }
2726
2727
2728 /**
2729 * Bind a buffer object to a uniform block binding point.
2730 * As above, but offset = 0.
2731 */
2732 static void
2733 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
2734 GLuint index,
2735 struct gl_buffer_object *bufObj)
2736 {
2737 if (index >= ctx->Const.MaxUniformBufferBindings) {
2738 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2739 return;
2740 }
2741
2742 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2743
2744 if (bufObj == ctx->Shared->NullBufferObj)
2745 bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
2746 else
2747 bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
2748 }
2749
2750 /**
2751 * Binds a buffer object to an atomic buffer binding point.
2752 *
2753 * The caller is responsible for validating the offset,
2754 * flushing the vertices and updating NewDriverState.
2755 */
2756 static void
2757 set_atomic_buffer_binding(struct gl_context *ctx,
2758 struct gl_atomic_buffer_binding *binding,
2759 struct gl_buffer_object *bufObj,
2760 GLintptr offset,
2761 GLsizeiptr size)
2762 {
2763 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2764
2765 if (bufObj == ctx->Shared->NullBufferObj) {
2766 binding->Offset = -1;
2767 binding->Size = -1;
2768 } else {
2769 binding->Offset = offset;
2770 binding->Size = size;
2771 bufObj->UsageHistory |= USAGE_ATOMIC_COUNTER_BUFFER;
2772 }
2773 }
2774
2775 /**
2776 * Binds a buffer object to an atomic buffer binding point.
2777 *
2778 * Unlike set_atomic_buffer_binding(), this function also validates the
2779 * index and offset, flushes vertices, and updates NewDriverState.
2780 * It also checks if the binding has actually changing before
2781 * updating it.
2782 */
2783 static void
2784 bind_atomic_buffer(struct gl_context *ctx,
2785 unsigned index,
2786 struct gl_buffer_object *bufObj,
2787 GLintptr offset,
2788 GLsizeiptr size,
2789 const char *name)
2790 {
2791 struct gl_atomic_buffer_binding *binding;
2792
2793 if (index >= ctx->Const.MaxAtomicBufferBindings) {
2794 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
2795 return;
2796 }
2797
2798 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
2799 _mesa_error(ctx, GL_INVALID_VALUE,
2800 "%s(offset misaligned %d/%d)", name, (int) offset,
2801 ATOMIC_COUNTER_SIZE);
2802 return;
2803 }
2804
2805 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
2806
2807 binding = &ctx->AtomicBufferBindings[index];
2808 if (binding->BufferObject == bufObj &&
2809 binding->Offset == offset &&
2810 binding->Size == size) {
2811 return;
2812 }
2813
2814 FLUSH_VERTICES(ctx, 0);
2815 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
2816
2817 set_atomic_buffer_binding(ctx, binding, bufObj, offset, size);
2818 }
2819
2820 static inline bool
2821 bind_buffers_check_offset_and_size(struct gl_context *ctx,
2822 GLuint index,
2823 const GLintptr *offsets,
2824 const GLsizeiptr *sizes)
2825 {
2826 if (offsets[index] < 0) {
2827 /* The ARB_multi_bind spec says:
2828 *
2829 * "An INVALID_VALUE error is generated by BindBuffersRange if any
2830 * value in <offsets> is less than zero (per binding)."
2831 */
2832 _mesa_error(ctx, GL_INVALID_VALUE,
2833 "glBindBuffersRange(offsets[%u]=%" PRId64 " < 0)",
2834 index, (int64_t) offsets[index]);
2835 return false;
2836 }
2837
2838 if (sizes[index] <= 0) {
2839 /* The ARB_multi_bind spec says:
2840 *
2841 * "An INVALID_VALUE error is generated by BindBuffersRange if any
2842 * value in <sizes> is less than or equal to zero (per binding)."
2843 */
2844 _mesa_error(ctx, GL_INVALID_VALUE,
2845 "glBindBuffersRange(sizes[%u]=%" PRId64 " <= 0)",
2846 index, (int64_t) sizes[index]);
2847 return false;
2848 }
2849
2850 return true;
2851 }
2852
2853 static bool
2854 error_check_bind_uniform_buffers(struct gl_context *ctx,
2855 GLuint first, GLsizei count,
2856 const char *caller)
2857 {
2858 if (!ctx->Extensions.ARB_uniform_buffer_object) {
2859 _mesa_error(ctx, GL_INVALID_ENUM,
2860 "%s(target=GL_UNIFORM_BUFFER)", caller);
2861 return false;
2862 }
2863
2864 /* The ARB_multi_bind_spec says:
2865 *
2866 * "An INVALID_OPERATION error is generated if <first> + <count> is
2867 * greater than the number of target-specific indexed binding points,
2868 * as described in section 6.7.1."
2869 */
2870 if (first + count > ctx->Const.MaxUniformBufferBindings) {
2871 _mesa_error(ctx, GL_INVALID_OPERATION,
2872 "%s(first=%u + count=%d > the value of "
2873 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
2874 caller, first, count,
2875 ctx->Const.MaxUniformBufferBindings);
2876 return false;
2877 }
2878
2879 return true;
2880 }
2881
2882 /**
2883 * Unbind all uniform buffers in the range
2884 * <first> through <first>+<count>-1
2885 */
2886 static void
2887 unbind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
2888 {
2889 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
2890 GLint i;
2891
2892 for (i = 0; i < count; i++)
2893 set_ubo_binding(ctx, &ctx->UniformBufferBindings[first + i],
2894 bufObj, -1, -1, GL_TRUE);
2895 }
2896
2897 static void
2898 bind_uniform_buffers_base(struct gl_context *ctx, GLuint first, GLsizei count,
2899 const GLuint *buffers)
2900 {
2901 GLint i;
2902
2903 if (!error_check_bind_uniform_buffers(ctx, first, count, "glBindBuffersBase"))
2904 return;
2905
2906 /* Assume that at least one binding will be changed */
2907 FLUSH_VERTICES(ctx, 0);
2908 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
2909
2910 if (!buffers) {
2911 /* The ARB_multi_bind spec says:
2912 *
2913 * "If <buffers> is NULL, all bindings from <first> through
2914 * <first>+<count>-1 are reset to their unbound (zero) state."
2915 */
2916 unbind_uniform_buffers(ctx, first, count);
2917 return;
2918 }
2919
2920 /* Note that the error semantics for multi-bind commands differ from
2921 * those of other GL commands.
2922 *
2923 * The Issues section in the ARB_multi_bind spec says:
2924 *
2925 * "(11) Typically, OpenGL specifies that if an error is generated by a
2926 * command, that command has no effect. This is somewhat
2927 * unfortunate for multi-bind commands, because it would require a
2928 * first pass to scan the entire list of bound objects for errors
2929 * and then a second pass to actually perform the bindings.
2930 * Should we have different error semantics?
2931 *
2932 * RESOLVED: Yes. In this specification, when the parameters for
2933 * one of the <count> binding points are invalid, that binding point
2934 * is not updated and an error will be generated. However, other
2935 * binding points in the same command will be updated if their
2936 * parameters are valid and no other error occurs."
2937 */
2938
2939 _mesa_begin_bufferobj_lookups(ctx);
2940
2941 for (i = 0; i < count; i++) {
2942 struct gl_uniform_buffer_binding *binding =
2943 &ctx->UniformBufferBindings[first + i];
2944 struct gl_buffer_object *bufObj;
2945
2946 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
2947 bufObj = binding->BufferObject;
2948 else
2949 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
2950 "glBindBuffersBase");
2951
2952 if (bufObj) {
2953 if (bufObj == ctx->Shared->NullBufferObj)
2954 set_ubo_binding(ctx, binding, bufObj, -1, -1, GL_TRUE);
2955 else
2956 set_ubo_binding(ctx, binding, bufObj, 0, 0, GL_TRUE);
2957 }
2958 }
2959
2960 _mesa_end_bufferobj_lookups(ctx);
2961 }
2962
2963 static void
2964 bind_uniform_buffers_range(struct gl_context *ctx, GLuint first, GLsizei count,
2965 const GLuint *buffers,
2966 const GLintptr *offsets, const GLsizeiptr *sizes)
2967 {
2968 GLint i;
2969
2970 if (!error_check_bind_uniform_buffers(ctx, first, count,
2971 "glBindBuffersRange"))
2972 return;
2973
2974 /* Assume that at least one binding will be changed */
2975 FLUSH_VERTICES(ctx, 0);
2976 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
2977
2978 if (!buffers) {
2979 /* The ARB_multi_bind spec says:
2980 *
2981 * "If <buffers> is NULL, all bindings from <first> through
2982 * <first>+<count>-1 are reset to their unbound (zero) state.
2983 * In this case, the offsets and sizes associated with the
2984 * binding points are set to default values, ignoring
2985 * <offsets> and <sizes>."
2986 */
2987 unbind_uniform_buffers(ctx, first, count);
2988 return;
2989 }
2990
2991 /* Note that the error semantics for multi-bind commands differ from
2992 * those of other GL commands.
2993 *
2994 * The Issues section in the ARB_multi_bind spec says:
2995 *
2996 * "(11) Typically, OpenGL specifies that if an error is generated by a
2997 * command, that command has no effect. This is somewhat
2998 * unfortunate for multi-bind commands, because it would require a
2999 * first pass to scan the entire list of bound objects for errors
3000 * and then a second pass to actually perform the bindings.
3001 * Should we have different error semantics?
3002 *
3003 * RESOLVED: Yes. In this specification, when the parameters for
3004 * one of the <count> binding points are invalid, that binding point
3005 * is not updated and an error will be generated. However, other
3006 * binding points in the same command will be updated if their
3007 * parameters are valid and no other error occurs."
3008 */
3009
3010 _mesa_begin_bufferobj_lookups(ctx);
3011
3012 for (i = 0; i < count; i++) {
3013 struct gl_uniform_buffer_binding *binding =
3014 &ctx->UniformBufferBindings[first + i];
3015 struct gl_buffer_object *bufObj;
3016
3017 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3018 continue;
3019
3020 /* The ARB_multi_bind spec says:
3021 *
3022 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3023 * pair of values in <offsets> and <sizes> does not respectively
3024 * satisfy the constraints described for those parameters for the
3025 * specified target, as described in section 6.7.1 (per binding)."
3026 *
3027 * Section 6.7.1 refers to table 6.5, which says:
3028 *
3029 * "┌───────────────────────────────────────────────────────────────┐
3030 * │ Uniform buffer array bindings (see sec. 7.6) │
3031 * ├─────────────────────┬─────────────────────────────────────────┤
3032 * │ ... │ ... │
3033 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3034 * │ │ OFFSET_ALIGNMENT │
3035 * │ ... │ ... │
3036 * │ size restriction │ none │
3037 * └─────────────────────┴─────────────────────────────────────────┘"
3038 */
3039 if (offsets[i] & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3040 _mesa_error(ctx, GL_INVALID_VALUE,
3041 "glBindBuffersRange(offsets[%u]=%" PRId64
3042 " is misaligned; it must be a multiple of the value of "
3043 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3044 "target=GL_UNIFORM_BUFFER)",
3045 i, (int64_t) offsets[i],
3046 ctx->Const.UniformBufferOffsetAlignment);
3047 continue;
3048 }
3049
3050 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3051 bufObj = binding->BufferObject;
3052 else
3053 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3054 "glBindBuffersRange");
3055
3056 if (bufObj) {
3057 if (bufObj == ctx->Shared->NullBufferObj)
3058 set_ubo_binding(ctx, binding, bufObj, -1, -1, GL_FALSE);
3059 else
3060 set_ubo_binding(ctx, binding, bufObj,
3061 offsets[i], sizes[i], GL_FALSE);
3062 }
3063 }
3064
3065 _mesa_end_bufferobj_lookups(ctx);
3066 }
3067
3068 static bool
3069 error_check_bind_xfb_buffers(struct gl_context *ctx,
3070 struct gl_transform_feedback_object *tfObj,
3071 GLuint first, GLsizei count, const char *caller)
3072 {
3073 if (!ctx->Extensions.EXT_transform_feedback) {
3074 _mesa_error(ctx, GL_INVALID_ENUM,
3075 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller);
3076 return false;
3077 }
3078
3079 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3080 *
3081 * "An INVALID_OPERATION error is generated :
3082 *
3083 * ...
3084 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3085 * FEEDBACK_BUFFER and transform feedback is currently active."
3086 *
3087 * We assume that this is also meant to apply to BindBuffersRange
3088 * and BindBuffersBase.
3089 */
3090 if (tfObj->Active) {
3091 _mesa_error(ctx, GL_INVALID_OPERATION,
3092 "%s(Changing transform feedback buffers while "
3093 "transform feedback is active)", caller);
3094 return false;
3095 }
3096
3097 /* The ARB_multi_bind_spec says:
3098 *
3099 * "An INVALID_OPERATION error is generated if <first> + <count> is
3100 * greater than the number of target-specific indexed binding points,
3101 * as described in section 6.7.1."
3102 */
3103 if (first + count > ctx->Const.MaxTransformFeedbackBuffers) {
3104 _mesa_error(ctx, GL_INVALID_OPERATION,
3105 "%s(first=%u + count=%d > the value of "
3106 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3107 caller, first, count,
3108 ctx->Const.MaxTransformFeedbackBuffers);
3109 return false;
3110 }
3111
3112 return true;
3113 }
3114
3115 /**
3116 * Unbind all transform feedback buffers in the range
3117 * <first> through <first>+<count>-1
3118 */
3119 static void
3120 unbind_xfb_buffers(struct gl_context *ctx,
3121 struct gl_transform_feedback_object *tfObj,
3122 GLuint first, GLsizei count)
3123 {
3124 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3125 GLint i;
3126
3127 for (i = 0; i < count; i++)
3128 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
3129 bufObj, 0, 0);
3130 }
3131
3132 static void
3133 bind_xfb_buffers_base(struct gl_context *ctx,
3134 GLuint first, GLsizei count,
3135 const GLuint *buffers)
3136 {
3137 struct gl_transform_feedback_object *tfObj =
3138 ctx->TransformFeedback.CurrentObject;
3139 GLint i;
3140
3141 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count,
3142 "glBindBuffersBase"))
3143 return;
3144
3145 /* Assume that at least one binding will be changed */
3146 FLUSH_VERTICES(ctx, 0);
3147 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
3148
3149 if (!buffers) {
3150 /* The ARB_multi_bind spec says:
3151 *
3152 * "If <buffers> is NULL, all bindings from <first> through
3153 * <first>+<count>-1 are reset to their unbound (zero) state."
3154 */
3155 unbind_xfb_buffers(ctx, tfObj, first, count);
3156 return;
3157 }
3158
3159 /* Note that the error semantics for multi-bind commands differ from
3160 * those of other GL commands.
3161 *
3162 * The Issues section in the ARB_multi_bind spec says:
3163 *
3164 * "(11) Typically, OpenGL specifies that if an error is generated by a
3165 * command, that command has no effect. This is somewhat
3166 * unfortunate for multi-bind commands, because it would require a
3167 * first pass to scan the entire list of bound objects for errors
3168 * and then a second pass to actually perform the bindings.
3169 * Should we have different error semantics?
3170 *
3171 * RESOLVED: Yes. In this specification, when the parameters for
3172 * one of the <count> binding points are invalid, that binding point
3173 * is not updated and an error will be generated. However, other
3174 * binding points in the same command will be updated if their
3175 * parameters are valid and no other error occurs."
3176 */
3177
3178 _mesa_begin_bufferobj_lookups(ctx);
3179
3180 for (i = 0; i < count; i++) {
3181 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[first + i];
3182 struct gl_buffer_object *bufObj;
3183
3184 if (boundBufObj && boundBufObj->Name == buffers[i])
3185 bufObj = boundBufObj;
3186 else
3187 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3188 "glBindBuffersBase");
3189
3190 if (bufObj)
3191 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
3192 bufObj, 0, 0);
3193 }
3194
3195 _mesa_end_bufferobj_lookups(ctx);
3196 }
3197
3198 static void
3199 bind_xfb_buffers_range(struct gl_context *ctx,
3200 GLuint first, GLsizei count,
3201 const GLuint *buffers,
3202 const GLintptr *offsets,
3203 const GLsizeiptr *sizes)
3204 {
3205 struct gl_transform_feedback_object *tfObj =
3206 ctx->TransformFeedback.CurrentObject;
3207 GLint i;
3208
3209 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count,
3210 "glBindBuffersRange"))
3211 return;
3212
3213 /* Assume that at least one binding will be changed */
3214 FLUSH_VERTICES(ctx, 0);
3215 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
3216
3217 if (!buffers) {
3218 /* The ARB_multi_bind spec says:
3219 *
3220 * "If <buffers> is NULL, all bindings from <first> through
3221 * <first>+<count>-1 are reset to their unbound (zero) state.
3222 * In this case, the offsets and sizes associated with the
3223 * binding points are set to default values, ignoring
3224 * <offsets> and <sizes>."
3225 */
3226 unbind_xfb_buffers(ctx, tfObj, first, count);
3227 return;
3228 }
3229
3230 /* Note that the error semantics for multi-bind commands differ from
3231 * those of other GL commands.
3232 *
3233 * The Issues section in the ARB_multi_bind spec says:
3234 *
3235 * "(11) Typically, OpenGL specifies that if an error is generated by a
3236 * command, that command has no effect. This is somewhat
3237 * unfortunate for multi-bind commands, because it would require a
3238 * first pass to scan the entire list of bound objects for errors
3239 * and then a second pass to actually perform the bindings.
3240 * Should we have different error semantics?
3241 *
3242 * RESOLVED: Yes. In this specification, when the parameters for
3243 * one of the <count> binding points are invalid, that binding point
3244 * is not updated and an error will be generated. However, other
3245 * binding points in the same command will be updated if their
3246 * parameters are valid and no other error occurs."
3247 */
3248
3249 _mesa_begin_bufferobj_lookups(ctx);
3250
3251 for (i = 0; i < count; i++) {
3252 const GLuint index = first + i;
3253 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[index];
3254 struct gl_buffer_object *bufObj;
3255
3256 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3257 continue;
3258
3259 /* The ARB_multi_bind spec says:
3260 *
3261 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3262 * pair of values in <offsets> and <sizes> does not respectively
3263 * satisfy the constraints described for those parameters for the
3264 * specified target, as described in section 6.7.1 (per binding)."
3265 *
3266 * Section 6.7.1 refers to table 6.5, which says:
3267 *
3268 * "┌───────────────────────────────────────────────────────────────┐
3269 * │ Transform feedback array bindings (see sec. 13.2.2) │
3270 * ├───────────────────────┬───────────────────────────────────────┤
3271 * │ ... │ ... │
3272 * │ offset restriction │ multiple of 4 │
3273 * │ ... │ ... │
3274 * │ size restriction │ multiple of 4 │
3275 * └───────────────────────┴───────────────────────────────────────┘"
3276 */
3277 if (offsets[i] & 0x3) {
3278 _mesa_error(ctx, GL_INVALID_VALUE,
3279 "glBindBuffersRange(offsets[%u]=%" PRId64
3280 " is misaligned; it must be a multiple of 4 when "
3281 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3282 i, (int64_t) offsets[i]);
3283 continue;
3284 }
3285
3286 if (sizes[i] & 0x3) {
3287 _mesa_error(ctx, GL_INVALID_VALUE,
3288 "glBindBuffersRange(sizes[%u]=%" PRId64
3289 " is misaligned; it must be a multiple of 4 when "
3290 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3291 i, (int64_t) sizes[i]);
3292 continue;
3293 }
3294
3295 if (boundBufObj && boundBufObj->Name == buffers[i])
3296 bufObj = boundBufObj;
3297 else
3298 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3299 "glBindBuffersRange");
3300
3301 if (bufObj)
3302 _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj,
3303 offsets[i], sizes[i]);
3304 }
3305
3306 _mesa_end_bufferobj_lookups(ctx);
3307 }
3308
3309 static bool
3310 error_check_bind_atomic_buffers(struct gl_context *ctx,
3311 GLuint first, GLsizei count,
3312 const char *caller)
3313 {
3314 if (!ctx->Extensions.ARB_shader_atomic_counters) {
3315 _mesa_error(ctx, GL_INVALID_ENUM,
3316 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller);
3317 return false;
3318 }
3319
3320 /* The ARB_multi_bind_spec says:
3321 *
3322 * "An INVALID_OPERATION error is generated if <first> + <count> is
3323 * greater than the number of target-specific indexed binding points,
3324 * as described in section 6.7.1."
3325 */
3326 if (first + count > ctx->Const.MaxAtomicBufferBindings) {
3327 _mesa_error(ctx, GL_INVALID_OPERATION,
3328 "%s(first=%u + count=%d > the value of "
3329 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3330 caller, first, count, ctx->Const.MaxAtomicBufferBindings);
3331 return false;
3332 }
3333
3334 return true;
3335 }
3336
3337 /**
3338 * Unbind all atomic counter buffers in the range
3339 * <first> through <first>+<count>-1
3340 */
3341 static void
3342 unbind_atomic_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3343 {
3344 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3345 GLint i;
3346
3347 for (i = 0; i < count; i++)
3348 set_atomic_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i],
3349 bufObj, -1, -1);
3350 }
3351
3352 static void
3353 bind_atomic_buffers_base(struct gl_context *ctx,
3354 GLuint first,
3355 GLsizei count,
3356 const GLuint *buffers)
3357 {
3358 GLint i;
3359
3360 if (!error_check_bind_atomic_buffers(ctx, first, count,
3361 "glBindBuffersBase"))
3362 return;
3363
3364 /* Assume that at least one binding will be changed */
3365 FLUSH_VERTICES(ctx, 0);
3366 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3367
3368 if (!buffers) {
3369 /* The ARB_multi_bind spec says:
3370 *
3371 * "If <buffers> is NULL, all bindings from <first> through
3372 * <first>+<count>-1 are reset to their unbound (zero) state."
3373 */
3374 unbind_atomic_buffers(ctx, first, count);
3375 return;
3376 }
3377
3378 /* Note that the error semantics for multi-bind commands differ from
3379 * those of other GL commands.
3380 *
3381 * The Issues section in the ARB_multi_bind spec says:
3382 *
3383 * "(11) Typically, OpenGL specifies that if an error is generated by a
3384 * command, that command has no effect. This is somewhat
3385 * unfortunate for multi-bind commands, because it would require a
3386 * first pass to scan the entire list of bound objects for errors
3387 * and then a second pass to actually perform the bindings.
3388 * Should we have different error semantics?
3389 *
3390 * RESOLVED: Yes. In this specification, when the parameters for
3391 * one of the <count> binding points are invalid, that binding point
3392 * is not updated and an error will be generated. However, other
3393 * binding points in the same command will be updated if their
3394 * parameters are valid and no other error occurs."
3395 */
3396
3397 _mesa_begin_bufferobj_lookups(ctx);
3398
3399 for (i = 0; i < count; i++) {
3400 struct gl_atomic_buffer_binding *binding =
3401 &ctx->AtomicBufferBindings[first + i];
3402 struct gl_buffer_object *bufObj;
3403
3404 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3405 bufObj = binding->BufferObject;
3406 else
3407 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3408 "glBindBuffersBase");
3409
3410 if (bufObj)
3411 set_atomic_buffer_binding(ctx, binding, bufObj, 0, 0);
3412 }
3413
3414 _mesa_end_bufferobj_lookups(ctx);
3415 }
3416
3417 static void
3418 bind_atomic_buffers_range(struct gl_context *ctx,
3419 GLuint first,
3420 GLsizei count,
3421 const GLuint *buffers,
3422 const GLintptr *offsets,
3423 const GLsizeiptr *sizes)
3424 {
3425 GLint i;
3426
3427 if (!error_check_bind_atomic_buffers(ctx, first, count,
3428 "glBindBuffersRange"))
3429 return;
3430
3431 /* Assume that at least one binding will be changed */
3432 FLUSH_VERTICES(ctx, 0);
3433 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3434
3435 if (!buffers) {
3436 /* The ARB_multi_bind spec says:
3437 *
3438 * "If <buffers> is NULL, all bindings from <first> through
3439 * <first>+<count>-1 are reset to their unbound (zero) state.
3440 * In this case, the offsets and sizes associated with the
3441 * binding points are set to default values, ignoring
3442 * <offsets> and <sizes>."
3443 */
3444 unbind_atomic_buffers(ctx, first, count);
3445 return;
3446 }
3447
3448 /* Note that the error semantics for multi-bind commands differ from
3449 * those of other GL commands.
3450 *
3451 * The Issues section in the ARB_multi_bind spec says:
3452 *
3453 * "(11) Typically, OpenGL specifies that if an error is generated by a
3454 * command, that command has no effect. This is somewhat
3455 * unfortunate for multi-bind commands, because it would require a
3456 * first pass to scan the entire list of bound objects for errors
3457 * and then a second pass to actually perform the bindings.
3458 * Should we have different error semantics?
3459 *
3460 * RESOLVED: Yes. In this specification, when the parameters for
3461 * one of the <count> binding points are invalid, that binding point
3462 * is not updated and an error will be generated. However, other
3463 * binding points in the same command will be updated if their
3464 * parameters are valid and no other error occurs."
3465 */
3466
3467 _mesa_begin_bufferobj_lookups(ctx);
3468
3469 for (i = 0; i < count; i++) {
3470 struct gl_atomic_buffer_binding *binding =
3471 &ctx->AtomicBufferBindings[first + i];
3472 struct gl_buffer_object *bufObj;
3473
3474 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3475 continue;
3476
3477 /* The ARB_multi_bind spec says:
3478 *
3479 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3480 * pair of values in <offsets> and <sizes> does not respectively
3481 * satisfy the constraints described for those parameters for the
3482 * specified target, as described in section 6.7.1 (per binding)."
3483 *
3484 * Section 6.7.1 refers to table 6.5, which says:
3485 *
3486 * "┌───────────────────────────────────────────────────────────────┐
3487 * │ Atomic counter array bindings (see sec. 7.7.2) │
3488 * ├───────────────────────┬───────────────────────────────────────┤
3489 * │ ... │ ... │
3490 * │ offset restriction │ multiple of 4 │
3491 * │ ... │ ... │
3492 * │ size restriction │ none │
3493 * └───────────────────────┴───────────────────────────────────────┘"
3494 */
3495 if (offsets[i] & (ATOMIC_COUNTER_SIZE - 1)) {
3496 _mesa_error(ctx, GL_INVALID_VALUE,
3497 "glBindBuffersRange(offsets[%u]=%" PRId64
3498 " is misaligned; it must be a multiple of %d when "
3499 "target=GL_ATOMIC_COUNTER_BUFFER)",
3500 i, (int64_t) offsets[i], ATOMIC_COUNTER_SIZE);
3501 continue;
3502 }
3503
3504 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3505 bufObj = binding->BufferObject;
3506 else
3507 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3508 "glBindBuffersRange");
3509
3510 if (bufObj)
3511 set_atomic_buffer_binding(ctx, binding, bufObj, offsets[i], sizes[i]);
3512 }
3513
3514 _mesa_end_bufferobj_lookups(ctx);
3515 }
3516
3517 void GLAPIENTRY
3518 _mesa_BindBufferRange(GLenum target, GLuint index,
3519 GLuint buffer, GLintptr offset, GLsizeiptr size)
3520 {
3521 GET_CURRENT_CONTEXT(ctx);
3522 struct gl_buffer_object *bufObj;
3523
3524 if (buffer == 0) {
3525 bufObj = ctx->Shared->NullBufferObj;
3526 } else {
3527 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3528 }
3529 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
3530 &bufObj, "glBindBufferRange"))
3531 return;
3532
3533 if (!bufObj) {
3534 _mesa_error(ctx, GL_INVALID_OPERATION,
3535 "glBindBufferRange(invalid buffer=%u)", buffer);
3536 return;
3537 }
3538
3539 if (buffer != 0) {
3540 if (size <= 0) {
3541 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
3542 (int) size);
3543 return;
3544 }
3545 }
3546
3547 switch (target) {
3548 case GL_TRANSFORM_FEEDBACK_BUFFER:
3549 _mesa_bind_buffer_range_transform_feedback(ctx, index, bufObj,
3550 offset, size);
3551 return;
3552 case GL_UNIFORM_BUFFER:
3553 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
3554 return;
3555 case GL_ATOMIC_COUNTER_BUFFER:
3556 bind_atomic_buffer(ctx, index, bufObj, offset, size,
3557 "glBindBufferRange");
3558 return;
3559 default:
3560 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
3561 return;
3562 }
3563 }
3564
3565 void GLAPIENTRY
3566 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
3567 {
3568 GET_CURRENT_CONTEXT(ctx);
3569 struct gl_buffer_object *bufObj;
3570
3571 if (buffer == 0) {
3572 bufObj = ctx->Shared->NullBufferObj;
3573 } else {
3574 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3575 }
3576 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
3577 &bufObj, "glBindBufferBase"))
3578 return;
3579
3580 if (!bufObj) {
3581 _mesa_error(ctx, GL_INVALID_OPERATION,
3582 "glBindBufferBase(invalid buffer=%u)", buffer);
3583 return;
3584 }
3585
3586 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3587 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3588 *
3589 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3590 * zero and size equal to the size of buffer."
3591 *
3592 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3593 *
3594 * "If the parameter (starting offset or size) was not specified when the
3595 * buffer object was bound, zero is returned."
3596 *
3597 * What happens if the size of the buffer changes? Does the size of the
3598 * buffer at the moment glBindBufferBase was called still play a role, like
3599 * the first quote would imply, or is the size meaningless in the
3600 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3601 * core spec page 45 says:
3602 *
3603 * "It is equivalent to calling BindBufferRange with offset zero, while
3604 * size is determined by the size of the bound buffer at the time the
3605 * binding is used."
3606 *
3607 * My interpretation is that the GL 4.1 spec was a clarification of the
3608 * behavior, not a change. In particular, this choice will only make
3609 * rendering work in cases where it would have had undefined results.
3610 */
3611
3612 switch (target) {
3613 case GL_TRANSFORM_FEEDBACK_BUFFER:
3614 _mesa_bind_buffer_base_transform_feedback(ctx, index, bufObj);
3615 return;
3616 case GL_UNIFORM_BUFFER:
3617 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
3618 return;
3619 case GL_ATOMIC_COUNTER_BUFFER:
3620 bind_atomic_buffer(ctx, index, bufObj, 0, 0,
3621 "glBindBufferBase");
3622 return;
3623 default:
3624 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
3625 return;
3626 }
3627 }
3628
3629 void GLAPIENTRY
3630 _mesa_BindBuffersRange(GLenum target, GLuint first, GLsizei count,
3631 const GLuint *buffers,
3632 const GLintptr *offsets, const GLsizeiptr *sizes)
3633 {
3634 GET_CURRENT_CONTEXT(ctx);
3635
3636 switch (target) {
3637 case GL_TRANSFORM_FEEDBACK_BUFFER:
3638 bind_xfb_buffers_range(ctx, first, count, buffers, offsets, sizes);
3639 return;
3640 case GL_UNIFORM_BUFFER:
3641 bind_uniform_buffers_range(ctx, first, count, buffers, offsets, sizes);
3642 return;
3643 case GL_ATOMIC_COUNTER_BUFFER:
3644 bind_atomic_buffers_range(ctx, first, count, buffers,
3645 offsets, sizes);
3646 return;
3647 default:
3648 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersRange(target=%s)",
3649 _mesa_lookup_enum_by_nr(target));
3650 break;
3651 }
3652 }
3653
3654 void GLAPIENTRY
3655 _mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count,
3656 const GLuint *buffers)
3657 {
3658 GET_CURRENT_CONTEXT(ctx);
3659
3660 switch (target) {
3661 case GL_TRANSFORM_FEEDBACK_BUFFER:
3662 bind_xfb_buffers_base(ctx, first, count, buffers);
3663 return;
3664 case GL_UNIFORM_BUFFER:
3665 bind_uniform_buffers_base(ctx, first, count, buffers);
3666 return;
3667 case GL_ATOMIC_COUNTER_BUFFER:
3668 bind_atomic_buffers_base(ctx, first, count, buffers);
3669 return;
3670 default:
3671 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersBase(target=%s)",
3672 _mesa_lookup_enum_by_nr(target));
3673 break;
3674 }
3675 }
3676
3677 void GLAPIENTRY
3678 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
3679 GLsizeiptr length)
3680 {
3681 GET_CURRENT_CONTEXT(ctx);
3682 struct gl_buffer_object *bufObj;
3683 const GLintptr end = offset + length;
3684
3685 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3686 if (!bufObj) {
3687 _mesa_error(ctx, GL_INVALID_VALUE,
3688 "glInvalidateBufferSubData(name = 0x%x) invalid object",
3689 buffer);
3690 return;
3691 }
3692
3693 /* The GL_ARB_invalidate_subdata spec says:
3694 *
3695 * "An INVALID_VALUE error is generated if <offset> or <length> is
3696 * negative, or if <offset> + <length> is greater than the value of
3697 * BUFFER_SIZE."
3698 */
3699 if (end < 0 || end > bufObj->Size) {
3700 _mesa_error(ctx, GL_INVALID_VALUE,
3701 "glInvalidateBufferSubData(invalid offset or length)");
3702 return;
3703 }
3704
3705 /* The OpenGL 4.4 (Core Profile) spec says:
3706 *
3707 * "An INVALID_OPERATION error is generated if buffer is currently
3708 * mapped by MapBuffer or if the invalidate range intersects the range
3709 * currently mapped by MapBufferRange, unless it was mapped
3710 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
3711 */
3712 if (!(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) &&
3713 bufferobj_range_mapped(bufObj, offset, length)) {
3714 _mesa_error(ctx, GL_INVALID_OPERATION,
3715 "glInvalidateBufferSubData(intersection with mapped "
3716 "range)");
3717 return;
3718 }
3719
3720 /* We don't actually do anything for this yet. Just return after
3721 * validating the parameters and generating the required errors.
3722 */
3723 return;
3724 }
3725
3726 void GLAPIENTRY
3727 _mesa_InvalidateBufferData(GLuint buffer)
3728 {
3729 GET_CURRENT_CONTEXT(ctx);
3730 struct gl_buffer_object *bufObj;
3731
3732 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3733 if (!bufObj) {
3734 _mesa_error(ctx, GL_INVALID_VALUE,
3735 "glInvalidateBufferData(name = 0x%x) invalid object",
3736 buffer);
3737 return;
3738 }
3739
3740 /* The OpenGL 4.4 (Core Profile) spec says:
3741 *
3742 * "An INVALID_OPERATION error is generated if buffer is currently
3743 * mapped by MapBuffer or if the invalidate range intersects the range
3744 * currently mapped by MapBufferRange, unless it was mapped
3745 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
3746 */
3747 if (_mesa_check_disallowed_mapping(bufObj)) {
3748 _mesa_error(ctx, GL_INVALID_OPERATION,
3749 "glInvalidateBufferData(intersection with mapped "
3750 "range)");
3751 return;
3752 }
3753
3754 /* We don't actually do anything for this yet. Just return after
3755 * validating the parameters and generating the required errors.
3756 */
3757 return;
3758 }