mesa: add error checks to glMapBufferRange, glMapBuffer for ARB_buffer_storage
[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 "glheader.h"
35 #include "enums.h"
36 #include "hash.h"
37 #include "imports.h"
38 #include "image.h"
39 #include "context.h"
40 #include "bufferobj.h"
41 #include "fbobject.h"
42 #include "mtypes.h"
43 #include "texobj.h"
44 #include "teximage.h"
45 #include "glformats.h"
46 #include "texstore.h"
47 #include "transformfeedback.h"
48 #include "dispatch.h"
49
50
51 /* Debug flags */
52 /*#define VBO_DEBUG*/
53 /*#define BOUNDS_CHECK*/
54
55
56 /**
57 * Used as a placeholder for buffer objects between glGenBuffers() and
58 * glBindBuffer() so that glIsBuffer() can work correctly.
59 */
60 static struct gl_buffer_object DummyBufferObject;
61
62
63 /**
64 * Return pointer to address of a buffer object target.
65 * \param ctx the GL context
66 * \param target the buffer object target to be retrieved.
67 * \return pointer to pointer to the buffer object bound to \c target in the
68 * specified context or \c NULL if \c target is invalid.
69 */
70 static inline struct gl_buffer_object **
71 get_buffer_target(struct gl_context *ctx, GLenum target)
72 {
73 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
74 */
75 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx)
76 && target != GL_ARRAY_BUFFER && target != GL_ELEMENT_ARRAY_BUFFER)
77 return NULL;
78
79 switch (target) {
80 case GL_ARRAY_BUFFER_ARB:
81 return &ctx->Array.ArrayBufferObj;
82 case GL_ELEMENT_ARRAY_BUFFER_ARB:
83 return &ctx->Array.VAO->IndexBufferObj;
84 case GL_PIXEL_PACK_BUFFER_EXT:
85 return &ctx->Pack.BufferObj;
86 case GL_PIXEL_UNPACK_BUFFER_EXT:
87 return &ctx->Unpack.BufferObj;
88 case GL_COPY_READ_BUFFER:
89 return &ctx->CopyReadBuffer;
90 case GL_COPY_WRITE_BUFFER:
91 return &ctx->CopyWriteBuffer;
92 case GL_DRAW_INDIRECT_BUFFER:
93 if (ctx->API == API_OPENGL_CORE &&
94 ctx->Extensions.ARB_draw_indirect) {
95 return &ctx->DrawIndirectBuffer;
96 }
97 break;
98 case GL_TRANSFORM_FEEDBACK_BUFFER:
99 if (ctx->Extensions.EXT_transform_feedback) {
100 return &ctx->TransformFeedback.CurrentBuffer;
101 }
102 break;
103 case GL_TEXTURE_BUFFER:
104 if (ctx->API == API_OPENGL_CORE &&
105 ctx->Extensions.ARB_texture_buffer_object) {
106 return &ctx->Texture.BufferObject;
107 }
108 break;
109 case GL_UNIFORM_BUFFER:
110 if (ctx->Extensions.ARB_uniform_buffer_object) {
111 return &ctx->UniformBuffer;
112 }
113 break;
114 case GL_ATOMIC_COUNTER_BUFFER:
115 if (ctx->Extensions.ARB_shader_atomic_counters) {
116 return &ctx->AtomicBuffer;
117 }
118 break;
119 default:
120 return NULL;
121 }
122 return NULL;
123 }
124
125
126 /**
127 * Get the buffer object bound to the specified target in a GL context.
128 * \param ctx the GL context
129 * \param target the buffer object target to be retrieved.
130 * \param error the GL error to record if target is illegal.
131 * \return pointer to the buffer object bound to \c target in the
132 * specified context or \c NULL if \c target is invalid.
133 */
134 static inline struct gl_buffer_object *
135 get_buffer(struct gl_context *ctx, const char *func, GLenum target,
136 GLenum error)
137 {
138 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
139
140 if (!bufObj) {
141 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
142 return NULL;
143 }
144
145 if (!_mesa_is_bufferobj(*bufObj)) {
146 _mesa_error(ctx, error, "%s(no buffer bound)", func);
147 return NULL;
148 }
149
150 return *bufObj;
151 }
152
153
154 /**
155 * Convert a GLbitfield describing the mapped buffer access flags
156 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
157 */
158 static GLenum
159 simplified_access_mode(struct gl_context *ctx, GLbitfield access)
160 {
161 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
162 if ((access & rwFlags) == rwFlags)
163 return GL_READ_WRITE;
164 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
165 return GL_READ_ONLY;
166 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
167 return GL_WRITE_ONLY;
168
169 /* Otherwise, AccessFlags is zero (the default state).
170 *
171 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
172 *
173 * Name Type Initial Value Legal Values
174 * ... ... ... ...
175 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
176 * READ_WRITE
177 *
178 * However, table 6.8 in the GL_OES_mapbuffer extension says:
179 *
180 * Get Value Type Get Command Value Description
181 * --------- ---- ----------- ----- -----------
182 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
183 *
184 * The difference is because GL_OES_mapbuffer only supports mapping buffers
185 * write-only.
186 */
187 assert(access == 0);
188
189 return _mesa_is_gles(ctx) ? GL_WRITE_ONLY : GL_READ_WRITE;
190 }
191
192
193 /**
194 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
195 * given range.
196 * The regions do not overlap if and only if the end of the given
197 * region is before the mapped region or the start of the given region
198 * is after the mapped region.
199 *
200 * \param obj Buffer object target on which to operate.
201 * \param offset Offset of the first byte of the subdata range.
202 * \param size Size, in bytes, of the subdata range.
203 * \return true if ranges overlap, false otherwise
204 *
205 */
206 static bool
207 bufferobj_range_mapped(const struct gl_buffer_object *obj,
208 GLintptr offset, GLsizeiptr size)
209 {
210 if (_mesa_bufferobj_mapped(obj)) {
211 const GLintptr end = offset + size;
212 const GLintptr mapEnd = obj->Offset + obj->Length;
213
214 if (!(end <= obj->Offset || offset >= mapEnd)) {
215 return true;
216 }
217 }
218 return false;
219 }
220
221
222 /**
223 * Tests the subdata range parameters and sets the GL error code for
224 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
225 * \c glClearBufferSubData.
226 *
227 * \param ctx GL context.
228 * \param target Buffer object target on which to operate.
229 * \param offset Offset of the first byte of the subdata range.
230 * \param size Size, in bytes, of the subdata range.
231 * \param mappedRange If true, checks if an overlapping range is mapped.
232 * If false, checks if buffer is mapped.
233 * \param errorNoBuffer Error code if no buffer is bound to target.
234 * \param caller Name of calling function for recording errors.
235 * \return A pointer to the buffer object bound to \c target in the
236 * specified context or \c NULL if any of the parameter or state
237 * conditions are invalid.
238 *
239 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
240 */
241 static struct gl_buffer_object *
242 buffer_object_subdata_range_good(struct gl_context * ctx, GLenum target,
243 GLintptrARB offset, GLsizeiptrARB size,
244 bool mappedRange, GLenum errorNoBuffer,
245 const char *caller)
246 {
247 struct gl_buffer_object *bufObj;
248
249 if (size < 0) {
250 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
251 return NULL;
252 }
253
254 if (offset < 0) {
255 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
256 return NULL;
257 }
258
259 bufObj = get_buffer(ctx, caller, target, errorNoBuffer);
260 if (!bufObj)
261 return NULL;
262
263 if (offset + size > bufObj->Size) {
264 _mesa_error(ctx, GL_INVALID_VALUE,
265 "%s(offset %lu + size %lu > buffer size %lu)", caller,
266 (unsigned long) offset,
267 (unsigned long) size,
268 (unsigned long) bufObj->Size);
269 return NULL;
270 }
271
272 if (mappedRange) {
273 if (bufferobj_range_mapped(bufObj, offset, size)) {
274 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
275 return NULL;
276 }
277 }
278 else {
279 if (_mesa_bufferobj_mapped(bufObj)) {
280 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
281 return NULL;
282 }
283 }
284
285 return bufObj;
286 }
287
288
289 /**
290 * Test the format and type parameters and set the GL error code for
291 * \c glClearBufferData and \c glClearBufferSubData.
292 *
293 * \param ctx GL context.
294 * \param internalformat Format to which the data is to be converted.
295 * \param format Format of the supplied data.
296 * \param type Type of the supplied data.
297 * \param caller Name of calling function for recording errors.
298 * \return If internalformat, format and type are legal the mesa_format
299 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
300 *
301 * \sa glClearBufferData and glClearBufferSubData
302 */
303 static mesa_format
304 validate_clear_buffer_format(struct gl_context *ctx,
305 GLenum internalformat,
306 GLenum format, GLenum type,
307 const char *caller)
308 {
309 mesa_format mesaFormat;
310 GLenum errorFormatType;
311
312 mesaFormat = _mesa_validate_texbuffer_format(ctx, internalformat);
313 if (mesaFormat == MESA_FORMAT_NONE) {
314 _mesa_error(ctx, GL_INVALID_ENUM,
315 "%s(invalid internalformat)", caller);
316 return MESA_FORMAT_NONE;
317 }
318
319 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
320 * EXT_texture_integer there is no conversion between integer and
321 * non-integer formats
322 */
323 if (_mesa_is_enum_format_signed_int(format) !=
324 _mesa_is_format_integer_color(mesaFormat)) {
325 _mesa_error(ctx, GL_INVALID_OPERATION,
326 "%s(integer vs non-integer)", caller);
327 return MESA_FORMAT_NONE;
328 }
329
330 if (!_mesa_is_color_format(format)) {
331 _mesa_error(ctx, GL_INVALID_ENUM,
332 "%s(format is not a color format)", caller);
333 return MESA_FORMAT_NONE;
334 }
335
336 errorFormatType = _mesa_error_check_format_and_type(ctx, format, type);
337 if (errorFormatType != GL_NO_ERROR) {
338 _mesa_error(ctx, GL_INVALID_ENUM,
339 "%s(invalid format or type)", caller);
340 return MESA_FORMAT_NONE;
341 }
342
343 return mesaFormat;
344 }
345
346
347 /**
348 * Convert user-specified clear value to the specified internal format.
349 *
350 * \param ctx GL context.
351 * \param internalformat Format to which the data is converted.
352 * \param clearValue Points to the converted clear value.
353 * \param format Format of the supplied data.
354 * \param type Type of the supplied data.
355 * \param data Data which is to be converted to internalformat.
356 * \param caller Name of calling function for recording errors.
357 * \return true if data could be converted, false otherwise.
358 *
359 * \sa glClearBufferData, glClearBufferSubData
360 */
361 static bool
362 convert_clear_buffer_data(struct gl_context *ctx,
363 mesa_format internalformat,
364 GLubyte *clearValue, GLenum format, GLenum type,
365 const GLvoid *data, const char *caller)
366 {
367 GLenum internalformatBase = _mesa_get_format_base_format(internalformat);
368
369 if (_mesa_texstore(ctx, 1, internalformatBase, internalformat,
370 0, &clearValue, 1, 1, 1,
371 format, type, data, &ctx->Unpack)) {
372 return true;
373 }
374 else {
375 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
376 return false;
377 }
378 }
379
380
381 /**
382 * Allocate and initialize a new buffer object.
383 *
384 * Default callback for the \c dd_function_table::NewBufferObject() hook.
385 */
386 static struct gl_buffer_object *
387 _mesa_new_buffer_object( struct gl_context *ctx, GLuint name, GLenum target )
388 {
389 struct gl_buffer_object *obj;
390
391 (void) ctx;
392
393 obj = MALLOC_STRUCT(gl_buffer_object);
394 _mesa_initialize_buffer_object(ctx, obj, name, target);
395 return obj;
396 }
397
398
399 /**
400 * Delete a buffer object.
401 *
402 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
403 */
404 static void
405 _mesa_delete_buffer_object(struct gl_context *ctx,
406 struct gl_buffer_object *bufObj)
407 {
408 (void) ctx;
409
410 _mesa_align_free(bufObj->Data);
411
412 /* assign strange values here to help w/ debugging */
413 bufObj->RefCount = -1000;
414 bufObj->Name = ~0;
415
416 _glthread_DESTROY_MUTEX(bufObj->Mutex);
417 free(bufObj->Label);
418 free(bufObj);
419 }
420
421
422
423 /**
424 * Set ptr to bufObj w/ reference counting.
425 * This is normally only called from the _mesa_reference_buffer_object() macro
426 * when there's a real pointer change.
427 */
428 void
429 _mesa_reference_buffer_object_(struct gl_context *ctx,
430 struct gl_buffer_object **ptr,
431 struct gl_buffer_object *bufObj)
432 {
433 if (*ptr) {
434 /* Unreference the old buffer */
435 GLboolean deleteFlag = GL_FALSE;
436 struct gl_buffer_object *oldObj = *ptr;
437
438 _glthread_LOCK_MUTEX(oldObj->Mutex);
439 ASSERT(oldObj->RefCount > 0);
440 oldObj->RefCount--;
441 #if 0
442 printf("BufferObj %p %d DECR to %d\n",
443 (void *) oldObj, oldObj->Name, oldObj->RefCount);
444 #endif
445 deleteFlag = (oldObj->RefCount == 0);
446 _glthread_UNLOCK_MUTEX(oldObj->Mutex);
447
448 if (deleteFlag) {
449
450 /* some sanity checking: don't delete a buffer still in use */
451 #if 0
452 /* unfortunately, these tests are invalid during context tear-down */
453 ASSERT(ctx->Array.ArrayBufferObj != bufObj);
454 ASSERT(ctx->Array.VAO->IndexBufferObj != bufObj);
455 ASSERT(ctx->Array.VAO->Vertex.BufferObj != bufObj);
456 #endif
457
458 ASSERT(ctx->Driver.DeleteBuffer);
459 ctx->Driver.DeleteBuffer(ctx, oldObj);
460 }
461
462 *ptr = NULL;
463 }
464 ASSERT(!*ptr);
465
466 if (bufObj) {
467 /* reference new buffer */
468 _glthread_LOCK_MUTEX(bufObj->Mutex);
469 if (bufObj->RefCount == 0) {
470 /* this buffer's being deleted (look just above) */
471 /* Not sure this can every really happen. Warn if it does. */
472 _mesa_problem(NULL, "referencing deleted buffer object");
473 *ptr = NULL;
474 }
475 else {
476 bufObj->RefCount++;
477 #if 0
478 printf("BufferObj %p %d INCR to %d\n",
479 (void *) bufObj, bufObj->Name, bufObj->RefCount);
480 #endif
481 *ptr = bufObj;
482 }
483 _glthread_UNLOCK_MUTEX(bufObj->Mutex);
484 }
485 }
486
487
488 /**
489 * Initialize a buffer object to default values.
490 */
491 void
492 _mesa_initialize_buffer_object( struct gl_context *ctx,
493 struct gl_buffer_object *obj,
494 GLuint name, GLenum target )
495 {
496 (void) target;
497
498 memset(obj, 0, sizeof(struct gl_buffer_object));
499 _glthread_INIT_MUTEX(obj->Mutex);
500 obj->RefCount = 1;
501 obj->Name = name;
502 obj->Usage = GL_STATIC_DRAW_ARB;
503 obj->AccessFlags = 0;
504 }
505
506
507
508 /**
509 * Callback called from _mesa_HashWalk()
510 */
511 static void
512 count_buffer_size(GLuint key, void *data, void *userData)
513 {
514 const struct gl_buffer_object *bufObj =
515 (const struct gl_buffer_object *) data;
516 GLuint *total = (GLuint *) userData;
517
518 *total = *total + bufObj->Size;
519 }
520
521
522 /**
523 * Compute total size (in bytes) of all buffer objects for the given context.
524 * For debugging purposes.
525 */
526 GLuint
527 _mesa_total_buffer_object_memory(struct gl_context *ctx)
528 {
529 GLuint total = 0;
530
531 _mesa_HashWalk(ctx->Shared->BufferObjects, count_buffer_size, &total);
532
533 return total;
534 }
535
536
537 /**
538 * Allocate space for and store data in a buffer object. Any data that was
539 * previously stored in the buffer object is lost. If \c data is \c NULL,
540 * memory will be allocated, but no copy will occur.
541 *
542 * This is the default callback for \c dd_function_table::BufferData()
543 * Note that all GL error checking will have been done already.
544 *
545 * \param ctx GL context.
546 * \param target Buffer object target on which to operate.
547 * \param size Size, in bytes, of the new data store.
548 * \param data Pointer to the data to store in the buffer object. This
549 * pointer may be \c NULL.
550 * \param usage Hints about how the data will be used.
551 * \param bufObj Object to be used.
552 *
553 * \return GL_TRUE for success, GL_FALSE for failure
554 * \sa glBufferDataARB, dd_function_table::BufferData.
555 */
556 static GLboolean
557 _mesa_buffer_data( struct gl_context *ctx, GLenum target, GLsizeiptrARB size,
558 const GLvoid * data, GLenum usage, GLenum storageFlags,
559 struct gl_buffer_object * bufObj )
560 {
561 void * new_data;
562
563 (void) target;
564
565 _mesa_align_free( bufObj->Data );
566
567 new_data = _mesa_align_malloc( size, ctx->Const.MinMapBufferAlignment );
568 if (new_data) {
569 bufObj->Data = (GLubyte *) new_data;
570 bufObj->Size = size;
571 bufObj->Usage = usage;
572 bufObj->StorageFlags = storageFlags;
573
574 if (data) {
575 memcpy( bufObj->Data, data, size );
576 }
577
578 return GL_TRUE;
579 }
580 else {
581 return GL_FALSE;
582 }
583 }
584
585
586 /**
587 * Replace data in a subrange of buffer object. If the data range
588 * specified by \c size + \c offset extends beyond the end of the buffer or
589 * if \c data is \c NULL, no copy is performed.
590 *
591 * This is the default callback for \c dd_function_table::BufferSubData()
592 * Note that all GL error checking will have been done already.
593 *
594 * \param ctx GL context.
595 * \param offset Offset of the first byte to be modified.
596 * \param size Size, in bytes, of the data range.
597 * \param data Pointer to the data to store in the buffer object.
598 * \param bufObj Object to be used.
599 *
600 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
601 */
602 static void
603 _mesa_buffer_subdata( struct gl_context *ctx, GLintptrARB offset,
604 GLsizeiptrARB size, const GLvoid * data,
605 struct gl_buffer_object * bufObj )
606 {
607 (void) ctx;
608
609 /* this should have been caught in _mesa_BufferSubData() */
610 ASSERT(size + offset <= bufObj->Size);
611
612 if (bufObj->Data) {
613 memcpy( (GLubyte *) bufObj->Data + offset, data, size );
614 }
615 }
616
617
618 /**
619 * Retrieve data from a subrange of buffer object. If the data range
620 * specified by \c size + \c offset extends beyond the end of the buffer or
621 * if \c data is \c NULL, no copy is performed.
622 *
623 * This is the default callback for \c dd_function_table::GetBufferSubData()
624 * Note that all GL error checking will have been done already.
625 *
626 * \param ctx GL context.
627 * \param target Buffer object target on which to operate.
628 * \param offset Offset of the first byte to be fetched.
629 * \param size Size, in bytes, of the data range.
630 * \param data Destination for data
631 * \param bufObj Object to be used.
632 *
633 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
634 */
635 static void
636 _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset,
637 GLsizeiptrARB size, GLvoid * data,
638 struct gl_buffer_object * bufObj )
639 {
640 (void) ctx;
641
642 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
643 memcpy( data, (GLubyte *) bufObj->Data + offset, size );
644 }
645 }
646
647
648 /**
649 * Clear a subrange of the buffer object with copies of the supplied data.
650 * If data is NULL the buffer is filled with zeros.
651 *
652 * This is the default callback for \c dd_function_table::ClearBufferSubData()
653 * Note that all GL error checking will have been done already.
654 *
655 * \param ctx GL context.
656 * \param offset Offset of the first byte to be cleared.
657 * \param size Size, in bytes, of the to be cleared range.
658 * \param clearValue Source of the data.
659 * \param clearValueSize Size, in bytes, of the supplied data.
660 * \param bufObj Object to be cleared.
661 *
662 * \sa glClearBufferSubData, glClearBufferData and
663 * dd_function_table::ClearBufferSubData.
664 */
665 static void
666 _mesa_buffer_clear_subdata(struct gl_context *ctx,
667 GLintptr offset, GLsizeiptr size,
668 const GLvoid *clearValue,
669 GLsizeiptr clearValueSize,
670 struct gl_buffer_object *bufObj)
671 {
672 GLsizeiptr i;
673 GLubyte *dest;
674
675 if (_mesa_bufferobj_mapped(bufObj)) {
676 GLubyte *data = malloc(size);
677 GLubyte *dataStart = data;
678 if (data == NULL) {
679 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearBuffer[Sub]Data");
680 return;
681 }
682
683 if (clearValue == NULL) {
684 /* Clear with zeros, per the spec */
685 memset(data, 0, size);
686 }
687 else {
688 for (i = 0; i < size/clearValueSize; ++i) {
689 memcpy(data, clearValue, clearValueSize);
690 data += clearValueSize;
691 }
692 }
693 ctx->Driver.BufferSubData(ctx, offset, size, dataStart, bufObj);
694 return;
695 }
696
697 ASSERT(ctx->Driver.MapBufferRange);
698 dest = ctx->Driver.MapBufferRange(ctx, offset, size,
699 GL_MAP_WRITE_BIT |
700 GL_MAP_INVALIDATE_RANGE_BIT,
701 bufObj);
702
703 if (!dest) {
704 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearBuffer[Sub]Data");
705 return;
706 }
707
708 if (clearValue == NULL) {
709 /* Clear with zeros, per the spec */
710 memset(dest, 0, size);
711 ctx->Driver.UnmapBuffer(ctx, bufObj);
712 return;
713 }
714
715 for (i = 0; i < size/clearValueSize; ++i) {
716 memcpy(dest, clearValue, clearValueSize);
717 dest += clearValueSize;
718 }
719
720 ctx->Driver.UnmapBuffer(ctx, bufObj);
721 }
722
723
724 /**
725 * Default fallback for \c dd_function_table::MapBufferRange().
726 * Called via glMapBufferRange().
727 */
728 static void *
729 _mesa_buffer_map_range( struct gl_context *ctx, GLintptr offset,
730 GLsizeiptr length, GLbitfield access,
731 struct gl_buffer_object *bufObj )
732 {
733 (void) ctx;
734 assert(!_mesa_bufferobj_mapped(bufObj));
735 /* Just return a direct pointer to the data */
736 bufObj->Pointer = bufObj->Data + offset;
737 bufObj->Length = length;
738 bufObj->Offset = offset;
739 bufObj->AccessFlags = access;
740 return bufObj->Pointer;
741 }
742
743
744 /**
745 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
746 * Called via glFlushMappedBufferRange().
747 */
748 static void
749 _mesa_buffer_flush_mapped_range( struct gl_context *ctx,
750 GLintptr offset, GLsizeiptr length,
751 struct gl_buffer_object *obj )
752 {
753 (void) ctx;
754 (void) offset;
755 (void) length;
756 (void) obj;
757 /* no-op */
758 }
759
760
761 /**
762 * Default callback for \c dd_function_table::MapBuffer().
763 *
764 * The input parameters will have been already tested for errors.
765 *
766 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
767 */
768 static GLboolean
769 _mesa_buffer_unmap( struct gl_context *ctx, struct gl_buffer_object *bufObj )
770 {
771 (void) ctx;
772 /* XXX we might assert here that bufObj->Pointer is non-null */
773 bufObj->Pointer = NULL;
774 bufObj->Length = 0;
775 bufObj->Offset = 0;
776 bufObj->AccessFlags = 0x0;
777 return GL_TRUE;
778 }
779
780
781 /**
782 * Default fallback for \c dd_function_table::CopyBufferSubData().
783 * Called via glCopyBufferSubData().
784 */
785 static void
786 _mesa_copy_buffer_subdata(struct gl_context *ctx,
787 struct gl_buffer_object *src,
788 struct gl_buffer_object *dst,
789 GLintptr readOffset, GLintptr writeOffset,
790 GLsizeiptr size)
791 {
792 GLubyte *srcPtr, *dstPtr;
793
794 /* the buffers should not be mapped */
795 assert(!_mesa_bufferobj_mapped(src));
796 assert(!_mesa_bufferobj_mapped(dst));
797
798 if (src == dst) {
799 srcPtr = dstPtr = ctx->Driver.MapBufferRange(ctx, 0, src->Size,
800 GL_MAP_READ_BIT |
801 GL_MAP_WRITE_BIT, src);
802
803 if (!srcPtr)
804 return;
805
806 srcPtr += readOffset;
807 dstPtr += writeOffset;
808 } else {
809 srcPtr = ctx->Driver.MapBufferRange(ctx, readOffset, size,
810 GL_MAP_READ_BIT, src);
811 dstPtr = ctx->Driver.MapBufferRange(ctx, writeOffset, size,
812 (GL_MAP_WRITE_BIT |
813 GL_MAP_INVALIDATE_RANGE_BIT), dst);
814 }
815
816 /* Note: the src and dst regions will never overlap. Trying to do so
817 * would generate GL_INVALID_VALUE earlier.
818 */
819 if (srcPtr && dstPtr)
820 memcpy(dstPtr, srcPtr, size);
821
822 ctx->Driver.UnmapBuffer(ctx, src);
823 if (dst != src)
824 ctx->Driver.UnmapBuffer(ctx, dst);
825 }
826
827
828
829 /**
830 * Initialize the state associated with buffer objects
831 */
832 void
833 _mesa_init_buffer_objects( struct gl_context *ctx )
834 {
835 GLuint i;
836
837 memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
838 _glthread_INIT_MUTEX(DummyBufferObject.Mutex);
839 DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
840
841 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
842 ctx->Shared->NullBufferObj);
843
844 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
845 ctx->Shared->NullBufferObj);
846 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
847 ctx->Shared->NullBufferObj);
848
849 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer,
850 ctx->Shared->NullBufferObj);
851
852 _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer,
853 ctx->Shared->NullBufferObj);
854
855 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
856 _mesa_reference_buffer_object(ctx,
857 &ctx->UniformBufferBindings[i].BufferObject,
858 ctx->Shared->NullBufferObj);
859 ctx->UniformBufferBindings[i].Offset = -1;
860 ctx->UniformBufferBindings[i].Size = -1;
861 }
862 }
863
864
865 void
866 _mesa_free_buffer_objects( struct gl_context *ctx )
867 {
868 GLuint i;
869
870 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
871
872 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
873 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
874
875 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, 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
886 bool
887 _mesa_handle_bind_buffer_gen(struct gl_context *ctx,
888 GLenum target,
889 GLuint buffer,
890 struct gl_buffer_object **buf_handle,
891 const char *caller)
892 {
893 struct gl_buffer_object *buf = *buf_handle;
894
895 if (!buf && ctx->API == API_OPENGL_CORE) {
896 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", caller);
897 return false;
898 }
899
900 if (!buf || buf == &DummyBufferObject) {
901 /* If this is a new buffer object id, or one which was generated but
902 * never used before, allocate a buffer object now.
903 */
904 ASSERT(ctx->Driver.NewBufferObject);
905 buf = ctx->Driver.NewBufferObject(ctx, buffer, target);
906 if (!buf) {
907 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
908 return false;
909 }
910 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf);
911 *buf_handle = buf;
912 }
913
914 return true;
915 }
916
917 /**
918 * Bind the specified target to buffer for the specified context.
919 * Called by glBindBuffer() and other functions.
920 */
921 static void
922 bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
923 {
924 struct gl_buffer_object *oldBufObj;
925 struct gl_buffer_object *newBufObj = NULL;
926 struct gl_buffer_object **bindTarget = NULL;
927
928 bindTarget = get_buffer_target(ctx, target);
929 if (!bindTarget) {
930 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
931 return;
932 }
933
934 /* Get pointer to old buffer object (to be unbound) */
935 oldBufObj = *bindTarget;
936 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
937 return; /* rebinding the same buffer object- no change */
938
939 /*
940 * Get pointer to new buffer object (newBufObj)
941 */
942 if (buffer == 0) {
943 /* The spec says there's not a buffer object named 0, but we use
944 * one internally because it simplifies things.
945 */
946 newBufObj = ctx->Shared->NullBufferObj;
947 }
948 else {
949 /* non-default buffer object */
950 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
951 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
952 &newBufObj, "glBindBuffer"))
953 return;
954 }
955
956 /* bind new buffer */
957 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
958 }
959
960
961 /**
962 * Update the default buffer objects in the given context to reference those
963 * specified in the shared state and release those referencing the old
964 * shared state.
965 */
966 void
967 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
968 {
969 /* Bind the NullBufferObj to remove references to those
970 * in the shared context hash table.
971 */
972 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
973 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
974 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
975 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
976 }
977
978
979
980 /**
981 * Return the gl_buffer_object for the given ID.
982 * Always return NULL for ID 0.
983 */
984 struct gl_buffer_object *
985 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
986 {
987 if (buffer == 0)
988 return NULL;
989 else
990 return (struct gl_buffer_object *)
991 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
992 }
993
994
995 /**
996 * If *ptr points to obj, set ptr = the Null/default buffer object.
997 * This is a helper for buffer object deletion.
998 * The GL spec says that deleting a buffer object causes it to get
999 * unbound from all arrays in the current context.
1000 */
1001 static void
1002 unbind(struct gl_context *ctx,
1003 struct gl_buffer_object **ptr,
1004 struct gl_buffer_object *obj)
1005 {
1006 if (*ptr == obj) {
1007 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
1008 }
1009 }
1010
1011
1012 /**
1013 * Plug default/fallback buffer object functions into the device
1014 * driver hooks.
1015 */
1016 void
1017 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
1018 {
1019 /* GL_ARB_vertex/pixel_buffer_object */
1020 driver->NewBufferObject = _mesa_new_buffer_object;
1021 driver->DeleteBuffer = _mesa_delete_buffer_object;
1022 driver->BufferData = _mesa_buffer_data;
1023 driver->BufferSubData = _mesa_buffer_subdata;
1024 driver->GetBufferSubData = _mesa_buffer_get_subdata;
1025 driver->UnmapBuffer = _mesa_buffer_unmap;
1026
1027 /* GL_ARB_clear_buffer_object */
1028 driver->ClearBufferSubData = _mesa_buffer_clear_subdata;
1029
1030 /* GL_ARB_map_buffer_range */
1031 driver->MapBufferRange = _mesa_buffer_map_range;
1032 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
1033
1034 /* GL_ARB_copy_buffer */
1035 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
1036 }
1037
1038
1039
1040 /**********************************************************************/
1041 /* API Functions */
1042 /**********************************************************************/
1043
1044 void GLAPIENTRY
1045 _mesa_BindBuffer(GLenum target, GLuint buffer)
1046 {
1047 GET_CURRENT_CONTEXT(ctx);
1048
1049 if (MESA_VERBOSE & VERBOSE_API)
1050 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
1051 _mesa_lookup_enum_by_nr(target), buffer);
1052
1053 bind_buffer_object(ctx, target, buffer);
1054 }
1055
1056
1057 /**
1058 * Delete a set of buffer objects.
1059 *
1060 * \param n Number of buffer objects to delete.
1061 * \param ids Array of \c n buffer object IDs.
1062 */
1063 void GLAPIENTRY
1064 _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
1065 {
1066 GET_CURRENT_CONTEXT(ctx);
1067 GLsizei i;
1068 FLUSH_VERTICES(ctx, 0);
1069
1070 if (n < 0) {
1071 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
1072 return;
1073 }
1074
1075 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1076
1077 for (i = 0; i < n; i++) {
1078 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
1079 if (bufObj) {
1080 struct gl_vertex_array_object *vao = ctx->Array.VAO;
1081 GLuint j;
1082
1083 ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
1084
1085 if (_mesa_bufferobj_mapped(bufObj)) {
1086 /* if mapped, unmap it now */
1087 ctx->Driver.UnmapBuffer(ctx, bufObj);
1088 bufObj->AccessFlags = 0;
1089 bufObj->Pointer = NULL;
1090 }
1091
1092 /* unbind any vertex pointers bound to this buffer */
1093 for (j = 0; j < Elements(vao->VertexBinding); j++) {
1094 unbind(ctx, &vao->VertexBinding[j].BufferObj, bufObj);
1095 }
1096
1097 if (ctx->Array.ArrayBufferObj == bufObj) {
1098 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
1099 }
1100 if (vao->IndexBufferObj == bufObj) {
1101 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
1102 }
1103
1104 /* unbind ARB_draw_indirect binding point */
1105 if (ctx->DrawIndirectBuffer == bufObj) {
1106 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER, 0 );
1107 }
1108
1109 /* unbind ARB_copy_buffer binding points */
1110 if (ctx->CopyReadBuffer == bufObj) {
1111 _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
1112 }
1113 if (ctx->CopyWriteBuffer == bufObj) {
1114 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
1115 }
1116
1117 /* unbind transform feedback binding points */
1118 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
1119 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
1120 }
1121 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
1122 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
1123 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
1124 }
1125 }
1126
1127 /* unbind UBO binding points */
1128 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
1129 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
1130 _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
1131 }
1132 }
1133
1134 if (ctx->UniformBuffer == bufObj) {
1135 _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
1136 }
1137
1138 /* unbind any pixel pack/unpack pointers bound to this buffer */
1139 if (ctx->Pack.BufferObj == bufObj) {
1140 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
1141 }
1142 if (ctx->Unpack.BufferObj == bufObj) {
1143 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
1144 }
1145
1146 if (ctx->Texture.BufferObject == bufObj) {
1147 _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
1148 }
1149
1150 /* The ID is immediately freed for re-use */
1151 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
1152 /* Make sure we do not run into the classic ABA problem on bind.
1153 * We don't want to allow re-binding a buffer object that's been
1154 * "deleted" by glDeleteBuffers().
1155 *
1156 * The explicit rebinding to the default object in the current context
1157 * prevents the above in the current context, but another context
1158 * sharing the same objects might suffer from this problem.
1159 * The alternative would be to do the hash lookup in any case on bind
1160 * which would introduce more runtime overhead than this.
1161 */
1162 bufObj->DeletePending = GL_TRUE;
1163 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
1164 }
1165 }
1166
1167 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1168 }
1169
1170
1171 /**
1172 * Generate a set of unique buffer object IDs and store them in \c buffer.
1173 *
1174 * \param n Number of IDs to generate.
1175 * \param buffer Array of \c n locations to store the IDs.
1176 */
1177 void GLAPIENTRY
1178 _mesa_GenBuffers(GLsizei n, GLuint *buffer)
1179 {
1180 GET_CURRENT_CONTEXT(ctx);
1181 GLuint first;
1182 GLint i;
1183
1184 if (MESA_VERBOSE & VERBOSE_API)
1185 _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
1186
1187 if (n < 0) {
1188 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
1189 return;
1190 }
1191
1192 if (!buffer) {
1193 return;
1194 }
1195
1196 /*
1197 * This must be atomic (generation and allocation of buffer object IDs)
1198 */
1199 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1200
1201 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
1202
1203 /* Insert the ID and pointer to dummy buffer object into hash table */
1204 for (i = 0; i < n; i++) {
1205 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
1206 &DummyBufferObject);
1207 buffer[i] = first + i;
1208 }
1209
1210 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1211 }
1212
1213
1214 /**
1215 * Determine if ID is the name of a buffer object.
1216 *
1217 * \param id ID of the potential buffer object.
1218 * \return \c GL_TRUE if \c id is the name of a buffer object,
1219 * \c GL_FALSE otherwise.
1220 */
1221 GLboolean GLAPIENTRY
1222 _mesa_IsBuffer(GLuint id)
1223 {
1224 struct gl_buffer_object *bufObj;
1225 GET_CURRENT_CONTEXT(ctx);
1226 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1227
1228 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1229 bufObj = _mesa_lookup_bufferobj(ctx, id);
1230 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1231
1232 return bufObj && bufObj != &DummyBufferObject;
1233 }
1234
1235
1236 void GLAPIENTRY
1237 _mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data,
1238 GLbitfield flags)
1239 {
1240 GET_CURRENT_CONTEXT(ctx);
1241 struct gl_buffer_object *bufObj;
1242
1243 if (size <= 0) {
1244 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(size <= 0)");
1245 return;
1246 }
1247
1248 if (flags & ~(GL_MAP_READ_BIT |
1249 GL_MAP_WRITE_BIT |
1250 GL_MAP_PERSISTENT_BIT |
1251 GL_MAP_COHERENT_BIT |
1252 GL_DYNAMIC_STORAGE_BIT |
1253 GL_CLIENT_STORAGE_BIT)) {
1254 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(flags)");
1255 return;
1256 }
1257
1258 if (flags & GL_MAP_PERSISTENT_BIT &&
1259 !(flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))) {
1260 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(flags!=READ/WRITE)");
1261 return;
1262 }
1263
1264 if (flags & GL_MAP_COHERENT_BIT && !(flags & GL_MAP_PERSISTENT_BIT)) {
1265 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(flags!=PERSISTENT)");
1266 return;
1267 }
1268
1269 bufObj = get_buffer(ctx, "glBufferStorage", target, GL_INVALID_OPERATION);
1270 if (!bufObj)
1271 return;
1272
1273 if (bufObj->Immutable) {
1274 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferStorage(immutable)");
1275 return;
1276 }
1277
1278 if (_mesa_bufferobj_mapped(bufObj)) {
1279 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1280 ctx->Driver.UnmapBuffer(ctx, bufObj);
1281 bufObj->AccessFlags = 0;
1282 ASSERT(bufObj->Pointer == NULL);
1283 }
1284
1285 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1286
1287 bufObj->Written = GL_TRUE;
1288 bufObj->Immutable = GL_TRUE;
1289
1290 ASSERT(ctx->Driver.BufferData);
1291 if (!ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
1292 flags, bufObj)) {
1293 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferStorage()");
1294 }
1295 }
1296
1297
1298 void GLAPIENTRY
1299 _mesa_BufferData(GLenum target, GLsizeiptrARB size,
1300 const GLvoid * data, GLenum usage)
1301 {
1302 GET_CURRENT_CONTEXT(ctx);
1303 struct gl_buffer_object *bufObj;
1304 bool valid_usage;
1305
1306 if (MESA_VERBOSE & VERBOSE_API)
1307 _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
1308 _mesa_lookup_enum_by_nr(target),
1309 (long int) size, data,
1310 _mesa_lookup_enum_by_nr(usage));
1311
1312 if (size < 0) {
1313 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
1314 return;
1315 }
1316
1317 switch (usage) {
1318 case GL_STREAM_DRAW_ARB:
1319 valid_usage = (ctx->API != API_OPENGLES);
1320 break;
1321
1322 case GL_STATIC_DRAW_ARB:
1323 case GL_DYNAMIC_DRAW_ARB:
1324 valid_usage = true;
1325 break;
1326
1327 case GL_STREAM_READ_ARB:
1328 case GL_STREAM_COPY_ARB:
1329 case GL_STATIC_READ_ARB:
1330 case GL_STATIC_COPY_ARB:
1331 case GL_DYNAMIC_READ_ARB:
1332 case GL_DYNAMIC_COPY_ARB:
1333 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1334 break;
1335
1336 default:
1337 valid_usage = false;
1338 break;
1339 }
1340
1341 if (!valid_usage) {
1342 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferData(usage)");
1343 return;
1344 }
1345
1346 bufObj = get_buffer(ctx, "glBufferDataARB", target, GL_INVALID_OPERATION);
1347 if (!bufObj)
1348 return;
1349
1350 if (bufObj->Immutable) {
1351 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferData(immutable)");
1352 return;
1353 }
1354
1355 if (_mesa_bufferobj_mapped(bufObj)) {
1356 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1357 ctx->Driver.UnmapBuffer(ctx, bufObj);
1358 bufObj->AccessFlags = 0;
1359 ASSERT(bufObj->Pointer == NULL);
1360 }
1361
1362 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1363
1364 bufObj->Written = GL_TRUE;
1365
1366 #ifdef VBO_DEBUG
1367 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1368 bufObj->Name, size, data, usage);
1369 #endif
1370
1371 #ifdef BOUNDS_CHECK
1372 size += 100;
1373 #endif
1374
1375 ASSERT(ctx->Driver.BufferData);
1376 if (!ctx->Driver.BufferData(ctx, target, size, data, usage,
1377 GL_MAP_READ_BIT |
1378 GL_MAP_WRITE_BIT |
1379 GL_DYNAMIC_STORAGE_BIT,
1380 bufObj)) {
1381 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
1382 }
1383 }
1384
1385
1386 void GLAPIENTRY
1387 _mesa_BufferSubData(GLenum target, GLintptrARB offset,
1388 GLsizeiptrARB size, const GLvoid * data)
1389 {
1390 GET_CURRENT_CONTEXT(ctx);
1391 struct gl_buffer_object *bufObj;
1392
1393 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1394 false, GL_INVALID_OPERATION,
1395 "glBufferSubDataARB" );
1396 if (!bufObj) {
1397 /* error already recorded */
1398 return;
1399 }
1400
1401 if (bufObj->Immutable &&
1402 !(bufObj->StorageFlags & GL_DYNAMIC_STORAGE_BIT)) {
1403 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferSubData");
1404 return;
1405 }
1406
1407 if (size == 0)
1408 return;
1409
1410 bufObj->Written = GL_TRUE;
1411
1412 ASSERT(ctx->Driver.BufferSubData);
1413 ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
1414 }
1415
1416
1417 void GLAPIENTRY
1418 _mesa_GetBufferSubData(GLenum target, GLintptrARB offset,
1419 GLsizeiptrARB size, void * data)
1420 {
1421 GET_CURRENT_CONTEXT(ctx);
1422 struct gl_buffer_object *bufObj;
1423
1424 bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
1425 false, GL_INVALID_OPERATION,
1426 "glGetBufferSubDataARB");
1427 if (!bufObj) {
1428 /* error already recorded */
1429 return;
1430 }
1431
1432 ASSERT(ctx->Driver.GetBufferSubData);
1433 ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
1434 }
1435
1436
1437 void GLAPIENTRY
1438 _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
1439 GLenum type, const GLvoid* data)
1440 {
1441 GET_CURRENT_CONTEXT(ctx);
1442 struct gl_buffer_object* bufObj;
1443 mesa_format mesaFormat;
1444 GLubyte clearValue[MAX_PIXEL_BYTES];
1445 GLsizeiptr clearValueSize;
1446
1447 bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
1448 if (!bufObj) {
1449 return;
1450 }
1451
1452 if (_mesa_bufferobj_mapped(bufObj)) {
1453 _mesa_error(ctx, GL_INVALID_OPERATION,
1454 "glClearBufferData(buffer currently mapped)");
1455 return;
1456 }
1457
1458 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1459 format, type,
1460 "glClearBufferData");
1461 if (mesaFormat == MESA_FORMAT_NONE) {
1462 return;
1463 }
1464
1465 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1466 if (bufObj->Size % clearValueSize != 0) {
1467 _mesa_error(ctx, GL_INVALID_VALUE,
1468 "glClearBufferData(size is not a multiple of "
1469 "internalformat size)");
1470 return;
1471 }
1472
1473 if (data == NULL) {
1474 /* clear to zeros, per the spec */
1475 ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
1476 NULL, 0, bufObj);
1477 return;
1478 }
1479
1480 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1481 format, type, data, "glClearBufferData")) {
1482 return;
1483 }
1484
1485 ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
1486 clearValue, clearValueSize, bufObj);
1487 }
1488
1489
1490 void GLAPIENTRY
1491 _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
1492 GLintptr offset, GLsizeiptr size,
1493 GLenum format, GLenum type,
1494 const GLvoid* data)
1495 {
1496 GET_CURRENT_CONTEXT(ctx);
1497 struct gl_buffer_object* bufObj;
1498 mesa_format mesaFormat;
1499 GLubyte clearValue[MAX_PIXEL_BYTES];
1500 GLsizeiptr clearValueSize;
1501
1502 bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
1503 true, GL_INVALID_VALUE,
1504 "glClearBufferSubData");
1505 if (!bufObj) {
1506 return;
1507 }
1508
1509 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1510 format, type,
1511 "glClearBufferSubData");
1512 if (mesaFormat == MESA_FORMAT_NONE) {
1513 return;
1514 }
1515
1516 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1517 if (offset % clearValueSize != 0 || size % clearValueSize != 0) {
1518 _mesa_error(ctx, GL_INVALID_VALUE,
1519 "glClearBufferSubData(offset or size is not a multiple of "
1520 "internalformat size)");
1521 return;
1522 }
1523
1524 if (data == NULL) {
1525 /* clear to zeros, per the spec */
1526 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1527 NULL, 0, bufObj);
1528 return;
1529 }
1530
1531 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1532 format, type, data,
1533 "glClearBufferSubData")) {
1534 return;
1535 }
1536
1537 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1538 clearValue, clearValueSize, bufObj);
1539 }
1540
1541
1542 void * GLAPIENTRY
1543 _mesa_MapBuffer(GLenum target, GLenum access)
1544 {
1545 GET_CURRENT_CONTEXT(ctx);
1546 struct gl_buffer_object * bufObj;
1547 GLbitfield accessFlags;
1548 void *map;
1549 bool valid_access;
1550
1551 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1552
1553 switch (access) {
1554 case GL_READ_ONLY_ARB:
1555 accessFlags = GL_MAP_READ_BIT;
1556 valid_access = _mesa_is_desktop_gl(ctx);
1557 break;
1558 case GL_WRITE_ONLY_ARB:
1559 accessFlags = GL_MAP_WRITE_BIT;
1560 valid_access = true;
1561 break;
1562 case GL_READ_WRITE_ARB:
1563 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1564 valid_access = _mesa_is_desktop_gl(ctx);
1565 break;
1566 default:
1567 valid_access = false;
1568 break;
1569 }
1570
1571 if (!valid_access) {
1572 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1573 return NULL;
1574 }
1575
1576 bufObj = get_buffer(ctx, "glMapBufferARB", target, GL_INVALID_OPERATION);
1577 if (!bufObj)
1578 return NULL;
1579
1580 if (accessFlags & GL_MAP_READ_BIT &&
1581 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
1582 _mesa_error(ctx, GL_INVALID_OPERATION,
1583 "glMapBuffer(invalid read flag)");
1584 return NULL;
1585 }
1586
1587 if (accessFlags & GL_MAP_WRITE_BIT &&
1588 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
1589 _mesa_error(ctx, GL_INVALID_OPERATION,
1590 "glMapBuffer(invalid write flag)");
1591 return NULL;
1592 }
1593
1594 if (_mesa_bufferobj_mapped(bufObj)) {
1595 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1596 return NULL;
1597 }
1598
1599 if (!bufObj->Size) {
1600 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1601 "glMapBuffer(buffer size = 0)");
1602 return NULL;
1603 }
1604
1605 ASSERT(ctx->Driver.MapBufferRange);
1606 map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj);
1607 if (!map) {
1608 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1609 return NULL;
1610 }
1611 else {
1612 /* The driver callback should have set these fields.
1613 * This is important because other modules (like VBO) might call
1614 * the driver function directly.
1615 */
1616 ASSERT(bufObj->Pointer == map);
1617 ASSERT(bufObj->Length == bufObj->Size);
1618 ASSERT(bufObj->Offset == 0);
1619 bufObj->AccessFlags = accessFlags;
1620 }
1621
1622 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1623 bufObj->Written = GL_TRUE;
1624
1625 #ifdef VBO_DEBUG
1626 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1627 bufObj->Name, bufObj->Size, access);
1628 if (access == GL_WRITE_ONLY_ARB) {
1629 GLuint i;
1630 GLubyte *b = (GLubyte *) bufObj->Pointer;
1631 for (i = 0; i < bufObj->Size; i++)
1632 b[i] = i & 0xff;
1633 }
1634 #endif
1635
1636 #ifdef BOUNDS_CHECK
1637 {
1638 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1639 GLuint i;
1640 /* buffer is 100 bytes larger than requested, fill with magic value */
1641 for (i = 0; i < 100; i++) {
1642 buf[bufObj->Size - i - 1] = 123;
1643 }
1644 }
1645 #endif
1646
1647 return bufObj->Pointer;
1648 }
1649
1650
1651 GLboolean GLAPIENTRY
1652 _mesa_UnmapBuffer(GLenum target)
1653 {
1654 GET_CURRENT_CONTEXT(ctx);
1655 struct gl_buffer_object *bufObj;
1656 GLboolean status = GL_TRUE;
1657 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1658
1659 bufObj = get_buffer(ctx, "glUnmapBufferARB", target, GL_INVALID_OPERATION);
1660 if (!bufObj)
1661 return GL_FALSE;
1662
1663 if (!_mesa_bufferobj_mapped(bufObj)) {
1664 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1665 return GL_FALSE;
1666 }
1667
1668 #ifdef BOUNDS_CHECK
1669 if (bufObj->Access != GL_READ_ONLY_ARB) {
1670 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1671 GLuint i;
1672 /* check that last 100 bytes are still = magic value */
1673 for (i = 0; i < 100; i++) {
1674 GLuint pos = bufObj->Size - i - 1;
1675 if (buf[pos] != 123) {
1676 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1677 " at position %d (value = %u)\n",
1678 pos, buf[pos]);
1679 }
1680 }
1681 }
1682 #endif
1683
1684 #ifdef VBO_DEBUG
1685 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1686 GLuint i, unchanged = 0;
1687 GLubyte *b = (GLubyte *) bufObj->Pointer;
1688 GLint pos = -1;
1689 /* check which bytes changed */
1690 for (i = 0; i < bufObj->Size - 1; i++) {
1691 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1692 unchanged++;
1693 if (pos == -1)
1694 pos = i;
1695 }
1696 }
1697 if (unchanged) {
1698 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1699 bufObj->Name, unchanged, bufObj->Size, pos);
1700 }
1701 }
1702 #endif
1703
1704 status = ctx->Driver.UnmapBuffer( ctx, bufObj );
1705 bufObj->AccessFlags = 0;
1706 ASSERT(bufObj->Pointer == NULL);
1707 ASSERT(bufObj->Offset == 0);
1708 ASSERT(bufObj->Length == 0);
1709
1710 return status;
1711 }
1712
1713
1714 void GLAPIENTRY
1715 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
1716 {
1717 GET_CURRENT_CONTEXT(ctx);
1718 struct gl_buffer_object *bufObj;
1719
1720 bufObj = get_buffer(ctx, "glGetBufferParameterivARB", target,
1721 GL_INVALID_OPERATION);
1722 if (!bufObj)
1723 return;
1724
1725 switch (pname) {
1726 case GL_BUFFER_SIZE_ARB:
1727 *params = (GLint) bufObj->Size;
1728 return;
1729 case GL_BUFFER_USAGE_ARB:
1730 *params = bufObj->Usage;
1731 return;
1732 case GL_BUFFER_ACCESS_ARB:
1733 *params = simplified_access_mode(ctx, bufObj->AccessFlags);
1734 return;
1735 case GL_BUFFER_MAPPED_ARB:
1736 *params = _mesa_bufferobj_mapped(bufObj);
1737 return;
1738 case GL_BUFFER_ACCESS_FLAGS:
1739 if (!ctx->Extensions.ARB_map_buffer_range)
1740 goto invalid_pname;
1741 *params = bufObj->AccessFlags;
1742 return;
1743 case GL_BUFFER_MAP_OFFSET:
1744 if (!ctx->Extensions.ARB_map_buffer_range)
1745 goto invalid_pname;
1746 *params = (GLint) bufObj->Offset;
1747 return;
1748 case GL_BUFFER_MAP_LENGTH:
1749 if (!ctx->Extensions.ARB_map_buffer_range)
1750 goto invalid_pname;
1751 *params = (GLint) bufObj->Length;
1752 return;
1753 case GL_BUFFER_IMMUTABLE_STORAGE:
1754 if (!ctx->Extensions.ARB_buffer_storage)
1755 goto invalid_pname;
1756 *params = bufObj->Immutable;
1757 return;
1758 case GL_BUFFER_STORAGE_FLAGS:
1759 if (!ctx->Extensions.ARB_buffer_storage)
1760 goto invalid_pname;
1761 *params = bufObj->StorageFlags;
1762 return;
1763 default:
1764 ; /* fall-through */
1765 }
1766
1767 invalid_pname:
1768 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1769 _mesa_lookup_enum_by_nr(pname));
1770 }
1771
1772
1773 /**
1774 * New in GL 3.2
1775 * This is pretty much a duplicate of GetBufferParameteriv() but the
1776 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1777 */
1778 void GLAPIENTRY
1779 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1780 {
1781 GET_CURRENT_CONTEXT(ctx);
1782 struct gl_buffer_object *bufObj;
1783
1784 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
1785 GL_INVALID_OPERATION);
1786 if (!bufObj)
1787 return;
1788
1789 switch (pname) {
1790 case GL_BUFFER_SIZE_ARB:
1791 *params = bufObj->Size;
1792 return;
1793 case GL_BUFFER_USAGE_ARB:
1794 *params = bufObj->Usage;
1795 return;
1796 case GL_BUFFER_ACCESS_ARB:
1797 *params = simplified_access_mode(ctx, bufObj->AccessFlags);
1798 return;
1799 case GL_BUFFER_ACCESS_FLAGS:
1800 if (!ctx->Extensions.ARB_map_buffer_range)
1801 goto invalid_pname;
1802 *params = bufObj->AccessFlags;
1803 return;
1804 case GL_BUFFER_MAPPED_ARB:
1805 *params = _mesa_bufferobj_mapped(bufObj);
1806 return;
1807 case GL_BUFFER_MAP_OFFSET:
1808 if (!ctx->Extensions.ARB_map_buffer_range)
1809 goto invalid_pname;
1810 *params = bufObj->Offset;
1811 return;
1812 case GL_BUFFER_MAP_LENGTH:
1813 if (!ctx->Extensions.ARB_map_buffer_range)
1814 goto invalid_pname;
1815 *params = bufObj->Length;
1816 return;
1817 case GL_BUFFER_IMMUTABLE_STORAGE:
1818 if (!ctx->Extensions.ARB_buffer_storage)
1819 goto invalid_pname;
1820 *params = bufObj->Immutable;
1821 return;
1822 case GL_BUFFER_STORAGE_FLAGS:
1823 if (!ctx->Extensions.ARB_buffer_storage)
1824 goto invalid_pname;
1825 *params = bufObj->StorageFlags;
1826 return;
1827 default:
1828 ; /* fall-through */
1829 }
1830
1831 invalid_pname:
1832 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1833 _mesa_lookup_enum_by_nr(pname));
1834 }
1835
1836
1837 void GLAPIENTRY
1838 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
1839 {
1840 GET_CURRENT_CONTEXT(ctx);
1841 struct gl_buffer_object * bufObj;
1842
1843 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1844 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1845 return;
1846 }
1847
1848 bufObj = get_buffer(ctx, "glGetBufferPointervARB", target,
1849 GL_INVALID_OPERATION);
1850 if (!bufObj)
1851 return;
1852
1853 *params = bufObj->Pointer;
1854 }
1855
1856
1857 void GLAPIENTRY
1858 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1859 GLintptr readOffset, GLintptr writeOffset,
1860 GLsizeiptr size)
1861 {
1862 GET_CURRENT_CONTEXT(ctx);
1863 struct gl_buffer_object *src, *dst;
1864
1865 src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
1866 GL_INVALID_OPERATION);
1867 if (!src)
1868 return;
1869
1870 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
1871 GL_INVALID_OPERATION);
1872 if (!dst)
1873 return;
1874
1875 if (_mesa_bufferobj_mapped(src)) {
1876 _mesa_error(ctx, GL_INVALID_OPERATION,
1877 "glCopyBufferSubData(readBuffer is mapped)");
1878 return;
1879 }
1880
1881 if (_mesa_bufferobj_mapped(dst)) {
1882 _mesa_error(ctx, GL_INVALID_OPERATION,
1883 "glCopyBufferSubData(writeBuffer is mapped)");
1884 return;
1885 }
1886
1887 if (readOffset < 0) {
1888 _mesa_error(ctx, GL_INVALID_VALUE,
1889 "glCopyBufferSubData(readOffset = %d)", (int) readOffset);
1890 return;
1891 }
1892
1893 if (writeOffset < 0) {
1894 _mesa_error(ctx, GL_INVALID_VALUE,
1895 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset);
1896 return;
1897 }
1898
1899 if (size < 0) {
1900 _mesa_error(ctx, GL_INVALID_VALUE,
1901 "glCopyBufferSubData(writeOffset = %d)", (int) size);
1902 return;
1903 }
1904
1905 if (readOffset + size > src->Size) {
1906 _mesa_error(ctx, GL_INVALID_VALUE,
1907 "glCopyBufferSubData(readOffset + size = %d)",
1908 (int) (readOffset + size));
1909 return;
1910 }
1911
1912 if (writeOffset + size > dst->Size) {
1913 _mesa_error(ctx, GL_INVALID_VALUE,
1914 "glCopyBufferSubData(writeOffset + size = %d)",
1915 (int) (writeOffset + size));
1916 return;
1917 }
1918
1919 if (src == dst) {
1920 if (readOffset + size <= writeOffset) {
1921 /* OK */
1922 }
1923 else if (writeOffset + size <= readOffset) {
1924 /* OK */
1925 }
1926 else {
1927 /* overlapping src/dst is illegal */
1928 _mesa_error(ctx, GL_INVALID_VALUE,
1929 "glCopyBufferSubData(overlapping src/dst)");
1930 return;
1931 }
1932 }
1933
1934 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1935 }
1936
1937
1938 /**
1939 * See GL_ARB_map_buffer_range spec
1940 */
1941 void * GLAPIENTRY
1942 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1943 GLbitfield access)
1944 {
1945 GET_CURRENT_CONTEXT(ctx);
1946 struct gl_buffer_object *bufObj;
1947 void *map;
1948 GLbitfield allowed_access;
1949
1950 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1951
1952 if (!ctx->Extensions.ARB_map_buffer_range) {
1953 _mesa_error(ctx, GL_INVALID_OPERATION,
1954 "glMapBufferRange(extension not supported)");
1955 return NULL;
1956 }
1957
1958 if (offset < 0) {
1959 _mesa_error(ctx, GL_INVALID_VALUE,
1960 "glMapBufferRange(offset = %ld)", (long)offset);
1961 return NULL;
1962 }
1963
1964 if (length < 0) {
1965 _mesa_error(ctx, GL_INVALID_VALUE,
1966 "glMapBufferRange(length = %ld)", (long)length);
1967 return NULL;
1968 }
1969
1970 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
1971 *
1972 * "An INVALID_OPERATION error is generated for any of the following
1973 * conditions:
1974 *
1975 * * <length> is zero."
1976 */
1977 if (_mesa_is_gles(ctx) && length == 0) {
1978 _mesa_error(ctx, GL_INVALID_OPERATION,
1979 "glMapBufferRange(length = 0)");
1980 return NULL;
1981 }
1982
1983 allowed_access = GL_MAP_READ_BIT |
1984 GL_MAP_WRITE_BIT |
1985 GL_MAP_INVALIDATE_RANGE_BIT |
1986 GL_MAP_INVALIDATE_BUFFER_BIT |
1987 GL_MAP_FLUSH_EXPLICIT_BIT |
1988 GL_MAP_UNSYNCHRONIZED_BIT;
1989
1990 if (ctx->Extensions.ARB_buffer_storage) {
1991 allowed_access |= GL_MAP_PERSISTENT_BIT |
1992 GL_MAP_COHERENT_BIT;
1993 }
1994
1995 if (access & ~allowed_access) {
1996 /* generate an error if any other than allowed bit is set */
1997 _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
1998 return NULL;
1999 }
2000
2001 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
2002 _mesa_error(ctx, GL_INVALID_OPERATION,
2003 "glMapBufferRange(access indicates neither read or write)");
2004 return NULL;
2005 }
2006
2007 if ((access & GL_MAP_READ_BIT) &&
2008 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
2009 GL_MAP_INVALIDATE_BUFFER_BIT |
2010 GL_MAP_UNSYNCHRONIZED_BIT))) {
2011 _mesa_error(ctx, GL_INVALID_OPERATION,
2012 "glMapBufferRange(invalid access flags)");
2013 return NULL;
2014 }
2015
2016 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
2017 ((access & GL_MAP_WRITE_BIT) == 0)) {
2018 _mesa_error(ctx, GL_INVALID_OPERATION,
2019 "glMapBufferRange(invalid access flags)");
2020 return NULL;
2021 }
2022
2023 bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
2024 if (!bufObj)
2025 return NULL;
2026
2027 if (access & GL_MAP_READ_BIT &&
2028 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
2029 _mesa_error(ctx, GL_INVALID_OPERATION,
2030 "glMapBufferRange(invalid read flag)");
2031 return NULL;
2032 }
2033
2034 if (access & GL_MAP_WRITE_BIT &&
2035 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
2036 _mesa_error(ctx, GL_INVALID_OPERATION,
2037 "glMapBufferRange(invalid write flag)");
2038 return NULL;
2039 }
2040
2041 if (access & GL_MAP_COHERENT_BIT &&
2042 !(bufObj->StorageFlags & GL_MAP_COHERENT_BIT)) {
2043 _mesa_error(ctx, GL_INVALID_OPERATION,
2044 "glMapBufferRange(invalid coherent flag)");
2045 return NULL;
2046 }
2047
2048 if (access & GL_MAP_PERSISTENT_BIT &&
2049 !(bufObj->StorageFlags & GL_MAP_PERSISTENT_BIT)) {
2050 _mesa_error(ctx, GL_INVALID_OPERATION,
2051 "glMapBufferRange(invalid persistent flag)");
2052 return NULL;
2053 }
2054
2055 if (offset + length > bufObj->Size) {
2056 _mesa_error(ctx, GL_INVALID_VALUE,
2057 "glMapBufferRange(offset + length > size)");
2058 return NULL;
2059 }
2060
2061 if (_mesa_bufferobj_mapped(bufObj)) {
2062 _mesa_error(ctx, GL_INVALID_OPERATION,
2063 "glMapBufferRange(buffer already mapped)");
2064 return NULL;
2065 }
2066
2067 if (!bufObj->Size) {
2068 _mesa_error(ctx, GL_OUT_OF_MEMORY,
2069 "glMapBufferRange(buffer size = 0)");
2070 return NULL;
2071 }
2072
2073 /* Mapping zero bytes should return a non-null pointer. */
2074 if (!length) {
2075 static long dummy = 0;
2076 bufObj->Pointer = &dummy;
2077 bufObj->Length = length;
2078 bufObj->Offset = offset;
2079 bufObj->AccessFlags = access;
2080 return bufObj->Pointer;
2081 }
2082
2083 ASSERT(ctx->Driver.MapBufferRange);
2084 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj);
2085 if (!map) {
2086 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
2087 }
2088 else {
2089 /* The driver callback should have set all these fields.
2090 * This is important because other modules (like VBO) might call
2091 * the driver function directly.
2092 */
2093 ASSERT(bufObj->Pointer == map);
2094 ASSERT(bufObj->Length == length);
2095 ASSERT(bufObj->Offset == offset);
2096 ASSERT(bufObj->AccessFlags == access);
2097 }
2098
2099 return map;
2100 }
2101
2102
2103 /**
2104 * See GL_ARB_map_buffer_range spec
2105 */
2106 void GLAPIENTRY
2107 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
2108 {
2109 GET_CURRENT_CONTEXT(ctx);
2110 struct gl_buffer_object *bufObj;
2111
2112 if (!ctx->Extensions.ARB_map_buffer_range) {
2113 _mesa_error(ctx, GL_INVALID_OPERATION,
2114 "glFlushMappedBufferRange(extension not supported)");
2115 return;
2116 }
2117
2118 if (offset < 0) {
2119 _mesa_error(ctx, GL_INVALID_VALUE,
2120 "glFlushMappedBufferRange(offset = %ld)", (long)offset);
2121 return;
2122 }
2123
2124 if (length < 0) {
2125 _mesa_error(ctx, GL_INVALID_VALUE,
2126 "glFlushMappedBufferRange(length = %ld)", (long)length);
2127 return;
2128 }
2129
2130 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
2131 GL_INVALID_OPERATION);
2132 if (!bufObj)
2133 return;
2134
2135 if (!_mesa_bufferobj_mapped(bufObj)) {
2136 /* buffer is not mapped */
2137 _mesa_error(ctx, GL_INVALID_OPERATION,
2138 "glFlushMappedBufferRange(buffer is not mapped)");
2139 return;
2140 }
2141
2142 if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
2143 _mesa_error(ctx, GL_INVALID_OPERATION,
2144 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
2145 return;
2146 }
2147
2148 if (offset + length > bufObj->Length) {
2149 _mesa_error(ctx, GL_INVALID_VALUE,
2150 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
2151 (long)offset, (long)length, (long)bufObj->Length);
2152 return;
2153 }
2154
2155 ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
2156
2157 if (ctx->Driver.FlushMappedBufferRange)
2158 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj);
2159 }
2160
2161
2162 static GLenum
2163 buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2164 {
2165 struct gl_buffer_object *bufObj;
2166 GLenum retval;
2167
2168 bufObj = _mesa_lookup_bufferobj(ctx, name);
2169 if (!bufObj) {
2170 _mesa_error(ctx, GL_INVALID_VALUE,
2171 "glObjectPurgeable(name = 0x%x)", name);
2172 return 0;
2173 }
2174 if (!_mesa_is_bufferobj(bufObj)) {
2175 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
2176 return 0;
2177 }
2178
2179 if (bufObj->Purgeable) {
2180 _mesa_error(ctx, GL_INVALID_OPERATION,
2181 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2182 return GL_VOLATILE_APPLE;
2183 }
2184
2185 bufObj->Purgeable = GL_TRUE;
2186
2187 retval = GL_VOLATILE_APPLE;
2188 if (ctx->Driver.BufferObjectPurgeable)
2189 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
2190
2191 return retval;
2192 }
2193
2194
2195 static GLenum
2196 renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2197 {
2198 struct gl_renderbuffer *bufObj;
2199 GLenum retval;
2200
2201 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2202 if (!bufObj) {
2203 _mesa_error(ctx, GL_INVALID_VALUE,
2204 "glObjectUnpurgeable(name = 0x%x)", name);
2205 return 0;
2206 }
2207
2208 if (bufObj->Purgeable) {
2209 _mesa_error(ctx, GL_INVALID_OPERATION,
2210 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2211 return GL_VOLATILE_APPLE;
2212 }
2213
2214 bufObj->Purgeable = GL_TRUE;
2215
2216 retval = GL_VOLATILE_APPLE;
2217 if (ctx->Driver.RenderObjectPurgeable)
2218 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
2219
2220 return retval;
2221 }
2222
2223
2224 static GLenum
2225 texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2226 {
2227 struct gl_texture_object *bufObj;
2228 GLenum retval;
2229
2230 bufObj = _mesa_lookup_texture(ctx, name);
2231 if (!bufObj) {
2232 _mesa_error(ctx, GL_INVALID_VALUE,
2233 "glObjectPurgeable(name = 0x%x)", name);
2234 return 0;
2235 }
2236
2237 if (bufObj->Purgeable) {
2238 _mesa_error(ctx, GL_INVALID_OPERATION,
2239 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2240 return GL_VOLATILE_APPLE;
2241 }
2242
2243 bufObj->Purgeable = GL_TRUE;
2244
2245 retval = GL_VOLATILE_APPLE;
2246 if (ctx->Driver.TextureObjectPurgeable)
2247 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
2248
2249 return retval;
2250 }
2251
2252
2253 GLenum GLAPIENTRY
2254 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2255 {
2256 GLenum retval;
2257
2258 GET_CURRENT_CONTEXT(ctx);
2259 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2260
2261 if (name == 0) {
2262 _mesa_error(ctx, GL_INVALID_VALUE,
2263 "glObjectPurgeable(name = 0x%x)", name);
2264 return 0;
2265 }
2266
2267 switch (option) {
2268 case GL_VOLATILE_APPLE:
2269 case GL_RELEASED_APPLE:
2270 /* legal */
2271 break;
2272 default:
2273 _mesa_error(ctx, GL_INVALID_ENUM,
2274 "glObjectPurgeable(name = 0x%x) invalid option: %d",
2275 name, option);
2276 return 0;
2277 }
2278
2279 switch (objectType) {
2280 case GL_TEXTURE:
2281 retval = texture_object_purgeable(ctx, name, option);
2282 break;
2283 case GL_RENDERBUFFER_EXT:
2284 retval = renderbuffer_purgeable(ctx, name, option);
2285 break;
2286 case GL_BUFFER_OBJECT_APPLE:
2287 retval = buffer_object_purgeable(ctx, name, option);
2288 break;
2289 default:
2290 _mesa_error(ctx, GL_INVALID_ENUM,
2291 "glObjectPurgeable(name = 0x%x) invalid type: %d",
2292 name, objectType);
2293 return 0;
2294 }
2295
2296 /* In strict conformance to the spec, we must only return VOLATILE when
2297 * when passed the VOLATILE option. Madness.
2298 *
2299 * XXX First fix the spec, then fix me.
2300 */
2301 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
2302 }
2303
2304
2305 static GLenum
2306 buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2307 {
2308 struct gl_buffer_object *bufObj;
2309 GLenum retval;
2310
2311 bufObj = _mesa_lookup_bufferobj(ctx, name);
2312 if (!bufObj) {
2313 _mesa_error(ctx, GL_INVALID_VALUE,
2314 "glObjectUnpurgeable(name = 0x%x)", name);
2315 return 0;
2316 }
2317
2318 if (! bufObj->Purgeable) {
2319 _mesa_error(ctx, GL_INVALID_OPERATION,
2320 "glObjectUnpurgeable(name = 0x%x) object is "
2321 " already \"unpurged\"", name);
2322 return 0;
2323 }
2324
2325 bufObj->Purgeable = GL_FALSE;
2326
2327 retval = option;
2328 if (ctx->Driver.BufferObjectUnpurgeable)
2329 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
2330
2331 return retval;
2332 }
2333
2334
2335 static GLenum
2336 renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2337 {
2338 struct gl_renderbuffer *bufObj;
2339 GLenum retval;
2340
2341 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2342 if (!bufObj) {
2343 _mesa_error(ctx, GL_INVALID_VALUE,
2344 "glObjectUnpurgeable(name = 0x%x)", name);
2345 return 0;
2346 }
2347
2348 if (! bufObj->Purgeable) {
2349 _mesa_error(ctx, GL_INVALID_OPERATION,
2350 "glObjectUnpurgeable(name = 0x%x) object is "
2351 " already \"unpurged\"", name);
2352 return 0;
2353 }
2354
2355 bufObj->Purgeable = GL_FALSE;
2356
2357 retval = option;
2358 if (ctx->Driver.RenderObjectUnpurgeable)
2359 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
2360
2361 return retval;
2362 }
2363
2364
2365 static GLenum
2366 texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2367 {
2368 struct gl_texture_object *bufObj;
2369 GLenum retval;
2370
2371 bufObj = _mesa_lookup_texture(ctx, name);
2372 if (!bufObj) {
2373 _mesa_error(ctx, GL_INVALID_VALUE,
2374 "glObjectUnpurgeable(name = 0x%x)", name);
2375 return 0;
2376 }
2377
2378 if (! bufObj->Purgeable) {
2379 _mesa_error(ctx, GL_INVALID_OPERATION,
2380 "glObjectUnpurgeable(name = 0x%x) object is"
2381 " already \"unpurged\"", name);
2382 return 0;
2383 }
2384
2385 bufObj->Purgeable = GL_FALSE;
2386
2387 retval = option;
2388 if (ctx->Driver.TextureObjectUnpurgeable)
2389 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
2390
2391 return retval;
2392 }
2393
2394
2395 GLenum GLAPIENTRY
2396 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2397 {
2398 GET_CURRENT_CONTEXT(ctx);
2399 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2400
2401 if (name == 0) {
2402 _mesa_error(ctx, GL_INVALID_VALUE,
2403 "glObjectUnpurgeable(name = 0x%x)", name);
2404 return 0;
2405 }
2406
2407 switch (option) {
2408 case GL_RETAINED_APPLE:
2409 case GL_UNDEFINED_APPLE:
2410 /* legal */
2411 break;
2412 default:
2413 _mesa_error(ctx, GL_INVALID_ENUM,
2414 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2415 name, option);
2416 return 0;
2417 }
2418
2419 switch (objectType) {
2420 case GL_BUFFER_OBJECT_APPLE:
2421 return buffer_object_unpurgeable(ctx, name, option);
2422 case GL_TEXTURE:
2423 return texture_object_unpurgeable(ctx, name, option);
2424 case GL_RENDERBUFFER_EXT:
2425 return renderbuffer_unpurgeable(ctx, name, option);
2426 default:
2427 _mesa_error(ctx, GL_INVALID_ENUM,
2428 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2429 name, objectType);
2430 return 0;
2431 }
2432 }
2433
2434
2435 static void
2436 get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
2437 GLenum pname, GLint *params)
2438 {
2439 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
2440 if (!bufObj) {
2441 _mesa_error(ctx, GL_INVALID_VALUE,
2442 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
2443 return;
2444 }
2445
2446 switch (pname) {
2447 case GL_PURGEABLE_APPLE:
2448 *params = bufObj->Purgeable;
2449 break;
2450 default:
2451 _mesa_error(ctx, GL_INVALID_ENUM,
2452 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2453 name, pname);
2454 break;
2455 }
2456 }
2457
2458
2459 static void
2460 get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
2461 GLenum pname, GLint *params)
2462 {
2463 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
2464 if (!rb) {
2465 _mesa_error(ctx, GL_INVALID_VALUE,
2466 "glObjectUnpurgeable(name = 0x%x)", name);
2467 return;
2468 }
2469
2470 switch (pname) {
2471 case GL_PURGEABLE_APPLE:
2472 *params = rb->Purgeable;
2473 break;
2474 default:
2475 _mesa_error(ctx, GL_INVALID_ENUM,
2476 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2477 name, pname);
2478 break;
2479 }
2480 }
2481
2482
2483 static void
2484 get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
2485 GLenum pname, GLint *params)
2486 {
2487 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
2488 if (!texObj) {
2489 _mesa_error(ctx, GL_INVALID_VALUE,
2490 "glObjectUnpurgeable(name = 0x%x)", name);
2491 return;
2492 }
2493
2494 switch (pname) {
2495 case GL_PURGEABLE_APPLE:
2496 *params = texObj->Purgeable;
2497 break;
2498 default:
2499 _mesa_error(ctx, GL_INVALID_ENUM,
2500 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2501 name, pname);
2502 break;
2503 }
2504 }
2505
2506
2507 void GLAPIENTRY
2508 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
2509 GLint *params)
2510 {
2511 GET_CURRENT_CONTEXT(ctx);
2512
2513 if (name == 0) {
2514 _mesa_error(ctx, GL_INVALID_VALUE,
2515 "glGetObjectParameteriv(name = 0x%x)", name);
2516 return;
2517 }
2518
2519 switch (objectType) {
2520 case GL_TEXTURE:
2521 get_texture_object_parameteriv(ctx, name, pname, params);
2522 break;
2523 case GL_BUFFER_OBJECT_APPLE:
2524 get_buffer_object_parameteriv(ctx, name, pname, params);
2525 break;
2526 case GL_RENDERBUFFER_EXT:
2527 get_renderbuffer_parameteriv(ctx, name, pname, params);
2528 break;
2529 default:
2530 _mesa_error(ctx, GL_INVALID_ENUM,
2531 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2532 name, objectType);
2533 }
2534 }
2535
2536 static void
2537 set_ubo_binding(struct gl_context *ctx,
2538 int index,
2539 struct gl_buffer_object *bufObj,
2540 GLintptr offset,
2541 GLsizeiptr size,
2542 GLboolean autoSize)
2543 {
2544 struct gl_uniform_buffer_binding *binding;
2545
2546 binding = &ctx->UniformBufferBindings[index];
2547 if (binding->BufferObject == bufObj &&
2548 binding->Offset == offset &&
2549 binding->Size == size &&
2550 binding->AutomaticSize == autoSize) {
2551 return;
2552 }
2553
2554 FLUSH_VERTICES(ctx, 0);
2555 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
2556
2557 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2558 binding->Offset = offset;
2559 binding->Size = size;
2560 binding->AutomaticSize = autoSize;
2561 }
2562
2563 /**
2564 * Bind a region of a buffer object to a uniform block binding point.
2565 * \param index the uniform buffer binding point index
2566 * \param bufObj the buffer object
2567 * \param offset offset to the start of buffer object region
2568 * \param size size of the buffer object region
2569 */
2570 static void
2571 bind_buffer_range_uniform_buffer(struct gl_context *ctx,
2572 GLuint index,
2573 struct gl_buffer_object *bufObj,
2574 GLintptr offset,
2575 GLsizeiptr size)
2576 {
2577 if (index >= ctx->Const.MaxUniformBufferBindings) {
2578 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
2579 return;
2580 }
2581
2582 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
2583 _mesa_error(ctx, GL_INVALID_VALUE,
2584 "glBindBufferRange(offset misalgned %d/%d)", (int) offset,
2585 ctx->Const.UniformBufferOffsetAlignment);
2586 return;
2587 }
2588
2589 if (bufObj == ctx->Shared->NullBufferObj) {
2590 offset = -1;
2591 size = -1;
2592 }
2593
2594 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2595 set_ubo_binding(ctx, index, bufObj, offset, size, GL_FALSE);
2596 }
2597
2598
2599 /**
2600 * Bind a buffer object to a uniform block binding point.
2601 * As above, but offset = 0.
2602 */
2603 static void
2604 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
2605 GLuint index,
2606 struct gl_buffer_object *bufObj)
2607 {
2608 if (index >= ctx->Const.MaxUniformBufferBindings) {
2609 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2610 return;
2611 }
2612
2613 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2614 if (bufObj == ctx->Shared->NullBufferObj)
2615 set_ubo_binding(ctx, index, bufObj, -1, -1, GL_TRUE);
2616 else
2617 set_ubo_binding(ctx, index, bufObj, 0, 0, GL_TRUE);
2618 }
2619
2620 static void
2621 set_atomic_buffer_binding(struct gl_context *ctx,
2622 unsigned index,
2623 struct gl_buffer_object *bufObj,
2624 GLintptr offset,
2625 GLsizeiptr size,
2626 const char *name)
2627 {
2628 struct gl_atomic_buffer_binding *binding;
2629
2630 if (index >= ctx->Const.MaxAtomicBufferBindings) {
2631 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
2632 return;
2633 }
2634
2635 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
2636 _mesa_error(ctx, GL_INVALID_VALUE,
2637 "%s(offset misalgned %d/%d)", name, (int) offset,
2638 ATOMIC_COUNTER_SIZE);
2639 return;
2640 }
2641
2642 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
2643
2644 binding = &ctx->AtomicBufferBindings[index];
2645 if (binding->BufferObject == bufObj &&
2646 binding->Offset == offset &&
2647 binding->Size == size) {
2648 return;
2649 }
2650
2651 FLUSH_VERTICES(ctx, 0);
2652 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
2653
2654 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2655
2656 if (bufObj == ctx->Shared->NullBufferObj) {
2657 binding->Offset = -1;
2658 binding->Size = -1;
2659 } else {
2660 binding->Offset = offset;
2661 binding->Size = size;
2662 }
2663 }
2664
2665 void GLAPIENTRY
2666 _mesa_BindBufferRange(GLenum target, GLuint index,
2667 GLuint buffer, GLintptr offset, GLsizeiptr size)
2668 {
2669 GET_CURRENT_CONTEXT(ctx);
2670 struct gl_buffer_object *bufObj;
2671
2672 if (buffer == 0) {
2673 bufObj = ctx->Shared->NullBufferObj;
2674 } else {
2675 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2676 }
2677 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
2678 &bufObj, "glBindBufferRange"))
2679 return;
2680
2681 if (!bufObj) {
2682 _mesa_error(ctx, GL_INVALID_OPERATION,
2683 "glBindBufferRange(invalid buffer=%u)", buffer);
2684 return;
2685 }
2686
2687 if (buffer != 0) {
2688 if (size <= 0) {
2689 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
2690 (int) size);
2691 return;
2692 }
2693 }
2694
2695 switch (target) {
2696 case GL_TRANSFORM_FEEDBACK_BUFFER:
2697 _mesa_bind_buffer_range_transform_feedback(ctx, index, bufObj,
2698 offset, size);
2699 return;
2700 case GL_UNIFORM_BUFFER:
2701 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
2702 return;
2703 case GL_ATOMIC_COUNTER_BUFFER:
2704 set_atomic_buffer_binding(ctx, index, bufObj, offset, size,
2705 "glBindBufferRange");
2706 return;
2707 default:
2708 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
2709 return;
2710 }
2711 }
2712
2713 void GLAPIENTRY
2714 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
2715 {
2716 GET_CURRENT_CONTEXT(ctx);
2717 struct gl_buffer_object *bufObj;
2718
2719 if (buffer == 0) {
2720 bufObj = ctx->Shared->NullBufferObj;
2721 } else {
2722 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2723 }
2724 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
2725 &bufObj, "glBindBufferBase"))
2726 return;
2727
2728 if (!bufObj) {
2729 _mesa_error(ctx, GL_INVALID_OPERATION,
2730 "glBindBufferBase(invalid buffer=%u)", buffer);
2731 return;
2732 }
2733
2734 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
2735 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
2736 *
2737 * "BindBufferBase is equivalent to calling BindBufferRange with offset
2738 * zero and size equal to the size of buffer."
2739 *
2740 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
2741 *
2742 * "If the parameter (starting offset or size) was not specified when the
2743 * buffer object was bound, zero is returned."
2744 *
2745 * What happens if the size of the buffer changes? Does the size of the
2746 * buffer at the moment glBindBufferBase was called still play a role, like
2747 * the first quote would imply, or is the size meaningless in the
2748 * glBindBufferBase case like the second quote would suggest? The GL 4.1
2749 * core spec page 45 says:
2750 *
2751 * "It is equivalent to calling BindBufferRange with offset zero, while
2752 * size is determined by the size of the bound buffer at the time the
2753 * binding is used."
2754 *
2755 * My interpretation is that the GL 4.1 spec was a clarification of the
2756 * behavior, not a change. In particular, this choice will only make
2757 * rendering work in cases where it would have had undefined results.
2758 */
2759
2760 switch (target) {
2761 case GL_TRANSFORM_FEEDBACK_BUFFER:
2762 _mesa_bind_buffer_base_transform_feedback(ctx, index, bufObj);
2763 return;
2764 case GL_UNIFORM_BUFFER:
2765 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
2766 return;
2767 case GL_ATOMIC_COUNTER_BUFFER:
2768 set_atomic_buffer_binding(ctx, index, bufObj, 0, 0,
2769 "glBindBufferBase");
2770 return;
2771 default:
2772 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
2773 return;
2774 }
2775 }
2776
2777 void GLAPIENTRY
2778 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
2779 GLsizeiptr length)
2780 {
2781 GET_CURRENT_CONTEXT(ctx);
2782 struct gl_buffer_object *bufObj;
2783 const GLintptr end = offset + length;
2784
2785 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2786 if (!bufObj) {
2787 _mesa_error(ctx, GL_INVALID_VALUE,
2788 "glInvalidateBufferSubData(name = 0x%x) invalid object",
2789 buffer);
2790 return;
2791 }
2792
2793 /* The GL_ARB_invalidate_subdata spec says:
2794 *
2795 * "An INVALID_VALUE error is generated if <offset> or <length> is
2796 * negative, or if <offset> + <length> is greater than the value of
2797 * BUFFER_SIZE."
2798 */
2799 if (end < 0 || end > bufObj->Size) {
2800 _mesa_error(ctx, GL_INVALID_VALUE,
2801 "glInvalidateBufferSubData(invalid offset or length)");
2802 return;
2803 }
2804
2805 /* The GL_ARB_invalidate_subdata spec says:
2806 *
2807 * "An INVALID_OPERATION error is generated if the buffer is currently
2808 * mapped by MapBuffer, or if the invalidate range intersects the range
2809 * currently mapped by MapBufferRange."
2810 */
2811 if (bufferobj_range_mapped(bufObj, offset, length)) {
2812 _mesa_error(ctx, GL_INVALID_OPERATION,
2813 "glInvalidateBufferSubData(intersection with mapped "
2814 "range)");
2815 return;
2816 }
2817
2818 /* We don't actually do anything for this yet. Just return after
2819 * validating the parameters and generating the required errors.
2820 */
2821 return;
2822 }
2823
2824 void GLAPIENTRY
2825 _mesa_InvalidateBufferData(GLuint buffer)
2826 {
2827 GET_CURRENT_CONTEXT(ctx);
2828 struct gl_buffer_object *bufObj;
2829
2830 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2831 if (!bufObj) {
2832 _mesa_error(ctx, GL_INVALID_VALUE,
2833 "glInvalidateBufferData(name = 0x%x) invalid object",
2834 buffer);
2835 return;
2836 }
2837
2838 /* The GL_ARB_invalidate_subdata spec says:
2839 *
2840 * "An INVALID_OPERATION error is generated if the buffer is currently
2841 * mapped by MapBuffer, or if the invalidate range intersects the range
2842 * currently mapped by MapBufferRange."
2843 */
2844 if (_mesa_bufferobj_mapped(bufObj)) {
2845 _mesa_error(ctx, GL_INVALID_OPERATION,
2846 "glInvalidateBufferData(intersection with mapped "
2847 "range)");
2848 return;
2849 }
2850
2851 /* We don't actually do anything for this yet. Just return after
2852 * validating the parameters and generating the required errors.
2853 */
2854 return;
2855 }