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